Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add to the end of a numpy array and remove from the beginning?

I have an array with shape: (1, 100), which is:

[[1. 2. 3. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 55.]]

I want to add something (say a number like 123) to the end and remove the first element, so that I will have:

[[2. 3. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 55. 123.]]

Which retains the original shape (1, 100)

I am trying:

x_pred = np.append(x_pred, next_index, axis=1)

(x_pred is a (1, 100) array and next_index is a scalar)

But I get an error:

ValueError: all the input arrays must have same number of dimensions

What am I doing wrong?

like image 330
Shamoon Avatar asked May 22 '19 13:05

Shamoon


People also ask

How do I add to the end of a NumPy array?

You can add a NumPy array element by using the append() method of the NumPy module. The values will be appended at the end of the array and a new ndarray will be returned with new and old values as shown above. The axis is an optional integer along which define how the array is going to be displayed.

How do I remove part of a NumPy array?

To remove an element from a NumPy array: Specify the index of the element to remove. Call the numpy. delete() function on the array for the given index.

How to append elements to the end of a NumPy array?

In python, numpy.append () is provided by Numpy module, by using which we can append elements to the end of a Numpy Array. arr : refers to the numpy array where the values will be added. values : refers to values that needs to be added.

How to remove elements from a NumPy array based on index?

How to remove elements from a numpy array? - Data Science Parichay How to remove elements from a numpy array? In this tutorial, we will look at how to remove elements from a numpy array based on their index with the help of simple examples. You can use the np.delete () function to remove specific elements from a numpy array based on their index.

How to add an element at end of an existing array?

We will pass the length of the array to add an element at end of an existing array. In this example, we have added an array of two elements at the end of an existing 1D array as well as added an array at end of the existing 2D numpy array by using np.insert () method along with the axis.

How to delete an element from an array in Python?

Pass the array and the index of the element that you want to delete. Here, we created a one-dimensional numpy array and then removed the element at index 2 (that is, the third element in the array, 4).


Video Answer


1 Answers

You can do this with roll.

a = np.zeros((1,10))

#roll and replace
a[0] = np.roll(a[0],-1)
a[0][-1] = new_value
like image 178
Christian Sloper Avatar answered Sep 28 '22 10:09

Christian Sloper