use*_*023 17 python arrays numpy random-forest
我是numpy的新手,我正在使用python中的随机林实现集群.我的问题是:
我怎样才能找到数组中确切行的索引?例如
[[ 0. 5. 2.]
[ 0. 0. 3.]
[ 0. 0. 0.]]
Run Code Online (Sandbox Code Playgroud)
我寻找[0. 0. 3.]并得到结果1(第二行的索引).
有什么建议吗?遵循代码(不工作......)
for index, element in enumerate(leaf_node.x):
for index_second_element, element_two in enumerate(leaf_node.x):
if (index <= index_second_element):
index_row = np.where(X == element)
index_column = np.where(X == element_two)
self.similarity_matrix[index_row][index_column] += 1
Run Code Online (Sandbox Code Playgroud)
Dan*_*iel 49
为什么不简单地做这样的事情?
>>> a
array([[ 0., 5., 2.],
[ 0., 0., 3.],
[ 0., 0., 0.]])
>>> b
array([ 0., 0., 3.])
>>> a==b
array([[ True, False, False],
[ True, True, True],
[ True, True, False]], dtype=bool)
>>> np.all(a==b,axis=1)
array([False, True, False], dtype=bool)
>>> np.where(np.all(a==b,axis=1))
(array([1]),)
Run Code Online (Sandbox Code Playgroud)