Concatenate n number of numpy arrays columnwise in python - python

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)

Related

Turning a Multi-Dimensional Array Into A Linear One Dimensional Array

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

How do I remove all elements from a numpy array that are equal to zero in another array?

How do I remove all elements from a numpy array that are equal to zero in another array?
I want to use the indices of one numpy array mask where mask==0 to delete the elements of another same-shaped array array.
I tried something like np.delete(array,mask==0) but that just gave me an error.
use np.where
array = array[np.where(another_array != 0)]

Numpy - Compare elements in two 2D arrays and replace values

I have a specific requirement for this problem. I need it to be simple and fast.
My problem:
I have two 2D arrays and I need to replace values in 1. array by values in 2. array according to condition. That is if element in x,y position in 1. array is smaller than element in x,y position in 2. array, then replace element in 1. array by element in 2. array.
what I tried and is not working:
import numpy as np
arr = np.random.randint(3,size=(2, 2))
arr2 = np.random.randint(3,size=(2, 2))
print(arr)
print(arr2)
arr[arr<arr2]=arr2 # Doesnt work.
This raises TypeError:
TypeError: NumPy boolean array indexing assignment requires a 0 or 1-dimensional input, input has 2 dimensions.
I can see, that it would be possible to iterate through columns or rows, but I believe it can be done without iteration.
Thanks in advance

Creating 3D array from 2D array and Vector in Python

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

sampling 2-D numpy array using another array

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]]

Categories