Home > Software engineering >  Add in-between steps into array of numbers (Python)
Add in-between steps into array of numbers (Python)

Time:03-18

I am looking for some function that takes an input array of numbers and adds steps (range) between these numbers. I need to specify the length of the output's array.

Example:

input_array = [1, 2, 5, 4]
output_array = do_something(input_array, output_length=10)

Result:

output_array => [1, 1.3, 1.6, 2, 3, 4, 5, 4.6, 4.3, 4]
len(output_array) => 10

Is there something like that, in Numpy for example?

I have a prototype of this function that uses dividing input array into pairs ([0,2], [2,5], [5,8]) and filling "spaces" between with np.linspace() but it don't work well: enter image description here

CodePudding user response:

There is numpy.interp which might be what you are looking for.

import numpy as np

points = np.arange(4)
values = np.array([1,2,5,4])
x = np.linspace(0, 3, num=10)
np.interp(x, points, values)

output:

array([1.        , 1.33333333, 1.66666667, 2.        , 3.        ,
       4.        , 5.        , 4.66666667, 4.33333333, 4.        ])
  • Related