tile and repeat are handy functions when you want to repeat an array in various ways:
In [233]: np.tile(np.array([4,6,6,1]),(3,1))
Out[233]:
array([[4, 6, 6, 1],
[4, 6, 6, 1],
[4, 6, 6, 1]])
On the failure, note the docs for fill:
a.fill(value)
Fill the array with a scalar value.
np.array([4,6,6,1]) is not a scalar value. a was initialized as a 3 element float array.
It is possible to assign values to elements of an array, provided the shapes are right:
In [241]: a=np.empty(3)
In [242]: a[:]=np.array([1,2,3]) # 3 numbers into 3 slots
In [243]: a
Out[243]: array([ 1., 2., 3.])
In [244]: a=np.empty((3,4))
In [245]: a[:]=np.array([1,2,3,4]) # 4 numbers into 4 columns
In [246]: a
Out[246]:
array([[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.]])
This fill works with an object type array, but the result is quite different, and should be used with considerable caution:
In [247]: a=np.empty(3, object)
In [248]: a
Out[248]: array([None, None, None], dtype=object)
In [249]: a.fill(np.array([1,2,3,4]))
In [250]: a
Out[250]: array([array([1, 2, 3, 4]), array([1, 2, 3, 4]), array([1, 2, 3, 4])], dtype=object)
This (3,) array is not the same as the (3,4) array produced by other methods. Each element of the object array is a pointer to the same thing. Changing a value in one element of a changes that value in all the elements (because they are the same object).
In [251]: a[0][3]=5
In [252]: a
Out[252]: array([array([1, 2, 3, 5]), array([1, 2, 3, 5]), array([1, 2, 3, 5])], dtype=object)
Answer from hpaulj on Stack Overflowtile and repeat are handy functions when you want to repeat an array in various ways:
In [233]: np.tile(np.array([4,6,6,1]),(3,1))
Out[233]:
array([[4, 6, 6, 1],
[4, 6, 6, 1],
[4, 6, 6, 1]])
On the failure, note the docs for fill:
a.fill(value)
Fill the array with a scalar value.
np.array([4,6,6,1]) is not a scalar value. a was initialized as a 3 element float array.
It is possible to assign values to elements of an array, provided the shapes are right:
In [241]: a=np.empty(3)
In [242]: a[:]=np.array([1,2,3]) # 3 numbers into 3 slots
In [243]: a
Out[243]: array([ 1., 2., 3.])
In [244]: a=np.empty((3,4))
In [245]: a[:]=np.array([1,2,3,4]) # 4 numbers into 4 columns
In [246]: a
Out[246]:
array([[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.]])
This fill works with an object type array, but the result is quite different, and should be used with considerable caution:
In [247]: a=np.empty(3, object)
In [248]: a
Out[248]: array([None, None, None], dtype=object)
In [249]: a.fill(np.array([1,2,3,4]))
In [250]: a
Out[250]: array([array([1, 2, 3, 4]), array([1, 2, 3, 4]), array([1, 2, 3, 4])], dtype=object)
This (3,) array is not the same as the (3,4) array produced by other methods. Each element of the object array is a pointer to the same thing. Changing a value in one element of a changes that value in all the elements (because they are the same object).
In [251]: a[0][3]=5
In [252]: a
Out[252]: array([array([1, 2, 3, 5]), array([1, 2, 3, 5]), array([1, 2, 3, 5])], dtype=object)
Use broadcasting
vstack, tile, and repeat are all great and whatnot, but broadcasting can be several orders of magnitude faster...
import numpy as np
from time import time
t = time()
for _ in xrange(10000):
a = np.array([4,6,6,1])
b = np.vstack((a,)*100)
print time()-t
t = time()
for _ in xrange(10000):
a = np.array([4,6,6,1])
b = np.tile(a,(3,1))
print time()-t
t = time()
for _ in xrange(10000):
a = np.array([4,6,6,1])
b = np.empty([100,a.shape[0]])
b[:] = a
print time()-t
prints:
2.76399993896
0.140000104904
0.0490000247955
python - How to create or fill an numpy array with another array? - Stack Overflow
How to fill a matrix using numpy
Filling an array in python - Stack Overflow
python - NumPy array initialization (fill with identical values) - Stack Overflow
There are multiple ways to achieve this. One is to use np.full in np.full((2,2,3), a) as pointed out by Divakar in the comments. Alternatively, you can use np.tile for this, which allows you to construct an array by repeating an input array a given number of times. To construct your example you could do:
import numpy as np
np.tile(np.arange(1, 4), [2, 2, 1])
If your numpy version is >= 1.10 you can use broadcast_to
a = np.arange(1,4)
a.shape = (1,1,3)
b = np.broadcast_to(a,(2,2,3))
This produces a view rather than copying so will be quicker for large arrays. EDIT this looks to be the result you're asking for with your demo.
Hello everyone,
I haven't done any type of linear algebra work with python before, only matlab. In matlab you create a matrix with the proper dimensions, then start filling it up with vectors of the appropriate size. I've tried to look up how to do this in python using a loop, and it doesn't seem as straightforward. It seems you first need to create a list, and append that list. Then convert that list to a numpy array. And then from there it appears you stack them? But stacking seems to only stack 1D and 2D arrays, so it's not exactly what I'm looking for.
For example:
import numpy as np
matrix=np.zeros((rows,columns))
list=[]
for i in range(columns):
for x in range(rows):
list.append(x)
arr=np.array(list)
matrix.append(arr)
list.clear() So in the above case, I've made a list, and converted it into an array with shape (rows, 1). Then I want to add it to the first column of my matrix. Then through the loop, I start to fill my matrix up by filling it one by one with these vectors. However, the issue is I'm trying to add a 1D array, to a multi-D matrix (which numpy doesn't allow you to do).
Hpw do I fill up a matrix using numpy?
Using np.repeat and np.tile on x and y respectively to get the desired columns and then stacking them together with z into one array:
Use with AIimport numpy as np
x = [1, 2, 3]
y = [4, 5]
z = [6, 7, 8, 9, 10, 11]
a = np.array([np.repeat(x, 2), np.tile(y, 3), z]).T
result:
Use with AIarray([[ 1, 4, 6],
[ 1, 5, 7],
[ 2, 4, 8],
[ 2, 5, 9],
[ 3, 4, 10],
[ 3, 5, 11]])
For a pure Python solution, to work with any list size.
Use with AIx = [11,12,13,14,15]
y = [21,23,24]
z = [31,32,33,34,35,36,37]
This verbose code:
Use with AItmp = []
for i in range(max(len(x), len(y), len(z))):
for yy in y:
tmp.append([x[i%len(x)], yy])
for i in range(len(tmp)):
tmp[i].append((z[i%len(z)]))
Returns
Use with AI# [[11, 21, 31],
# [11, 23, 32],
# [11, 24, 33],
# [12, 21, 34],
# [12, 23, 35],
# [12, 24, 36],
# [13, 21, 37],
# [13, 23, 31],
# [13, 24, 32],
# [14, 21, 33],
# [14, 23, 34],
# [14, 24, 35],
# [15, 21, 36],
# [15, 23, 37],
# [15, 24, 31],
# [11, 21, 32],
# [11, 23, 33],
# [11, 24, 34],
# [12, 21, 35],
# [12, 23, 36],
# [12, 24, 37]]
You could also try:
In [79]: np.full(3, np.nan)
Out[79]: array([ nan, nan, nan])
The pertinent doc:
Definition: np.full(shape, fill_value, dtype=None, order='C')
Docstring:
Return a new array of given shape and type, filled with `fill_value`.
Although I think this might be only available in numpy 1.8+
np.fill modifies the array in-place, and returns None. Therefor, if you're assigning the result to a name, it gets a value of None.
An alternative is to use an expression which returns nan, e.g.:
a = np.empty(3) * np.nan