Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Array Get row index searching by a row

Tags:

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 
like image 775
user2801023 Avatar asked Sep 20 '13 23:09

user2801023


People also ask

How do I find a row in NumPy?

In the NumPy with the help of shape() function, we can find the number of rows and columns. In this function, we pass a matrix and it will return row and column number of the matrix. Return: The number of rows and columns.

How do I select a specific row in NumPy?

We can use [][] operator to select an element from Numpy Array i.e. Example 1: Select the element at row index 1 and column index 2. Or we can pass the comma separated list of indices representing row index & column index too i.e.


1 Answers

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]),) 
like image 68
Daniel Avatar answered Oct 01 '22 11:10

Daniel