Prepend element to numpy array
Asked Answered
C

5

108

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. I've tried:

X = X.insert(X, 0)

how do I insert into X?

Camera answered 3/5, 2016 at 7:33 Comment(0)
T
133

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.

Taite answered 3/5, 2016 at 7:34 Comment(1)
More about axis=0 is here medium.com/@panjeh/…Weft
S
67

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.

Sterner answered 19/2, 2019 at 19:18 Comment(1)
Awesome! Not only the first option is faster, but can be easily used to insert an entire sequence ... for example, in my case, I wanted to insert several zeros at the begging; aa =np.concatenate(([0]*99, aa)) ... as I was manipulating a signal prior to smoothing, then finding the peaks.Gabbert
S
6

You can try the following

X = np.append(arr = np.array([[6]]), 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

Stentor answered 14/11, 2018 at 13:35 Comment(0)
V
1

I know this is a fairly old one, but a short solution is using numpy slicing tricks:

np.r_[[[6.]], X]

If you need to do it in a second dimension you can use np.c_.

I think this is the least cluttered version I can think of

Varicotomy answered 5/12, 2018 at 10:10 Comment(1)
BTW: you can append this way as well by simply reversing the order of course.Varicotomy
L
0

An addition to the comment of jbf81tb, as of 2023, all variants but the one with conversion to list, are more or less equal:

arr = np.random.rand(1000000)
%timeit np.concatenate(([-1],arr))
1.33 ms ± 23.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

arr = np.random.rand(1000000)
%timeit np.asarray([-1] + list(arr))
63.7 ms ± 691 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

arr = np.random.rand(1000000)
%timeit np.r_[-1,arr]
1.41 ms ± 35.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

arr = np.random.rand(1000000)
%timeit np.insert(arr,0,-1)
1.39 ms ± 24.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Larondalarosa answered 16/2, 2023 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.