Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

python - Numpy Array Get row index searching by a row

I am new to numpy and I am implementing clustering with random forest in python. My question is:

How could I find the index of the exact row in an array? For example

[[ 0.  5.  2.]
 [ 0.  0.  3.]
 [ 0.  0.  0.]]

and I look for [0. 0. 3.] and get as result 1(the index of the second row).

Any suggestion? Follows the code (not working...)

    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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why not simply do something like this?

>>> 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]),)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...