Combining list of 2D numpy arrays - python

How do I combine N, 2D numpy arrays (of dimension R x C) to create a 3D numpy array of shape (N, R, C)? Right now, the N-2D numpy arrays are contained inside a list, and I want that to become a 3D numpy array. Let's say X is my list of 2D numpy arrays, if I just do np.array(X), I get something of shape (N,). If I do np.vstack(X), I get something of shape (N x R, C). How do I solve this problem?

You can use np.stack:
test = np.stack([np.ones([2, 3]) for _ in range(4)])
print(test.shape) # (4, 2, 3)

you could just use :
np.array([np.array(x) for x in ArrayList])

Related

nested operations on two numpy arrays, one 2d and one 1d

Say I have one 2d numpy array X with shape (3,3) and one numpy array Y with shape (3,) where
X = np.array([[0,1,2],
[3,4,5],
[1,9,2]])
Y = np.array([[1,0,1]])
How can I create a numpy array, Z for example, from multiplying X,Y element-wise and then summation row-wise?
multiplying element-wise would yield: 0,0,2, 3,0,5, 1,0,2
then, adding each row would yield:
Z = np.array([2,8,3])
I have tried variations of
Z = np.sum(X * Y) --> adds all elements of entire array, not row-wise.
I know I can use a forloop but the dataset is very large and so I am trying to find a more efficient numpy-specific way to perform the operation. Is this possible?
You can do the following:
sum_row = np.sum(X*Y, axis=1) # axis=0 for columnwise

Most efficient way to apply operation on each element of Numpy array

Assume I have a function which takes a numpy array of shape (m, k) and I want to apply that function on each element of numpy array of shape (n, m, k).
Naive approach is to iterate through the given numpy array and append the transformed element to an empty numpy array of shape (0, m, k)
result = np.empty(shape=(0, m, k))
for element in elements:
result = np.append(result, [some_operation(element)], axis=0)
What's the efficient way to apply some, let's say, "operation" on numpy array of shape (n, m, k)? I guess there is a more "numpy" approach.
Many thanks.
I think map is the most appropriate function for this

Vstack of two arrays with same number of rows gives an error

I have a numpy array of shape (29, 10) and a list of 29 elements and I want to end up with an array of shape (29,11)
I am basically converting the list to a numpy array and trying to vstack, but it complain about dimensions not being the same.
Toy example
a = np.zeros((29,10))
a.shape
(29,10)
b = np.array(['A']*29)
b.shape
(29,)
np.vstack((a, b))
ValueError: all the input array dimensions except for the concatenation axis must match exactly
Dimensions do actually match, why am I getting this error and how can I solve it?
I think you are looking for np.hstack.
np.hstack((a, b.reshape(-1,1)))
Moreover b must be 2-dimensional, that's why I used a reshape.
The problem is that you want to append a 1D array to a 2D array.
Also, for the dimension you've given for b, you are probably looking for hstack.
Try this:
a = np.zeros((29,10))
a.shape
(29,10)
b = np.array(['A']*29)[:,None] #to ensure 2D structure
b.shape
(29,1)
np.hstack((a, b))
If you do want to vertically stack, you'd need this:
a = np.zeros((29,10))
a.shape
(29,10)
b = np.array(['A']*10)[None,:] #to ensure 2D structure
b.shape
(1,10)
np.vstack((a, b))

Create one-dimensional arrays of arrays in numpy

As part of my current project I am using a third party package that at one point expects an array of shape (n,) which contains n arrays of shape (m_n,2). This array is created from a list of arrays using np.array(). Most of the time the subarrays can be expected not to have the same shape across all of them. This results in the desired output. Sometimes, however all subarrays have the same shape. Then np.array() returns an output of shape (n,m_n,2). Is there a way to force numpy to give me an output of shape (n,)? Alternatively, I'd be very thankful for a way to directly create an array like this.
Below is an example of my problem.
import numpy as np
a = np.zeros((3,2))
b = np.zeros((4,2))
list1 = [a, a]
list2 = [a, b]
array1 = np.array(list1)
array2 = np.array(list2)
print(np.shape(array1))
print(np.shape(array2))

multiply multidimensional arrays in python

I have stored a number of 2d arrays in a 3d array and I need to multiply each one with a vector. so I have stored all those vectors in a 2d array. It's like this:
A = np.random.random((L, M, N))
B = np.random.random((L, M))
and I need to multiply each A[l] by B[l] which results in a Nx1 array and the output of the whole operation would be a LxN 2d array. Is there a function that can do this or do I need a loop?
An option is np.einsum
import numpy as np
output = np.einsum("ijk, ij -> ik", A, B)
This results in a (L, N) sized array containing matrix products of all the A[i].T.dot(B[i])

Categories