I have a numpy array a with shape (m,n,3) that I want to index into the first two columns with another array idx of shape (100,2). So what I want is the following:
np.array([a[x,y,:] for x,y in idx])
What's the most efficient way to do this?
Related
This should be trivial but I'm not finding the correct way to accomplish it. I have a 2D array with shape (5, 5527) I have an an array of indices with the lowest value for the arguments in the first axis with shape (5527,)
How can I flatten my (5,5527) array into a 1D array by only using the values at the index specified by the index array?
I thought np.take would accomplish this but it does not. It outputs an array with shape (5527,5527)
You were close: np.take_along_axis
I have a column in a Dataframe where each cell has a (300,) shaped numpy array.
When I extract the values of this column using the .values method, I get a numpy array of shape (N,) where N is the number of rows of the Dataframe. And each element of N is a (300,) array. I would have expected the extracted shape to be (Nx300).
So I would like to shape of the extracted column to be (Nx300). I tried using pd.as_matrix() but this still gets me a numpy array of shape (N,).
Any suggestions?
You can use numpy.concatenate, connvert to list and cast to array:
a = np.random.randint(10, size=300)
print (a.shape)
(300,)
df = pd.DataFrame({ 'A':[a,a,a]})
arr = np.array(np.concatenate(df.values).tolist())
print (arr.shape)
(3, 300)
I have a numpy boolean vector of shape 1 x N, and an 2d array with shape 160 x N. What is a fast way of subsetting the columns of the 2d array such that for each index of the boolean vector that has True in it, the column is kept, and for each index of the boolean vector that has False in it, the column is discarded?
If you call the vector mask and the array features, i've found the following to be far too slow: np.array([f[mask] for f in features])
Is there a better way? I feel like there has to be, right?
You can try this,
new_array = 2d_array[:,bool_array==True]
So depending on the axes you can select which one you want to remove. In case you get a 1-d array, then you can just reshape it and get the required array. This method will be faster also.
I have many numpy arrays of shape (Ni,227,227,3), where Ni of each array is different.
I want to join them and make array of shape (N1+N2+..+Nk,227,227,3) where k is the number of arrays.
I tried numpy.concatenate and numpy.append but they ask for same dimension in axis 0. I am also confused on what is axis 1 and axis 2 in my arrays.
So, the main problem here was with the one of the arrays of shape (0,) instead of (0,227,227,3).
np.concatenate(alist,axis=0) works.
I want to convert X,Y,Z numpy array to (X*Z)*Y numpy array.
Code(Slow):
def rearrange(data):
samples,channels,t_insts=data.shape
append_data=np.empty([0,channels])
for sample in range(0,samples):
for t_inst in range(0,t_insts):
channel_data=data[sample,:,t_inst]
append_data=np.vstack((append_data,channel_data))
return append_data.shape
I am looking for a better vectorized approach if possible
You can use np.transpose to swap rows with columns and then reshape -
data.transpose(0,2,1).reshape(-1,data.shape[1])
Or use np.swapaxes to do the swapping of rows and columns and then reshape -
data.swapaxes(1,2).reshape(-1,data.shape[1])