Issue
I have an empty list: x = []
.
I have a numpy array, y
, of shape: (180, 161)
. I can't necessarily define x
to be an np.empty
of a particular shape, because I won't know the shape of y
ahead of time.
I want to append y
to x
so that x
will have a .shape
of (1, 180, 161)
.
Then if I append more, I want it to be (n, 180, 161)
I tried .append
and .stack
, but I've had a variety of errors:
TypeError: only size-1 arrays can be converted to Python scalars
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 3 dimension(s) and the array at index 1 has 2 dimension(s)
And so on. It seems that this should be simple, but it's strangely difficult.
Solution
Assuming all items in x
have the same shape, you can first construct a list
and then construct the NumPy array from the list.
There, you have two options:
np.array()
which is faster but not flexiblenp.stack()
which is slower but allows you to choose over which axis should the stack happen (it is roughly equivalent tonp.array().transpose(...).copy()
The code would look like:
import numpy as np
n = 100
x = [np.random.randint(0, 10, (10, 20)) for _ in range(n)]
# same as: y = np.stack(x, 0)
y = np.array(x)
print(y.shape)
# (100, 10, 20)
Of course this line:
x = [np.random.randint(0, 10, (10, 20)) for _ in range(n)]
can be replaced with:
x = []
for _ in range(n):
x.append(np.random.randint(0, 10, (10, 20)))
You could also use np.append()
, e.g.:
def stacker(arrs):
result = arrs[0][None, ...]
for arr in arrs[1:]:
result = np.append(result, arr[None, ...], 0)
return result
but with horrific performances:
n = 1000
shape = (100, 100)
x = [np.random.randint(0, n, shape) for _ in range(n)]
%timeit np.array(x)
# 10 loops, best of 3: 21.1 ms per loop
%timeit np.stack(x)
# 10 loops, best of 3: 21.6 ms per loop
%timeit stacker(x)
# 1 loop, best of 3: 11 s per loop
and, as you can see, performance-wise, the list
-based method is way faster.
Answered By - norok2
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.