I'm a real beginner with Python, and I have a recurrent problem with my ndarrays.
I'm very confused with the brackets (is there any schematic synthesis of the use of brackets in Python anywhere?). I always end up having arrays with many dimensions.
Right now I have this one:
>>> values
Out[1]:
array([[[ array([[ 4.23156519, -0.93539198],
[ 3.50074853, -1.67043386],
[ 4.64192393, -1.03918172],
[ 4.52056725, 0.2561267 ],
[ 3.36400016, 0.26435125],
[ 3.82025672, 1.16503286]])]]], dtype=object)
From here, how can I reduce the dimensions? I just wanted a 6x2 array. I tried np.reshape but since the current shape of values is (1,1,1) I can't directly reshape the array in a 6x2 one.
I'm sorry for the silly question, I'm seeking a general and schematic answer that would explain me how to pass from a higher dimension to a lower one and vice versa.
Here is the way I created the array. values is clustered_points
indices=[] # initialize indices
clustered_points=[] # initialize array containing points in different sub-arrays=clusters
for k in range(len(mu)):
a=r[:,k]
index=[t for t in range(len(a)) if a[t] == 1]
indices.append(index)
clustered_points.append(data[indices[k]])
clustered_points=np.reshape(clustered_points,(len(clustered_points),1,1))
To make an array that matches your initial display, I have to take special care to embed one array within another:
In [402]: x=np.array([[ 4.23156519, -0.93539198],
[ 3.50074853, -1.67043386],
[ 4.64192393, -1.03918172],
[ 4.52056725, 0.2561267 ],
[ 3.36400016, 0.26435125],
[ 3.82025672, 1.16503286]])
In [403]: a=array([[[None]]],dtype=object)
In [404]: a[0,0,0]=x
In [405]: a
Out[405]:
array([[[ array([[ 4.23156519, -0.93539198],
[ 3.50074853, -1.67043386],
[ 4.64192393, -1.03918172],
[ 4.52056725, 0.2561267 ],
[ 3.36400016, 0.26435125],
[ 3.82025672, 1.16503286]])]]], dtype=object)
In [406]: a.shape
Out[406]: (1, 1, 1)
In [407]: a[0,0,0].shape
Out[407]: (6, 2)
Simply doing a cut-n-paste from the display produces a different array with shape (1,1,1,6,2). That does not have the inner array marking. Either way a[0,0,0] gives the inner (6,2) array.
reshape and squeeze work on a (1,1,1,6,2) array, but not on a (6,2) nested inside a (1,1,1). You need to understand the difference.
(edit)
To run your 'how I did it' clip, I have to make some guesses as to the inputs (that almost merits a downvote).
I'll guess at some inputs:
In [420]: mu=np.arange(3); r=np.ones((4,3));data=np.ones(5)
In [421]: %paste
indices=[] # initialize indices
clustered_points=[] # initialize array containing points in different sub-arrays=clusters
for k in range(len(mu)):
a=r[:,k]
index=[t for t in range(len(a)) if a[t] == 1]
indices.append(index)
clustered_points.append(data[indices[k]])
## -- End pasted text --
In [422]: clustered_points
Out[422]:
[array([ 1., 1., 1., 1.]),
array([ 1., 1., 1., 1.]),
array([ 1., 1., 1., 1.])]
cluster_points is a list with several 1d arrays.
I can do
np.reshape(clustered_points,(12,1,1))
np.reshape(clustered_points,(3,4,1,1))
though it would be better, I think, to do np.array(clustered_points) first, and may be even check its shape.
Since
np.reshape(clustered_points,(len(clustered_points),1,1))
supposedly works then clustered_points must be a list of n single element arrays. But this reshape should produce a (n,1,1) array, not your (1,1,1,...) array.
So that edit doesn't help.
=========================
I'm seeking a general and schematic answer that would explain me how to pass from a higher dimension to a lower one and vice versa.
The first step is be clear, to yourself and others, what is the structure of your array. That includes knowing shape and dtype. And if the dtype is anything other than simple numerics, pay attention to the structure of the elements (e.g. objects within the array).
Singular dimensions (value 1) can be removed with indexing, [0], or squeeze. reshape also removes demensions (or adds them), but you have to pay attention to the total number of elements. If the old shape had 12 elements, the new has to have 12 as well. But reshape does not operate across dtype boundaries.
I think you're looking for numpy.squeeze:
#!/usr/bin/env python
import numpy
a = [[[[[ 4.23156519, -0.93539198],
[ 3.50074853, -1.67043386],
[ 4.64192393, -1.03918172],
[ 4.52056725, 0.2561267 ],
[ 3.36400016, 0.26435125],
[ 3.82025672, 1.16503286]]]]]
a = numpy.array(a)
print("a.shape=%s" % str(a.shape))
b = numpy.squeeze(a)
print("b.shape=%s" % str(b.shape))
gives
a.shape=(1, 1, 1, 6, 2)
b.shape=(6, 2)
The underlying issue is probably, how did you create this array?
A python list object cannot be manipulated in the same ways that np.ndarrays can be. In general, once your final list is created, you can cast it into a numpy array:
values = []
# fill values with values.append(...)
# ...
values = np.asarray(values)
To find the shape of an array, you can use either A.shape or np.shape(A). To remove dimensions of length one, the best approach is to use the squeeze method either as A.squeeze() or np.squeeze(A), i.e:
>>> values.squeeze()
array([[4.23156519, -0.93539198],
[3.50074853, -1.67043386],
[4.64192393, -1.03918172],
[4.52056725, 0.2561267],
[3.36400016, 0.26435125],
[3.82025672, 1.16503286]], dtype=object)
If your values array is really what you've said, then it should also be fine to use reshape
>>> values.reshape(6,2)
array([[4.23156519, -0.93539198],
[3.50074853, -1.67043386],
[4.64192393, -1.03918172],
[4.52056725, 0.2561267],
[3.36400016, 0.26435125],
[3.82025672, 1.16503286]], dtype=object)
If you're getting an error trying to reshape values, is it possible it is actually a list instead of an array?
If you want to create 6x2 array then just do this:
A = array([[4.23156519, -0.93539198],
[3.50074853, -1.67043386],
[4.64192393, -1.03918172],
[4.52056725, 0.2561267],
[3.36400016, 0.26435125],
[3.82025672, 1.16503286]], dtype=object)
If you want to reduce your array:
A = array([[[ array([[ 4.23156519, -0.93539198],
[ 3.50074853, -1.67043386],
[ 4.64192393, -1.03918172],
[ 4.52056725, 0.2561267 ],
[ 3.36400016, 0.26435125],
[ 3.82025672, 1.16503286]])]]], dtype=object)
it is actually 1x1x1x6x2 array, you can get 6x2 by doing A[0][0][0]
Related
I'm trying to take the exp of nonzero elements in a sparse theano variable. I have the current code:
A = T.matrix("Some matrix with many zeros")
A_sparse = theano.sparse.csc_from_dense(A)
I'm trying to do something that's equivalent to the following numpy syntax:
mask = (A_sparse != 0)
A_sparse[mask] = np.exp(A_sparse[mask])
but Theano doesn't support != masks yet. (And (A_sparse > 0) | (A_sparse < 0) doesn't seem to work either.)
How can I achieve this?
The support for sparse matrices in Theano is incomplete, so some things are tricky to achieve. You can use theano.sparse.structured_exp(A_sparse) in that particular case, but I try to answer your question more generally below.
Comparison
In Theano one would normally use the comparison operators described here: http://deeplearning.net/software/theano/library/tensor/basic.html
For example, instead of A != 0, one would write T.neq(A, 0). With sparse matrices one has to use the comparison operators in theano.sparse. Both operators have to be sparse matrices, and the result is also a sparse matrix:
mask = theano.sparse.neq(A_sparse, theano.sparse.sp_zeros_like(A_sparse))
Modifying a Subtensor
In order to modify part of a matrix, one can use theano.tensor.set_subtensor. With dense matrices this would work:
indices = mask.nonzero()
A = T.set_subtensor(A[indices], T.exp(A[indices]))
Notice that Theano doesn't have a separated boolean type—the mask is zeros and ones—so nonzero() has to be called first to take the indices of the nonzero elements. Furthermore, this is not implemented for sparse matrices.
Operating on Nonzero Sparse Elements
Theano provides sparse operations that are said to be structured and operate only on the nonzero elements. See:
http://deeplearning.net/software/theano/tutorial/sparse.html#structured-operation
More precisely, they operate on the data attribute of a sparse matrix, independent of the indices of the elements. Such operations are straightforward to implement. Note that the structured operations will operate on all the values in the data array, also those that are explicitly set to zero.
Here's a way of doing this with the scipy.sparse module. I don't know how theano implements its sparse. It's likely to be based on similar ideas (since it uses name like csc)
In [224]: A=sparse.csc_matrix([[1.,0,0,2,0],[0,0,3,0,0],[0,1,1,2,0]])
In [225]: A.A
Out[225]:
array([[ 1., 0., 0., 2., 0.],
[ 0., 0., 3., 0., 0.],
[ 0., 1., 1., 2., 0.]])
In [226]: A.data
Out[226]: array([ 1., 1., 3., 1., 2., 2.])
In [227]: A.data[:]=np.exp(A.data)
In [228]: A.A
Out[228]:
array([[ 2.71828183, 0. , 0. , 7.3890561 , 0. ],
[ 0. , 0. , 20.08553692, 0. , 0. ],
[ 0. , 2.71828183, 2.71828183, 7.3890561 , 0. ]])
The main attributes of the csc format at data, indices, indptr. It's possible that data has some 0 values if you fiddle with them after creation, but a freshly created matrix shouldn't.
The matrix also has a nonzero method modeled on the numpy one. In practice it converts the matrix to coo format, filters out any zero values, and returns the row and col attributes:
In [229]: A.nonzero()
Out[229]: (array([0, 0, 1, 2, 2, 2]), array([0, 3, 2, 1, 2, 3]))
And the csc format allows indexing just as a dense numpy array:
In [230]: A[A.nonzero()]
Out[230]:
matrix([[ 2.71828183, 7.3890561 , 20.08553692, 2.71828183,
2.71828183, 7.3890561 ]])
T.where works.
A_sparse = T.where(A_sparse == 0, 0, T.exp(A_sparse))
#Seppo Envari's answer seems faster though. So I'll accept his answer.
I'm looking for a way to assign a 1D numpy-array consisting of x elements to a 2D numpy Array of shape (y,z).
Example:
A=np.array([[0],[0],[0]])
A[2]=np.array([0,2])
Which should result in
A=[[0],[0],[0,2]]
This works perfectly fine using a python list, but has been causing me huge trouble when trying to do it in numpy, usually resulting in the error message:
could not broadcast input array from shape (z) into shape (x)
This seems to occur as a result of the fact that numpy copies everything instead of modifying the array in place. I have only recently begun using numpy and would really be grateful if someone could help find a way to do this efficiently.
Actually the issue is that Numpy refuses to perform implicit copies or reshapes. For instance:
>>> A=np.array([[0],[0],[0]])
>>> A[2]=np.array([0,2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (2) into shape (1)
Here A[2] is a subarray of A, of shape 1. 2 cells can't fit in 1, so we get shape error. The reverse situation is possible and known as broadcasting:
>>> A[0:2]=5
>>> A
array([[5],
[5],
[0]])
Here a single scalar has been broadcast to update the entire subarray. We can resize A to be able to fit the shape 2 entry:
>>> A.shape
(3, 1)
>>> A.resize((3,2))
>>> A.shape
(3, 2)
>>> A[2]=np.array([0,2])
>>> A
array([[5, 5],
[0, 0],
[0, 2]])
We can see that the resizing actually reorganized our cells. It still starts with 5 5 0 but the cells are no longer along a single column. This is because numpy doesn't copy unless asked to, either; all of our multicell slices in fact refer into the same original array. We can make a second matrix and copy the original into a single column there:
>>> B=np.zeros((A.shape[0]+1,A.shape[1]))
>>> B[:,0]=A.transpose()
>>> B
array([[ 5., 0.],
[ 5., 0.],
[ 0., 0.]])
The transpose is because the slice of B is 1-dimensional shape (3 long) rather than a 2-dimensional shape like A (which is 1 wide and 3 high). Numpy considers the 1-dimensional array to be a horisontal shape, so a 3 wide and 1 high matrix will fit. You could think of it like copying a range of cells in a spreadsheet.
Notably, the numbers thus placed in B are copies of what was in A. This is because we did a modification of B. Views can be used to manipulate sections of a matrix (including seeing it in another shape, like transpose() does), for instance:
>>> C=B[::-1,1]
>>> C
array([ 0., 0., 0.])
>>> C[:]=[1,2,3]
>>> B
array([[ 5., 3.],
[ 5., 2.],
[ 0., 1.]])
I was given code and I'm familiar with numpy, but this one line really has me stuck looking for an answer.
plt.contourf(lat,lev,T.mean(0).mean(-1),extend='both')
T is a 4 dimensional variable dependent on time, lat, lon, lev.
My question is, what does the T.mean(0).mean(-1) do?
Thanks!
The value passed to mean specifies the axis along which to take the mean. Therefore, T.mean(0) takes the mean along the 0th axis and returns a 3D array. The .mean(-1) then performs the mean along the last axis of the newly created 3D array, returning a 2D array.
Which is conveniently ideal for contourf.
It's the axis along which to take the mean.
>>> import numpy
>>> arr = numpy.array([[1,2,3,4],[5,6,7,8]])
>>> arr.mean(0) == [(1+5)/2, (2+6)/2, (3+7)/2, (4+8)/2]
array([ True, True, True, True], dtype=bool)
>>> arr.mean(1) == [(1+2+3+4)/4, (5+6+7+8)/4]
array([ True, True], dtype=bool)
Here are some examples, which I hope will explain what is going on:
In [191]:
#the data
a=np.random.random((3,3,3))
print a
[[[ 0.21715561 0.23384392 0.21248607]
[ 0.10788638 0.61387072 0.56579586]
[ 0.6027137 0.77929822 0.80993495]]
[[ 0.36330373 0.26790271 0.79011397]
[ 0.01571846 0.99187387 0.1301911 ]
[ 0.18856381 0.09577381 0.03728304]]
[[ 0.18849473 0.16550599 0.41999887]
[ 0.65009076 0.39260551 0.92284577]
[ 0.92642505 0.46513472 0.77273484]]]
In [192]:
#mean() returns the grand mean
a.mean()
Out[192]:
0.44176096869094533
In [193]:
#mean(0) returns the mean along the 1st axis
a.mean(0)
Out[193]:
array([[ 0.25631803, 0.22241754, 0.47419964],
[ 0.25789853, 0.6661167 , 0.53961091],
[ 0.57256752, 0.44673558, 0.53998427]])
In [195]:
#what is this doing?
a.mean(-1)
Out[195]:
array([[ 0.22116187, 0.42918432, 0.73064896],
[ 0.47377347, 0.37926114, 0.10720688],
[ 0.25799986, 0.65518068, 0.72143154]])
In [196]:
#it is returning the mean along the last axis, in this case, the 3rd axis
a.mean(2)
Out[196]:
array([[ 0.22116187, 0.42918432, 0.73064896],
[ 0.47377347, 0.37926114, 0.10720688],
[ 0.25799986, 0.65518068, 0.72143154]])
In [197]:
#Ok, this is now clear: calculate the mean along the 1st axis first, then calculate the mean along the last axis of the resultant.
a.mean(0).mean(-1)
Out[197]:
array([ 0.31764507, 0.48787538, 0.51976246])
IMO, using T as a variable name is probably not a good idea. .T() means transpose in numpy.
As part of broader program I am working on, I ended up with object arrays with strings, 3D coordinates and etc all mixed. I know object arrays might not be very favorite in comparison to structured arrays but I am hoping to get around this without changing a lot of codes.
Lets assume every row of my array obj_array (with N rows) has format of
Single entry/object of obj_array: ['NAME',[10.0,20.0,30.0],....]
Now, I am trying to load this object array and slice the 3D coordinate chunk. Up to here, everything works fine with simply asking lets say for .
obj_array[:,[1,2,3]]
However the result is also an object array and I will face problem as I want to form a 2D array of floats with:
size [N,3] of N rows and 3 entries of X,Y,Z coordinates
For now, I am looping over rows and assigning every row to a row of a destination 2D flot array to get around the problem. I am wondering if there is any better way with array conversion tools of numpy ? I tried a few things and could not get around it.
Centers = np.zeros([N,3])
for row in range(obj_array.shape[0]):
Centers[row,:] = obj_array[row,1]
Thanks
Nasty little problem... I have been fooling around with this toy example:
>>> arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
>>> arr
array([['one', [1, 2, 3]],
['two', [4, 5, 6]]], dtype=object)
My first guess was:
>>> np.array(arr[:, 1])
array([[1, 2, 3], [4, 5, 6]], dtype=object)
But that keeps the object dtype, so perhaps then:
>>> np.array(arr[:, 1], dtype=np.float)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
You can normally work around this doing the following:
>>> np.array(arr[:, 1], dtype=[('', np.float)]*3).view(np.float).reshape(-1, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected a readable buffer object
Not here though, which was kind of puzzling. Apparently it is the fact that the objects in your array are lists that throws this off, as replacing the lists with tuples works:
>>> np.array([tuple(j) for j in arr[:, 1]],
... dtype=[('', np.float)]*3).view(np.float).reshape(-1, 3)
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
Since there doesn't seem to be any entirely satisfactory solution, the easiest is probably to go with:
>>> np.array(list(arr[:, 1]), dtype=np.float)
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
Although that will not be very efficient, probably better to go with something like:
>>> np.fromiter((tuple(j) for j in arr[:, 1]), dtype=[('', np.float)]*3,
... count=len(arr)).view(np.float).reshape(-1, 3)
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
Based on Jaime's toy example I think you can do this very simply using np.vstack():
arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
float_arr = np.vstack(arr[:, 1]).astype(np.float)
This will work regardless of whether the 'numeric' elements in your object array are 1D numpy arrays, lists or tuples.
This works great working on your array arr to convert from an object to an array of floats. Number processing is extremely easy after. Thanks for that last post!!!! I just modified it to include any DataFrame size:
float_arr = np.vstack(arr[:, :]).astype(np.float)
This is way faster to just convert your object array to a NumPy float array:
arr=np.array(arr, dtype=[('O', np.float)]).astype(np.float) - from there no looping, index it just like you'd normally do on a NumPy array. You'd have to do it in chunks though with your different datatypes arr[:, 1], arr[:,2], etc. Had the same issue with a NumPy tuple object returned from a C++ DLL function - conversion for 17M elements takes <2s.
You may want to use structured array, so that when you need to access the names and the values independently you can easily do so. In this example, there are two data points:
x = zeros(2, dtype=[('name','S10'), ('value','f4',(3,))])
x[0][0]='item1'
x[1][0]='item2'
y1=x['name']
y2=x['value']
the result:
>>> y1
array(['item1', 'item2'],
dtype='|S10')
>>> y2
array([[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)
See more details: http://docs.scipy.org/doc/numpy/user/basics.rec.html
This problem usually happens when you have a dataset with different types, usually, dates in the first column or so.
What I use to do, is to store the date column in a different variable; and take the rest of the "X matrix of features" into X. So I have dates and X, for instance.
Then I apply the conversion to the X matrix as:
X = np.array(list(X[:,:]), dtype=np.float)
Hope to help!
For structured arrays use
structured_to_unstructured(arr).astype(np.float)
See: https://numpy.org/doc/stable/user/basics.rec.html#numpy.lib.recfunctions.structured_to_unstructured
np.array(list(arr), dtype=np.float) would work to convert all the elements in array to float at once.
Is there way to initialize a numpy array of a shape and add to it? I will explain what I need with a list example. If I want to create a list of objects generated in a loop, I can do:
a = []
for i in range(5):
a.append(i)
I want to do something similar with a numpy array. I know about vstack, concatenate etc. However, it seems these require two numpy arrays as inputs. What I need is:
big_array # Initially empty. This is where I don't know what to specify
for i in range(5):
array i of shape = (2,4) created.
add to big_array
The big_array should have a shape (10,4). How to do this?
EDIT:
I want to add the following clarification. I am aware that I can define big_array = numpy.zeros((10,4)) and then fill it up. However, this requires specifying the size of big_array in advance. I know the size in this case, but what if I do not? When we use the .append function for extending the list in python, we don't need to know its final size in advance. I am wondering if something similar exists for creating a bigger array from smaller arrays, starting with an empty array.
numpy.zeros
Return a new array of given shape and
type, filled with zeros.
or
numpy.ones
Return a new array of given shape and
type, filled with ones.
or
numpy.empty
Return a new array of given shape and
type, without initializing entries.
However, the mentality in which we construct an array by appending elements to a list is not much used in numpy, because it's less efficient (numpy datatypes are much closer to the underlying C arrays). Instead, you should preallocate the array to the size that you need it to be, and then fill in the rows. You can use numpy.append if you must, though.
The way I usually do that is by creating a regular list, then append my stuff into it, and finally transform the list to a numpy array as follows :
import numpy as np
big_array = [] # empty regular list
for i in range(5):
arr = i*np.ones((2,4)) # for instance
big_array.append(arr)
big_np_array = np.array(big_array) # transformed to a numpy array
of course your final object takes twice the space in the memory at the creation step, but appending on python list is very fast, and creation using np.array() also.
Introduced in numpy 1.8:
numpy.full
Return a new array of given shape and type, filled with fill_value.
Examples:
>>> import numpy as np
>>> np.full((2, 2), np.inf)
array([[ inf, inf],
[ inf, inf]])
>>> np.full((2, 2), 10)
array([[10, 10],
[10, 10]])
Array analogue for the python's
a = []
for i in range(5):
a.append(i)
is:
import numpy as np
a = np.empty((0))
for i in range(5):
a = np.append(a, i)
You do want to avoid explicit loops as much as possible when doing array computing, as that reduces the speed gain from that form of computing. There are multiple ways to initialize a numpy array. If you want it filled with zeros, do as katrielalex said:
big_array = numpy.zeros((10,4))
EDIT: What sort of sequence is it you're making? You should check out the different numpy functions that create arrays, like numpy.linspace(start, stop, size) (equally spaced number), or numpy.arange(start, stop, inc). Where possible, these functions will make arrays substantially faster than doing the same work in explicit loops
To initialize a numpy array with a specific matrix:
import numpy as np
mat = np.array([[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[1, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[1, 0, 1, 0, 1]])
print mat.shape
print mat
output:
(5, 5)
[[1 1 0 0 0]
[0 1 0 0 1]
[1 0 0 1 1]
[0 0 0 0 0]
[1 0 1 0 1]]
For your first array example use,
a = numpy.arange(5)
To initialize big_array, use
big_array = numpy.zeros((10,4))
This assumes you want to initialize with zeros, which is pretty typical, but there are many other ways to initialize an array in numpy.
Edit:
If you don't know the size of big_array in advance, it's generally best to first build a Python list using append, and when you have everything collected in the list, convert this list to a numpy array using numpy.array(mylist). The reason for this is that lists are meant to grow very efficiently and quickly, whereas numpy.concatenate would be very inefficient since numpy arrays don't change size easily. But once everything is collected in a list, and you know the final array size, a numpy array can be efficiently constructed.
numpy.fromiter() is what you are looking for:
big_array = numpy.fromiter(xrange(5), dtype="int")
It also works with generator expressions, e.g.:
big_array = numpy.fromiter( (i*(i+1)/2 for i in xrange(5)), dtype="int" )
If you know the length of the array in advance, you can specify it with an optional 'count' argument.
I realize that this is a bit late, but I did not notice any of the other answers mentioning indexing into the empty array:
big_array = numpy.empty(10, 4)
for i in range(5):
array_i = numpy.random.random(2, 4)
big_array[2 * i:2 * (i + 1), :] = array_i
This way, you preallocate the entire result array with numpy.empty and fill in the rows as you go using indexed assignment.
It is perfectly safe to preallocate with empty instead of zeros in the example you gave since you are guaranteeing that the entire array will be filled with the chunks you generate.
I'd suggest defining shape first.
Then iterate over it to insert values.
big_array= np.zeros(shape = ( 6, 2 ))
for it in range(6):
big_array[it] = (it,it) # For example
>>>big_array
array([[ 0., 0.],
[ 1., 1.],
[ 2., 2.],
[ 3., 3.],
[ 4., 4.],
[ 5., 5.]])
Whenever you are in the following situation:
a = []
for i in range(5):
a.append(i)
and you want something similar in numpy, several previous answers have pointed out ways to do it, but as #katrielalex pointed out these methods are not efficient. The efficient way to do this is to build a long list and then reshape it the way you want after you have a long list. For example, let's say I am reading some lines from a file and each row has a list of numbers and I want to build a numpy array of shape (number of lines read, length of vector in each row). Here is how I would do it more efficiently:
long_list = []
counter = 0
with open('filename', 'r') as f:
for row in f:
row_list = row.split()
long_list.extend(row_list)
counter++
# now we have a long list and we are ready to reshape
result = np.array(long_list).reshape(counter, len(row_list)) # desired numpy array
Maybe something like this will fit your needs..
import numpy as np
N = 5
res = []
for i in range(N):
res.append(np.cumsum(np.ones(shape=(2,4))))
res = np.array(res).reshape((10, 4))
print(res)
Which produces the following output
[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]]
If you want to add your item in multi-dimensional array, here is the solution.
import numpy as np
big_array = np.ndarray(shape=(0, 2, 4) # Empty with height and width 2, 4 and length 0
for i in range(5):
big_array = np.concatenate((big_array, i))
Here is the numpy official document for referral
# https://thispointer.com/create-an-empty-2d-numpy-array-matrix-and-append-rows-or-columns-in-python/
# Create an empty Numpy array with 4 columns or 0 rows
empty_array = np.empty((0, 4), int)
# Append a row to the 2D numpy array
empty_array = np.append(empty_array, np.array([[11, 21, 31, 41]]), axis=0)
# Append 2nd rows to the 2D Numpy array
empty_array = np.append(empty_array, np.array([[15, 25, 35, 45]]), axis=0)
print('2D Numpy array:')
print(empty_array)
pay attention that each inputed np.array is 2-dimensional