2

I am looking to create a an array via numpy that generates an equally spaced values from interval to interval based on values in a given array.

I understand there is:

np.linspace(min, max, num_elements)

but what I am looking for is imagine you have a set of values:

arr = np.array([1, 2, 4, 6, 7, 8, 12, 10])

When I do:

 #some numpy function
arr = np.somefunction(arr, 16)

>>>arr
>>> array([1, 1.12, 2, 2.5, 4, 4.5, etc...)] 
# new array with 16 elements including all the numbers from previous 
# array with generated numbers to 'evenly space them out'

So I am looking for the same functionality as linspace() but takes all the elements in an array and creates another array with the desired elements but evenly spaced intervals from the set values in the array. I hope I am making myself clear on this..

What I am trying to actually do with this set up is take existing x,y data and expand the data to have more 'control points' in a sense so i can do calculations in the long run.

Thank you in advance.

3
  • 2
    Can you clarify why the desired output specifically has interpolated values of 1.12, 2.5, 4.5 etc? Are you looking to interpolate between values in an existing array, while also keeping the original values? In your example you're converting an array of length 8 to length 16, but what if you wanted to convert an array from length 8 to length 9? How would expect to preserve the original values in such a case? Commented May 8, 2016 at 3:31
  • Those are just made up values I decided to add.. Since I don't know how to do the actual calculation. They are just placeholders. Commented May 8, 2016 at 3:37
  • Ok, but can your question a bit more re. my other questions? Commented May 8, 2016 at 3:39

1 Answer 1

5
xp = np.arange(len(arr)) # X coordinates of arr
targets = np.arange(0, len(arr)-0.5, 0.5) # X coordinates desired
np.interp(targets, xp, arr)

The above does simple linear interpolation of 8 data points at 0.5 spacing for a total of 15 points (because of fenceposting):

array([  1. ,   1.5,   2. ,   3. ,   4. ,   5. ,   6. ,   6.5,   7. ,
         7.5,   8. ,  10. ,  12. ,  11. ,  10. ])

There are some additional options you can use in numpy.interp to tweak the behavior. You can also generate targets in different ways if you want.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.