I have the below array
and would like to repeat
each array
n
times.
x_array
[array([14.91488012, 1.2986064 , 4.98965322]),
array([2.39389187e 02, 1.04442059e-01, 3.06391338e-01]),
array([ 48.19437348, 201.09951372, 0.35223001]),
array([ 19.96978171, 367.52578786, 0.68676553]),
array([0.55120466, 0.27133609, 0.75646697]),
array([8.21287360e 02, 1.76495077e 02, 4.87263691e-01]),
array([184.03439377, 1.24823107, 5.33109884]),
array([575.59800297, 186.4650814 , 2.21028258]),
array([0.50308552, 3.09976082, 0.10537899]),
array([1.02259912e 00, 1.52282513e 02, 1.15085308e-01])]
I've tried np.repeat(x_array, 2)
but this doesn't preserve the order of the matrix
/array
. I've also tried x_array*2
, but this seems to just put the new array at the bottom. I was hopping to repeat x_array[0]
n
times and do the same for the next set of arrays
, so that I have n
total of each in order.
Thanks in advance.
CodePudding user response:
Building off of the last example from https://numpy.org/doc/stable/reference/generated/numpy.repeat.html,
x_array = np.array(x_array) # Or a similiar operation to convert x_array to an ndarray vs. a list of arrays.
expanded_x_array = np.repeat(x_array, n, axis=0)
print(expanded_x_array)
should produce what you are looking for.
CodePudding user response:
You just need to specify the axis:
>>> np.repeat(x_array, 2, axis=0)
array([[1.49149e 01, 1.29861e 00, 4.98965e 00],
[1.49149e 01, 1.29861e 00, 4.98965e 00],
[2.39389e 02, 1.04442e-01, 3.06391e-01],
[2.39389e 02, 1.04442e-01, 3.06391e-01],
...,
[5.03086e-01, 3.09976e 00, 1.05379e-01],
[5.03086e-01, 3.09976e 00, 1.05379e-01],
[1.02260e 00, 1.52283e 02, 1.15085e-01],
[1.02260e 00, 1.52283e 02, 1.15085e-01]])
From the docs:
numpy.repeat(a, repeats, axis=None)
...
axis int, optional
The axis along which to repeat values. By default, use the flattened input array, and return a flat output array.
(added bold)
CodePudding user response:
You could use a list comprehension:
n = 2
repeated_list = [row for row in a for _ in range(n)]
print(repeated_list)
CodePudding user response:
Your terminology is confusing. You say it's an "array", but the display looks more like a list, And the fact that x_array*2
puts an "new array" at the bottom confirms that - that's a list use of *
.
np.repeat(x_array)
first makes an array (a real one!)
np.array(x_array)
is a (n,3) float dtype array. Without axis
np.repeat
flattens - as documented!
Specifying the axis=0
works because it's repeating on that first n
dimension. The result is a (2*n,3) float dtype array (not a list).
It is possible to make a 1d object dtype array containing those arrays. With that repeat
will work without the axis parameter.
Knowing what you have, and describing it accurately, can make this kind of task much easier - and the questions clearer.
illustration
Make a list of arrays:
In [21]: alist = [np.ones(3,int),np.zeros(3,int),np.arange(3)]
In [22]: alist
Out[22]: [array([1, 1, 1]), array([0, 0, 0]), array([0, 1, 2])]
List repeat:
In [23]: alist*2
Out[23]:
[array([1, 1, 1]),
array([0, 0, 0]),
array([0, 1, 2]),
array([1, 1, 1]),
array([0, 0, 0]),
array([0, 1, 2])]
Make a 2d array from the list:
In [24]: np.array(alist)
Out[24]:
array([[1, 1, 1],
[0, 0, 0],
[0, 1, 2]])
repeat without axis repeats elements in a flattened way:
In [25]: np.repeat(alist,2)
Out[25]: array([1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2])
repeat this 2d array on 0 axis:
In [26]: np.repeat(alist,2,axis=0)
Out[26]:
array([[1, 1, 1],
[1, 1, 1],
[0, 0, 0],
[0, 0, 0],
[0, 1, 2],
[0, 1, 2]])
Object dtype array from list:
In [27]: arr = np.empty(3,object); arr[:]=alist
In [28]: arr
Out[28]: array([array([1, 1, 1]), array([0, 0, 0]), array([0, 1, 2])], dtype=object)
Since the arrays have the same size we have to use this special construct. Otherwise we get the 2d array [24].
This array has a repeat
method, and with only one dimension we dont need to specify the axis. It's repeating the object elements, arrays, not the numbers in the 2d [24] array.
In [29]: arr.repeat(2)
Out[29]:
array([array([1, 1, 1]), array([1, 1, 1]), array([0, 0, 0]),
array([0, 0, 0]), array([0, 1, 2]), array([0, 1, 2])], dtype=object)