3

is there a fast way, to create a vector with 1 and x * 0 in python?

I would like to have something like

a = [1,0,0,0,0,0,0,0,0,...,0]
b = [1,1,0,0,0,0,0,0,0,...,0]

I tried it with list but see yourself :(

lst = [1, n*[0]]
lst = np.array(lst)
print(lst)
==> [1 list([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])]
1
  • 3
    Try to avoid naming your list list, by the way, because it will override the built-in name list Commented Nov 24, 2019 at 0:37

3 Answers 3

8

A proper NumPy solution:

import numpy as np

n = 10
arr = np.zeros(shape=n + 1, dtype=np.int64)
arr[0] = 1

Results in: [1 0 0 0 0 0 0 0 0 0 0]


Quick benchmarks

Here are the functions we're going to compare:

def func_1(n):
    return np.array([1, *n*[0]])

def func_2(n):
    arr = np.zeros(shape=n + 1, dtype=np.int64)
    arr[0] = 1
    return arr

def func_3(n):
    return np.array([1] + n * [0])

def func_4(n):
    return np.array([1] + [0 for _ in range(n)])

def func_5(n):
    return np.array([1].extend((0 for _ in range(n))))

def func_6(n):
    return np.array([1].extend([0 for _ in range(n)]))

def func_7(n):
    arr = [0 for _ in range(n)]
    arr[0] = 1
    return np.array(arr)

Results of timeit for arr_size = 100000000:

  • %timeit func_1(arr_size)

    1 loop, best of 3: 7.3 s per loop

  • %timeit func_2(arr_size)

    10 loops, best of 3: 177 ms per loop

  • %timeit func_3(arr_size)

    1 loop, best of 3: 7.26 s per loop

  • %timeit func_4(arr_size)

    1 loop, best of 3: 11.4 s per loop

  • %timeit func_5(arr_size)

    1 loop, best of 3: 6.3 s per loop

  • %timeit func_6(arr_size)

    1 loop, best of 3: 4.95 s per loop

  • %timeit func_7(arr_size)

    1 loop, best of 3: 10.6 s per loop

Sign up to request clarification or add additional context in comments.

Comments

7

For optimal performance, see the AMC's numpy answer below.


Use unpacking by simply adding an asterisk to your code: [1, *n*[0]] instead of [1, n*[0]]:

>>> arr = np.array([1, *n*[0]])

array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Comments

0

There is another NumPy solution with only slightly worse performance than the one posted by @AMC, but with the convenience that it is a single expression and doesn't need to be wrapped in a function to be used inline:

>>> n = 10
>>> np.eye(1, n + 1, 0, dtype=int)[0]
array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

It's also easy to create other one-hot vectors of the same length by changing the third argument:

>> np.eye(1, n + 1, 4, dtype=int)[0]
array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0])

Here's how the performance compares to @AMC's func_2 above (same arr_size = 100000000):

def func_8(n, k=0):
    return np.eye(1, n + 1, k, dtype=int)[0]
>>> %timeit func_2(arr_size)
16.4 µs ± 111 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>>> %timeit func_8(arr_size)
19 µs ± 113 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.