I am looking for an efficient and cleaner way to achieve this as I can easily do this through list comprehension.
I have a large 2D numpy array A of shape 20,000x500 and a vector numpy array B of shape 20,000.
I want to concatenate B with each column of A to create a list of length 500 where each element of the list is as follows:
np.hstack(A[:, i], B.reshape(-1,1)) where i is in range(0,500)
I am currently using list comprehension which makes it appear messy.
Edit:
Output is list of length 500 with each element of list an array of shape 20,000x2
Related
I am a bit new to python and I have a large list of data whose shape is as such:
print(mydata.shape)
>>> (487, 24, 12 , 13)
One index of this array itself looks like this:
[4.22617843e-11 5.30694273e-11 3.73693923e-11 2.09353628e-11
2.42581129e-11 3.87492538e-11 2.34626762e-11 1.87155829e-11
2.99512706e-11 3.32095254e-11 4.91165476e-11 9.57019117e-11
7.86496424e-11]]]]
I am trying to take all the elements from this multi-dimensional array and put it into a one-dimensional one so I can graph it.
I would appreciate any help. Thank you.
mydata.ravel()
Will make a new array, flattened to shape:
(487*24*12*13,)
or...
(1823328,)
You can do this by using flatten()
mydata.flatten(order='C')
And order types depend on the following:
order: The order in which items from the NumPy array will be read.
‘C’: Read items from array row wise i.e. using C-like index order.
‘F’: Read items from array column wise i.e. using Fortran-like index
order.
‘A’: Read items from an array based on the memory order of items
I know that we concatenate two 2-D numpy
arrays named arr1 and arr2 with same number of rows with the help of following command:
np.concatenate((arr1,arr2),axis=1)
But I have n number of numpy arrays (I haven't done global variable name assignment to these arrays) in a list ,say, list_array which is a list containing n elements where each element is a 2-D array. We need looping or any efficient program would be okay.
Question
How can I concatenate these elements of the list which are 2-D arrays column wise?
Thank you
I am not from CS background. Any help will be appreciated
Just a side note,
Concatenating with np.concatenate on axis=1 is equivalent to a horizontal stack: np.hstack:
>>> np.hstack(list_array)
vs
>>> np.concatenate(list_array, axis=1)
I want to stack the first n columns of a 2D array vertically. My realization is in the following:
np.vstack(input_seq[:,:n].flatten().tolist())
I am wondering if stacking 1D array directly would be faster? np.vstack(input_seq[:,:n].flatten())
Or are there any faster approaches to stack lists? Asking since I'm gonna repeat this process millions of times.
Any hint would be appreciated! Thanks!
Just reshape your array:
new = input_seq[:, :n].reshape(-1)
Since you're indexing, the reshaped array is already a copy, so you can manipulate it without changing the original array (a reshaped array points to the same data otherwise).
Note that this method makes new one dimensional, while your methods make it two dimensional. If you need your new array to be two dimensional, just reshape it with an extra dimension of 1:
new = input_seq[:, :n].reshape(-1, 1)
I want to sort a numpy array of arrays based on their last entry.
For example, say we have this array of arrays:
a=np.array([np.array([1,2,5]),np.array([1,3,0]),np.array([1,4,-17])])
I want to return the array sorted this way:
np.array([np.array([1,4,-17]),np.array([1,3,0]) ,np.array([1,2,5]) ])
I.e. as -17 <= 0 <= 5, last array becomes the first one, and so on. Elements within each array must not be altered.
I suppose numpy has a builtin but I haven't been able to find it.
I have a pretty big numpy matrix (2-D array with more than 1000 * 1000 cells), and another 2-D array of indexes in the following form: [[x1,y1],[x2,y2],...,[xn,yn]], which is also quite large (n > 1000). I want to extract all the cells in the matrix that their (x,y) coordinates appear in the array as efficient as possible, i.e. without loops. If the array was an array of tuples I could just to
cells = matrix[array]
and get what I want, but the array is not in that format, and I couldn't find an efficient way to convert it to the desired form...
You can make your array into a tuple of arrays like this:
tuple(array.T)
This matches the output style of np.where(), which can be indexed.
cells=matrix[tuple(array.T)]
you can also do standard numpy array slicing and get Divakar's answer in the comments:
cells=matrix[array[:,0],array[:,1]]