我正在實施最近質心分類演算法,但在我的情況下,我有點不知道如何使用 numpy.mean 。
所以假設我有一些球形資料集X:
[[ 0.39151059 3.48203037]
[-0.68677876 1.45377717]
[ 2.30803493 4.19341503]
[ 0.50395297 2.87076658]
[ 0.06677012 3.23265678]
[-0.24135103 3.78044279]
[-0.05660036 2.37695381]
[ 0.74210998 -3.2654815 ]
[ 0.05815341 -2.41905942]
[ 0.72126958 -1.71081388]
[ 1.03581142 -4.09666955]
[ 0.23209714 -1.86675298]
[-0.49136284 -1.55736028]
[ 0.00654881 -2.22505305]]]
和標記的向量Y:
[0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1.]
具有 100 個 2D 資料點的示例給出以下結果:

NCC 演算法包括首先計算每個類的類均值(0 和 1:即藍色和紅色),然后為下一個資料點計算最近的類質心。
這是我目前的功能:
def mean_ncc(X,Y):
# find unique classes
m_cids = np.unique(Y) #[0. 1.]
# compute class means
mu = np.zeros((len(cids), X.shape[1])) #[[0. 0.] [0. 0.]] (in the case where Y has 2 unique points (0 and 1)
for class_idx, class_label in enumerate(cids):
mu[class_idx, :] = #problem here
return mu
所以在這里我想要一個包含“0”(藍色)點和“1”(紅色)點的類均值的陣列:如何指定要計算其均值的 X 元素的數量?我想做這樣的事情:
for class_idx, class_label in enumerate(m_cids):
mu[class_idx, :] = np.mean(X[only the elements,that contains the same class_label], axis=0)
有可能還是有其他方法可以實作這一點?
uj5u.com熱心網友回復:
你可以使用這樣的東西:
import numpy as np
tags = [0, 0, 1, 1, 0, 1]
values = [5, 4, 2, 5, 9, 8]
tags_np = np.array(tags)
values_np = np.array(values)
print(values_np[tags_np == 1].mean())
編輯:您肯定需要更多地研究平均函式的軸引數:
import numpy as np
values = [[5, 4],
[5, 4],
[4, 3],
[4, 3]]
values_np = np.array(values)
tags_np = np.array([0, 0, 1, 1])
print(values_np[tags_np == 0].mean(axis=0))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/463184.html
