How to select items by line from a NumPy array?

I have an array like this numpy array

dd= [[foo 0.567 0.611] [bar 0.469 0.479] [noo 0.220 0.269] [tar 0.480 0.508] [boo 0.324 0.324]] 

It’s like one cycle through an array, choosing foo and getting 0.567 0.611 as a float as a singleton. Then select the panel and get 0.469 0.479 as floating as a singlet .....

I could get the vector of the first elements as a list using

 dv= dd[:,1] 

The elements 'foo' and 'bar' are not unknown variables; they can change.

How can I change if the item is in position [1]?

 [[0.567 foo2 0.611] [0.469 bar2 0.479] [0.220 noo2 0.269] [0.480 tar2 0.508] [0.324 boo2 0.324]] 
+6
source share
2 answers

You put the NumPy tag in your question, so I assume that you need the NumPy syntax that the answer before mine doesn't use.

If you really want to use NumPy, then you probably don't need the strings in your array, otherwise you will also have to represent your floats as strings.

What you are looking for is NumPy syntax for accessing elements of a 2D array row by row (and exclude the first column) .

This syntax is:

 M[row_index,1:] # selects all but 1st col from row given by 'row_index' 

W / r / t the second scenario of your question is selecting non-contiguous columns :

 M[row_index,[0,2]] # selects 1st & 3rd cols from row given by 'row_index' 


A small complication in your Question is that you want to use a row for row_index, so you need to delete the rows (so that you can create a 2D NumPy array of floats), replace them with a numeric string of indices, and then create a lookup table to match strings with numerical indices lines :

 >>> import numpy as NP >>> # create a look-up table so you can remove the strings from your python nested list, >>> # which will allow you to represent your data as a 2D NumPy array with dtype=float >>> keys ['foo', 'bar', 'noo', 'tar', 'boo'] >>> values # 1D index array comprised of one float value for each unique string in 'keys' array([0., 1., 2., 3., 4.]) >>> LuT = dict(zip(keys, values)) >>> # add an index to data by inserting 'values' array as first column of the data matrix >>> A = NP.hstack((vals, A)) >>> A NP.array([ [ 0., .567, .611], [ 1., .469, .479], [ 2., .22, .269], [ 3., .48, .508], [ 4., .324, .324] ]) >>> # so now to look up an item, by 'key': >>> # write a small function to perform the look-ups: >>> def select_row(key): return A[LuT[key],1:] >>> select_row('foo') array([ 0.567, 0.611]) >>> select_row('noo') array([ 0.22 , 0.269]) 

Second scenario in your question: what if the index column changes?

 >>> # eg, move index to column 1 (as in your Q) >>> A = NP.roll(A, 1, axis=1) >>> A array([[ 0.611, 1. , 0.567], [ 0.479, 2. , 0.469], [ 0.269, 3. , 0.22 ], [ 0.508, 4. , 0.48 ], [ 0.324, 5. , 0.324]]) >>> # the original function is changed slightly, to select non-adjacent columns: >>> def select_row2(key): return A[LuT[key],[0,2]] >>> select_row2('foo') array([ 0.611, 0.567]) 
+16
source

First, the vector of the first elements

 dv = dd[:,0] 

(python is 0-indexed)

Secondly, to walk through the array (and store in a dict, for example), you write:

 dc = {} ind = 0 # this corresponds to the column with the names for row in dd: dc[row[ind]] = row[1:] 
+2
source

Source: https://habr.com/ru/post/897998/


All Articles