examples - python append array to array numpy
Prepend element to numpy array (2)
I have the following numpy array
import numpy as np X = np.array([[5.], [4.], [3.], [2.], [1.]])
I want to insert
[6.] at the beginning.
X = X.insert(X, 0)
how do I insert into X?
You can try the following
X = np.append(arr = np.array([]), values = X, axis= 0)
Instead of inserting 6 to the existing X, let append 6 by X.
So, first argument
arr is numpy array of scalar 6, second argument is your array to be added, and third is the place where we want to add
numpy has an
insert function that's accesible via
np.insert with documentation.
You'll want to use it in this case like so:
X = np.insert(X, 0, 6., axis=0)
the first argument
X specifies the object to be inserted into.
The second argument
0 specifies where.
The third argument
6. specifies what is to be inserted.
The fourth argument
axis=0 specifies that the insertion should happen at position
0 for every column. We could've chosen rows but your X is a columns vector, so I figured we'd stay consistent.