I'm trying to get values from an ndarray with indices in another ndarray but I keep getting this error
IndexError too many indices for array.
The array that I'm trying to get the values from, scores , has scores.shape = (10,10000)
and the array pointing out the indices, indices , has indices.shape = (10000,2)
I'm trying to get the values this way:
values = scores[tuple(indices)]
but this is where I get the error.
What I'm trying to do this way is to access several individual values of scores, e.g. scores[0,6], scores[1,9] in another array so I get something like
[scores[0,6],scores[1,9],...]
all in one go and avoiding loops. Those [[0,6] , [1,9], ...] are stored in the indices array. I mention the previous in case that could lead to a work around.
Try the following: scores[indices[:,0],indices[:,1]]. Or alternatively, scores[tuple(indices.T)].
When you do scores[tuple(indices)], tuple(indices) is creating a tuple of 2-element arrays. Numpy interprets this as you trying to get 2 elements of a 10,000 dimensional array! For the sort of indexing you need, Numpy expects arrays of values for each dimension. In other words, rather than ( [x1,y1], [x2,y2] ), it wants ( [x1,x2], [y1, y2] ).
Related
I am new to programming in Python. I was trying to use the sorted command to arrange my 2D array from a csv file in ascending order of the first column elements. However, instead of outputting a 2D array, it gives me a list of arrays instead. Here is an example of the problem I faced.
not_ordered_array = [[-0.633, 0.03279], [-7.330e-01, 3.564e-04], [-0.567 , 0.06843], [-7.000e-01, -5.551e-17], [-0.667, 0.006772], [-0.533, 0.06808], [-0.633, 0.03279]]
ordered_array = sorted(not_ordered_array, key = lambda x: x[0], reverse = False)
print(ordered_array)
[array([-7.330e-01, 3.564e-04]), array([-7.000e-01, -5.551e-17]), array([-0.667, 0.006772]), array([-0.633, 0.03279]), array([-0.6, 0.05097]), array([-0.567 , 0.06843]), array([-0.533, 0.06808])]
I am trying to use operations on the ordered_array after this. How do I get the ordered_array in a 2D array instead of a list of arrays?
I know how to do this if I know the number of dimensions of the array when coding. I have seen Select 'area' from a 2D array in python
I am trying to figure out how to extract a "volume" from an any dimensional array.
I know how to slice arrays. a[0: 10] I know how to use that.
What I essentially want is a [lower_bound: higher_bound]. But the bounds are arrays that specify the locations in each dimension.
Something like 0:2, 2:4 in the answer you linked is just a tuple of range objects. You can create such a tuple yourself using any code you want, and then do a[slice(*t)] (ref) to slice with that tuple.
Suppose I have multiple NxN 2D arrays stored into a list in Python 3. I want to collapse all the arrays into 1 array, with the same dimensions NxN, but such that each element of this new array contains a 1xN array of the corresponding values from the original arrays.
To give you some more context, each array in this list corresponds to the set of values at a given time. For each new time point, I am storing the updated version of that array into the list. Once that's done, I want to compute the standard deviation of the values at each (i,j) element in the array.
I tried using a for loop, but it takes far too long for my simulations because this is a set of 100,000 arrays. I was wondering if there were any numpy or vectorized functions that can help me perform this operation more efficiently. Thanks!
Lets say l is your list of arrays. You need to get std of corresponding elements of those arrays into a single array:
std_l = np.std(np.stack(l),axis=0)
lets say I have 10 arrays of size 10x10 each, called a,b,c...j
I create np.zeros of 10x2 called arr. I've already made arr[:,1]=1,2,3...10
so as of now my new array is
[0,1]
[0,2]
...
[0,10]
What i need now is to make
arr[0,0] = a, arr[1,0] = b ... arr[10,0]=j
Since Im also dealing tensors, the error I get is
"only one element tensors can be converted to Python scalars"
Basically, to reiterate, I want to put those 2D arrays into my new array as individual elements of the new array.
Final output reqd:
[a,1]
[b,2]
...
[j,10]
EDIT: The final array will be multidimensional, but Im not sure of the value of the final shape, 10x10x2 maybe??
EDIT: Think of the problem as images and labels. I want to store an entire image and its corresponding label as each row of an array
I have an array of 2d indices.
indices = [[2,4], [6,77], [102,554]]
Now, I have a different 4-dimensional array, arr, and I want to only extract an array (it is an array, since it is 4-dimensional) with corresponding index in the indices array. It is equivalent to the following code.
for i in range(len(indices)):
output[i] = arr[indices[i][0], indices[i][1]]
However, I realized that using explicit for-loop yields a slow result. Is there any built-in numpy API that I can utilized? At this point, I tried using np.choose, np.put, np.take, but did not succeed to yield what I wanted. Thank you!
We need to index into the first two axes with the two columns from indices (thinking of it as an array).
Thus, simply convert to array and index, like so -
indices_arr = np.array(indices)
out = arr[indices_arr[:,0], indices_arr[:,1]]
Or we could extract those directly without converting to array and then index -
d0,d1 = [i[0] for i in indices], [i[1] for i in indices]
out = arr[d0,d1]
Another way to extract the elements would be with conversion to tuple, like so -
out = arr[tuple(indices_arr.T)]
If indices is already an array, skip the conversion process and use indices in places where we had indices_arr.
Try using the take function of numpy arrays. Your code should be something like:
outputarray= np.take(arr,indices)