You can do this:
newrow = [1, 2, 3]
A = numpy.vstack([A, newrow])
Answer from jknair on Stack OverflowYou can do this:
newrow = [1, 2, 3]
A = numpy.vstack([A, newrow])
What is X? If it is a 2D-array, how can you then compare its row to a number: i < 3?
EDIT after OP's comment:
A = array([[0, 1, 2], [0, 2, 0]])
X = array([[0, 1, 2], [1, 2, 0], [2, 1, 2], [3, 2, 0]])
add to A all rows from X where the first element < 3:
import numpy as np
A = np.vstack((A, X[X[:,0] < 3]))
# returns:
array([[0, 1, 2],
[0, 2, 0],
[0, 1, 2],
[1, 2, 0],
[2, 1, 2]])
python - How to add a new row to an empty numpy array - Stack Overflow
python - Appending a new row to a numpy array - Stack Overflow
do you guys know how to add append an array in a 2d array?
Trying to add 1 to the element at a certain index of a numpy array
Videos
I have a numpy array img:
array([[4064, 4228, 2506, 1696, 2134, 3194],
[3996, 4296, 2506, 1670, 2066, 3194],
[3994, 4296, 2516, 1668, 2060, 3210],
...,
[4312, 4000, 1782, 588, 525, 425],
[4336, 3936, 1789, 489, 477, 413],
[4360, 3916, 1789, 443, 416, 413]])with shape (120560400, 6) . I'd like to append to each row six more values, each of these being a function of some of the six values that are already there, converting it into an array of shape (120560400, 12).
I've tried iterating through each row, calculating the values and using np.append(), though as expected this is taking far too long. Is there a quicker, more efficient way?
With pandas there's the much quicker option of just calculating a new column, rather than iterating through rows and I'm hoping there's something similar with numpy. Alternatively, should I look at converting it to a pandas dataframe, adding the columns and then turning it back into a numpy array?
TIA!
The way to "start" the array that you want is:
arr = np.empty((0,3), int)
Which is an empty array but it has the proper dimensionality.
>>> arr
array([], shape=(0, 3), dtype=int64)
Then be sure to append along axis 0:
arr = np.append(arr, np.array([[1,2,3]]), axis=0)
arr = np.append(arr, np.array([[4,5,6]]), axis=0)
But, @jonrsharpe is right. In fact, if you're going to be appending in a loop, it would be much faster to append to a list as in your first example, then convert to a numpy array at the end, since you're really not using numpy as intended during the loop:
In [210]: %%timeit
.....: l = []
.....: for i in xrange(1000):
.....: l.append([3*i+1,3*i+2,3*i+3])
.....: l = np.asarray(l)
.....:
1000 loops, best of 3: 1.18 ms per loop
In [211]: %%timeit
.....: a = np.empty((0,3), int)
.....: for i in xrange(1000):
.....: a = np.append(a, 3*i+np.array([[1,2,3]]), 0)
.....:
100 loops, best of 3: 18.5 ms per loop
In [214]: np.allclose(a, l)
Out[214]: True
The numpythonic way to do it depends on your application, but it would be more like:
In [220]: timeit n = np.arange(1,3001).reshape(1000,3)
100000 loops, best of 3: 5.93 ยตs per loop
In [221]: np.allclose(a, n)
Out[221]: True
Here is my solution:
arr = []
arr.append([1,2,3])
arr.append([4,5,6])
np_arr = np.array(arr)
The correct way to build an array incrementally is to not start with an array:
alist = []
alist.append([1, 2, 3])
alist.append([4, 5, 6])
arr = np.array(alist)
This is essentially the same as
arr = np.array([ [1,2,3], [4,5,6] ])
the most common way of making a small (or large) sample array.
Even if you have good reason to use some version of concatenate (hstack, vstack, etc), it is better to collect the components in a list, and perform the concatante once.
If you want [[1,2,3],[4,5,6]] I could present you an alternative without append: np.arange and then reshape it:
>>> import numpy as np
>>> np.arange(1,7).reshape(2, 3)
array([[1, 2, 3],
[4, 5, 6]])
Or create a big array and fill it manually (or in a loop):
>>> array = np.empty((2, 3), int)
>>> array[0] = [1,2,3]
>>> array[1] = [4,5,6]
>>> array
array([[1, 2, 3],
[4, 5, 6]])
A note on your examples:
In the second one you forgot to save the result, make it array = np.concatenate((array,newrow1), axis=0) and it works (not exactly like you want it but the array is not empty anymore). The first example seems badly indented and without know the variables and/or the problem there it's hard to debug.