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
Related
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 have a 2-d numpy array that I know is sorted across axis=1. Example:
[[0,0,1],
[0,7,1],
[1,1,0]]
I want to check with complexity of log(n) if the array contains a certain row, example: [0,7,1].
I thought about using search-sorted
to get the index in which the row would have to go in, and if the row is indeed in the array, it would have to be before or after this index, but can I be sure it would be one way or the other?
You can create a function that returns the index:
idxlist=[[0,0,1],[0,7,1],[1,1,0]]
def checkforindex(idxlist, idx):
return idxlist.index(idx)
print(checkforindex(idxlist, [1,1,0]))
2
I have a pandas dataframe for lists. And each one of the lists can use np.asarray(list) to convert the list to a numpy array. The shape of the array should be (263,300) ,so i do this
a=dataframe.to_numpy()
# a.shape is (100000,)
output_array=np.array([])
for list in a:
output_array=np.append(output_array,np.asarray(list))
Since there are 100000 rows in my pandas, so i expect to get
output_array.shape is (100000,263,300)
It works, but it takes long time.
I want to know which part of my code cost the most and how to solve it.
Is there a more efficient method to reach this? Thanks!
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
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]]