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
Related
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)
I have two numpy arrays, one bigger than another, but both with the same number of dimensions.
I want to get a slice from the bigger array that matches the size of the smaller array. (Starting from 0,0,0....)
So, imagine the big array has shape (10,5,7).
And the small array has shape (10,4,6).
I want to get from the bigger array this slice:
biggerArray[:10,:4,:6]
The length of the shape tuple may vary, and I want to do it for any number of dimensions (Both will always have the same number of dimensions).
How to do that? Is there a way to use tuples as ranges in slices?
Construct the tuple of slice objects manually. biggerArray[:10, :4, :6] is syntactic sugar for biggerArray[(slice(10), slice(4), slice(6))], so:
biggerArray[tuple(map(slice, smallerArray.shape))]
or
biggerArray[tuple(slice(0, n) for n in smallerArray.shape)]
You may want to assert result.shape == smallerArray.shape afterwards, just in case the input shapes weren't what you thought they were.
I have a 2-d numpy array that I convert into a 3-d array by using:
trainSet = numpy.reshape(trainSet, (trainSet.shape[0], 1, trainSet.shape[1]))
My problem is I'm trying to adjust values in the 1st axis (the new one). However, no matter what I try I can not actually figure out how to access the 1's in the new axis. I've tried a multitude of print statements such as
print(trainSet[0][0][0])
print(trainSet[1][0][1])
but no matter what I try I can not print out any 1's, just the contents of the 2-d array. I know it's a 3d array because when I use
print(trainSet.shape)
I get
(12, 1, 793)
Any help would be immensely appreciated!
The one refers to the size of that particular dimension, not the content of the array. The array [[5 5]] has shape (1, 2) but, still, you don't really expect its values must include 1 for that reason, do you?
What inserting a dimension of length one does is increasing the nesting level by one, so a 2d array [[a b] [c d]] will become [[[a b] [c d]]] or [[[a b]] [[c d]]] or [[[a] [b]] [[c] [d]]] depending on where you insert the new axis.
In particular, there are no new elements.
If I didn't mistake your question, what you want is
trainSet[:,0,:]
the amount of values remains the same, you're just creating a 3D structure instead of the 2D structure.
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] ).