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.
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.
I just wrote some code that does this operation ~100,000 times, so I needed to figure out the fastest way to do this. I'm not an expert in code efficiency by any means, but I could figure some things out by using the %%timeit magic function in a jupyter notebook.
My findings:
np.concatenate(([number],array))
requires the least time. Let's call it 1x time.
np.asarray([number] + list(array))
comes in at ~2x.
np.r_[number,array]
is ~4x.
np.insert(array,0,number)
appears to be the worst option here at 8x.
I have no idea how this changes with the size of array (I used a shape (15,) array) and most of the options I suggested only work if you want to put the number at the beginning. However, since that's what the question is asking about, I figure this is a good place to make these comparisons.
Simplest way:
a = np.array([1 + 2j, 5 + 7j])
a = np.insert(a, 0, 0)
Then:
>>> a
array([ 0.+0.j, 1.+2.j, 5.+7.j])
Note that this creates a new array, it does not actually insert the 0 into the original array.
There are several alternatives to np.insert, all of which also create a new array:
In [377]: a
Out[377]: array([ 1.+2.j, 5.+7.j])
In [378]: np.r_[0, a]
Out[378]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
In [379]: np.append(0, a)
Out[379]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
In [380]: np.concatenate([[0], a])
Out[380]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
In [381]: np.hstack([0, a])
Out[381]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
In [382]: np.insert(a, 0, 0)
Out[382]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
An alternative is "horizontal stack" (also creates a new array):
np.hstack((0,a))
Another way to do that would be to use numpy.concatenate . Example -
np.concatenate([[88],a,[77]])
Demo -
In [62]: a = np.array([2, 56, 4, 8, 564])
In [64]: np.concatenate([[88],a,[77]])
Out[64]: array([ 88, 2, 56, 4, 8, 564, 77])
You can pass the list of indices to np.insert :
>>> np.insert(a,[0,5],[88,77])
array([ 88, 2, 56, 4, 8, 564, 77])
Or if you don't know the length of your array you can use array.size to specify the end of array :
>>> np.insert(a,[0,a.size],[88,77])
array([ 88, 2, 56, 4, 8, 564, 77])
append() creates a new array which can be the old array with the appended element.
I think it's more normal to use the proper method for adding an element:
a = numpy.append(a, a[0])
When appending only once or once every now and again, using np.append on your array should be fine. The drawback of this approach is that memory is allocated for a completely new array every time it is called. When growing an array for a significant amount of samples it would be better to either pre-allocate the array (if the total size is known) or to append to a list and convert to an array afterward.
Using np.append:
b = np.array([0])
for k in range(int(10e4)):
b = np.append(b, k)
1.2 s ± 16.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Using python list converting to array afterward:
d = [0]
for k in range(int(10e4)):
d.append(k)
f = np.array(d)
13.5 ms ± 277 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Pre-allocating numpy array:
e = np.zeros((n,))
for k in range(n):
e[k] = k
9.92 ms ± 752 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
When the final size is unkown pre-allocating is difficult, I tried pre-allocating in chunks of 50 but it did not come close to using a list.
85.1 ms ± 561 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)