How to create equal intermediate values ​​between unequal spaced values ​​in Python?

I have an array A (variable) of the form:

A = [1, 3, 7, 9, 15, 20, 24]

Now I want to create 10 (variables) equally spaced values ​​between the values ​​of array A to get an array B of the form:

B = [1, 1.2, 1.4, ... 2.8, 3, 3.4, 3.8, ... , 6.6, 7, 7.2, ..., 23.6, 24]

In essence, B must always have values ​​of A and equally spaced values ​​between values ​​of A.

I solved this using code:

import numpy as np
A = np.array([1, 3, 7, 9, 15, 20, 24])
B = []
for i in range(len(A) - 1):
    B = np.append(B, np.linspace(A[i], A[i + 1], 11))
print (B)

But NumPy already has some kind of function or there are other best methods for creating such an array.

+4
source share
4 answers

An alternative method using interpolation instead of concatenation:

n = 10
x = np.arange(0, n * len(A), n)       # 0, 10, .., 50, 60
xx = np.arange((len(A) - 1) * n + 1)  # 0, 1, .., 59, 60
B = np.interp(xx, x, A)

Result:

In [31]: B
Out[31]: 
array([  1. ,   1.2,   1.4,   1.6,   1.8,   2. ,   2.2,   2.4,   2.6,
         2.8,   3. ,   3.4,   3.8,   4.2,   4.6,   5. ,   5.4,   5.8,
         6.2,   6.6,   7. ,   7.2,   7.4,   7.6,   7.8,   8. ,   8.2,
         8.4,   8.6,   8.8,   9. ,   9.6,  10.2,  10.8,  11.4,  12. ,
        12.6,  13.2,  13.8,  14.4,  15. ,  15.5,  16. ,  16.5,  17. ,
        17.5,  18. ,  18.5,  19. ,  19.5,  20. ,  20.4,  20.8,  21.2,
        21.6,  22. ,  22.4,  22.8,  23.2,  23.6,  24. ])

, , Python for-loop linspace. :

In [58]: timeit np.interp(np.arange((len(A) - 1) * 10 + 1), np.arange(0, 10*len(A), 10), A)
100000 loops, best of 3: 10.3 µs per loop

In [59]: timeit np.append(np.concatenate([np.linspace(i, j, 10, False) for i, j in zip(A, A[1:])]), A[-1])
10000 loops, best of 3: 94.2 µs per loop

In [60]: timeit np.unique(np.hstack(np.linspace(a, b, 10 + 1) for a, b in zip(A[:-1], A[1:])))
10000 loops, best of 3: 140 µs per loop
+8

zip np.concatenate , , np.append:

>>> np.append(np.concatenate([np.linspace(i, j, 10, False) for i,j in zip(A,A[1:])]),A[-1])
array([  1. ,   1.2,   1.4,   1.6,   1.8,   2. ,   2.2,   2.4,   2.6,
         2.8,   3. ,   3.4,   3.8,   4.2,   4.6,   5. ,   5.4,   5.8,
         6.2,   6.6,   7. ,   7.2,   7.4,   7.6,   7.8,   8. ,   8.2,
         8.4,   8.6,   8.8,   9. ,   9.6,  10.2,  10.8,  11.4,  12. ,
        12.6,  13.2,  13.8,  14.4,  15. ,  15.5,  16. ,  16.5,  17. ,
        17.5,  18. ,  18.5,  19. ,  19.5,  20. ,  20.4,  20.8,  21.2,
        21.6,  22. ,  22.4,  22.8,  23.2,  23.6,  24. ])

retstep=True (, ), - .

>>> np.concatenate([np.linspace(i, j, 10, False,retstep=True) for i,j in zip(A,A[1:])])
array([array([ 1. ,  1.2,  1.4,  1.6,  1.8,  2. ,  2.2,  2.4,  2.6,  2.8]),
       0.2,
       array([ 3. ,  3.4,  3.8,  4.2,  4.6,  5. ,  5.4,  5.8,  6.2,  6.6]),
       0.4,
       array([ 7. ,  7.2,  7.4,  7.6,  7.8,  8. ,  8.2,  8.4,  8.6,  8.8]),
       0.2,
       array([  9. ,   9.6,  10.2,  10.8,  11.4,  12. ,  12.6,  13.2,  13.8,  14.4]),
       0.6,
       array([ 15. ,  15.5,  16. ,  16.5,  17. ,  17.5,  18. ,  18.5,  19. ,  19.5]),
       0.5,
       array([ 20. ,  20.4,  20.8,  21.2,  21.6,  22. ,  22.4,  22.8,  23.2,  23.6]),
       0.4], dtype=object)
+3

:

print np.hstack(np.linspace(a, b, 10, endpoint=False) for a, b in zip(A[:-1], A[1:]))

:

[  1.    1.2   1.4   1.6   1.8   2.    2.2   2.4   2.6   2.8   3.    3.4
   3.8   4.2   4.6   5.    5.4   5.8   6.2   6.6   7.    7.2   7.4   7.6
   7.8   8.    8.2   8.4   8.6   8.8   9.    9.6  10.2  10.8  11.4  12.
  12.6  13.2  13.8  14.4  15.   15.5  16.   16.5  17.   17.5  18.   18.5
  19.   19.5  20.   20.4  20.8  21.2  21.6  22.   22.4  22.8  23.2  23.6]

endpoint , 9 10 .


Edit

24 , append Kasra , - ;) - endpoint 10 + 1 a b. 24 ( endpoint - true). ( : Bas Swinckels, unique ...)

print np.unique(np.hstack(np.linspace(a, b, 10 + 1) for a, b in zip(A[:-1], A[1:])))

[  1.    1.2   1.4   1.6   1.8   2.    2.2   2.4   2.6   2.8   3.
   3.4   3.8   4.2   4.6   5.    5.4   5.8   6.2   6.6   7.    7.2
   7.4   7.6   7.8   8.    8.2   8.4   8.6   8.8   9     9.6  10.2
  10.8  11.4  12.   12.6  13.2  13.8  14.4  15.   15.5  16.   16.5
  17.   17.5  18.   18.5  19.   19.5  20.   20.4  20.8  21.2  21.6
  22.   22.4  22.8  23.2  23.6  24. ]
+3

broadcasting matrix multiplication.

:

  • , 1 i.e. [0,1) steps.

  • A, 2D- .

  • , A .

-

out2D = (np.diff(A)[:,None]*np.arange(steps)/steps) + A[:-1,None]
out = np.append(out2D,A[-1])

, -, , , , other solution , . , , -

№1: A 100 steps = 10

In [42]: A = np.sort(np.random.randint(1,100000,(1,100))).ravel()

In [43]: steps = 10

In [44]: %timeit interp_based(A,steps)
100000 loops, best of 3: 18.3 µs per loop

In [45]: %timeit broadcasting_based(A,steps)
100000 loops, best of 3: 19.7 µs per loop

№2: A 500 steps = 10

In [46]: A = np.sort(np.random.randint(1,100000,(1,500))).ravel()

In [47]: steps = 10

In [48]: %timeit interp_based(A,steps)
10000 loops, best of 3: 101 µs per loop

In [49]: %timeit broadcasting_based(A,steps)
10000 loops, best of 3: 48.8 µs per loop

№3: A 1000 steps = 20

In [50]: A = np.sort(np.random.randint(1,100000,(1,1000))).ravel()

In [51]: steps = 20

In [52]: %timeit interp_based(A,steps)
1000 loops, best of 3: 345 µs per loop

In [53]: %timeit broadcasting_based(A,steps)
10000 loops, best of 3: 139 µs per loop
0

All Articles