Numpy - Add Row to Array

Numpy - add row to array

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]])

Appending a new row to a numpy array

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.

how to add rows to a numpy array

I believe the operation you are looking for is np.concatenate, which can construct a new array by concatenating two arrays.

Simple example, we can add a row of zeroes like this:

>>> np.concatenate((small_array, np.zeros((2,1,3))), axis=1)
array([[[3., 2., 7.],
[3., 2., 4.],
[0., 0., 0.]],

[[3., 2., 9.],
[3., 2., 7.],
[0., 0., 0.]]])

Now, instead of zeros, we can get the values from the first row in each matrix:

>>> np.concatenate((small_array, small_array[:,:1,:]), axis=1)
array([[[3., 2., 7.],
[3., 2., 4.],
[3., 2., 7.]],

[[3., 2., 9.],
[3., 2., 7.],
[3., 2., 9.]]])

At this point, you can modify the value in the third column of the new rows as needed.

The axis parameter is important here, it tells concatenate() along which axis I want to concatenate the two input arrays.

Documentation: https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html

Inserting a row at a specific location in a 2d array in numpy?

You are probably looking for numpy.insert

>>> import numpy as np
>>> a = np.zeros((2, 2))
>>> a
array([[ 0., 0.],
[ 0., 0.]])
# In the following line 1 is the index before which to insert, 0 is the axis.
>>> np.insert(a, 1, np.array((1, 1)), 0)
array([[ 0., 0.],
[ 1., 1.],
[ 0., 0.]])
>>> np.insert(a, 1, np.array((1, 1)), 1)
array([[ 0., 1., 0.],
[ 0., 1., 0.]])

How do I add rows and columns to a NUMPY array?

If you want zeroes in the added elements, my_array.resize((1600, 1000)) should work. Note that this differs from numpy.resize(my_array, (1600, 1000)), in which previous lines are duplicated, which is probably not what you want.

Otherwise (for instance if you want to avoid initializing elements to zero, which could be unnecessary), you can indeed use hstack and vstack to add an array containing the new elements; numpy.concatenate() (see pydoc numpy.concatenate) should work too (it is just more general, as far as I understand).

In either case, I would guess that a new memory block has to be allocated in order to extend the array, and that all these methods take about the same time.

How to add a new row to an empty numpy array

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

Is it possible in numpy array to add rows with different length and then add elements to that rows in python?

Start with the nested list:

In [99]: alist = [
...: ["Hi", "Anne"],
...: ["How", "are", "you"],
...: ["fine"]
...: ]
In [100]: alist
Out[100]: [['Hi', 'Anne'], ['How', 'are', 'you'], ['fine']]

Make an array from it:

In [101]: arr = np.array(alist)
<ipython-input-101-3fd8e9bd05a9>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
arr = np.array(alist)
In [102]: arr
Out[102]:
array([list(['Hi', 'Anne']), list(['How', 'are', 'you']), list(['fine'])],
dtype=object)

That warning tells us that we are doing something unusual, or at least suboptimal. We can suppress the warning with dtype=object, but the result will be the same.

Look at the result - it's a 3 element array, where it each element is a list. It's not a multidimensional array. We can make an array of arrays

In [103]: arr1 = np.array([np.array(el) for el in arr], object)
In [104]: arr1
Out[104]:
array([array(['Hi', 'Anne'], dtype='<U4'),
array(['How', 'are', 'you'], dtype='<U3'),
array(['fine'], dtype='<U4')], dtype=object)

Sounds like you want to duplicate this list constructor with arrays:

In [107]: al = []

     ...: for row in alist:
...: al1 = []
...: for el in row:
...: al1.append(el)
...: al.append(al1)
...:
In [108]: al
Out[108]: [['Hi', 'Anne'], ['How', 'are', 'you'], ['fine']]

But there are several problems.

There isn't a simple "empty" array; arrays can have a shape like (0,) or (0,3) or (3,0,3) etc.

Arrays don't have a simple and fast append. np.append does not qualify. Any attempt to "grow" an array results in making a new array with a full copy. List append just adds a pointer to an object that's designed to grow.

While numpy can make string dtype arrays (as in [104]), it does not have special string handling code. You must still use python string methods to manipulate those strings.

Math on object dtype arrays is hit-or-miss and slower than math on numeric arrays. Essentially it takes place at list-comprehension speeds.

numpy is designed for fast numerical work on multidimensional arrays. Think things like matrix multiplication, addition. Even when used as a stepping stone to machine learning the arrays need to be numeric and "rectangular". Ragged lists cannot be used for ML.

How to insert extra rows and columns at desired location in a matrix ( python )?

If we have the desired columns indices, it can be done by np.insert as:

desired_cols_ids = np.array([2, 4], dtype=np.int64)

zero_arr_row = np.zeros((1, a_obtained.shape[1]))
# [[0. 0. 0. 0.]]

a_obtained = np.insert(a_obtained, desired_cols_ids, zero_arr_row, axis=0)
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 0 0 0 0]
# [ 9 10 11 12]
# [13 14 15 16]
# [ 0 0 0 0]]

zero_arr_col = np.zeros((a_obtained.shape[0], 1))
# [[0.]
# [0.]
# [0.]
# [0.]
# [0.]
# [0.]]

a_obtained = np.insert(a_obtained, desired_cols_ids, zero_arr_col, axis=1)
# [[ 1 2 0 3 4 0]
# [ 5 6 0 7 8 0]
# [ 0 0 0 0 0 0]
# [ 9 10 0 11 12 0]
# [13 14 0 15 16 0]
# [ 0 0 0 0 0 0]]

For a little explanation, at first, we insert into the array rows (so it is modified) and then insert into the columns of the modified array.

Adding elements of each row to another array in Python

One option is to use numpy.where to check if a value in Result is 0 or not and add accordingly:

out = np.where(Result!=0, X[:, None] + Result, Result)

Output:

array([[ 0.        ,  7.53578696,  5.00926061,  4.20097937,  0.        ],
[-7.53578696, 0. , -2.52652635, 0. , 0. ],
[-5.00926061, 2.52652635, 0. , -0.80828124, 0. ],
[-4.20097937, 0. , 0.80828124, 0. , 0. ],
[ 0. , -2.46421304, -4.99073939, -5.79902063, 0. ]])

How to find a missing row between two similar 2d numpy arrays

Try this:

bb_missing = aa[~np.all(aa==bb[:, None], axis=2).any(axis=0)]

Output:

>>> bb_missing
array([[ 3, 4],
[48, 59]])


Related Topics



Leave a reply



Submit