score:0
If interpolation for visualization is the goal, simply plotting the function does the trick. Numpy draws lines between data points, which is equivalent to linear interpolation.
However, if you want to work with the interpolated values you can use np.interp
for linear interpolation or some function from scipy.interpolate
for any type of interpolation. Finally, you can perform nearest neighbor interpolation by simply rounding the index.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 11) # [ 0. 0.4 0.8 1.2 1.6 2. 2.4 2.8 3.2 3.6 4. ]
y = np.array([0, 1, 2, 3, 3, 1, -1, -4, -5, -6, -7])
xi = np.linspace(0, 4, 1000)
# a) nearest neighbor rounding
dx = x[1] - x[0]
i = np.round(xi/dx).astype(dtype=int)
y1 = y[i]
# b) linear interpolation
y2 = np.interp(xi, x, y)
plt.plot(x, y, '.', label='original points')
plt.plot(xi, y1, label='rounding (nearest neighbor)')
plt.plot(xi, y2, label='linear interpolation')
plt.legend(loc='best')
plt.show()
score:0
The solution given as comment by @ali_m works well :
np.interp(1.2, x, y) # 3.0
np.interp(2.3, x, y) # -0.5
np.interp(2.4, x, y) # -1
Credit To: stackoverflow.com
Related Query
- Value of a function with numpy
- Built in function in numpy to interpret an Integer as a numpy array with index = integer value set
- The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() python numpy using ReLu function
- How can I change all value with same function in numpy array?
- Function object with numpy fields doesn't compute value when upon function call
- numpy division with RuntimeWarning: invalid value encountered in double_scalars
- Replace NaN's in NumPy array with closest non-NaN value
- replace zeroes in numpy array with the median value
- Function of Numpy Array with if-statement
- How to iterate 1d NumPy array with index and value
- How to use numpy with 'None' value in Python?
- Passing a structured numpy array with strings to a cython function
- How does one initialize a variable with tf.get_variable and a numpy value in TensorFlow?
- find max value of a list with numpy nan
- Truth value of numpy array with one falsey element seems to depend on dtype
- Cython inline function with numpy array as parameter
- NumPy ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
- numpy function to set elements of array to a value given a list of indices
- Finding index of maximum value in array with NumPy
- Create a numpy matrix with elements as a function of indices
- compute z-score with the function in scipy and numpy
- How to quickly fill a numpy array with values from separate calls to a function
- how to make a new numpy array same size as a given array and fill it with a scalar value
- Numpy get index of row with second-largest value
- Combining two numpy arrays to form an array with the largest value from each array
- Is there any numpy autocorrellation function with standardized output?
- Numpy find indices of groups with same value
- Comparing NumPy arange and custom range function for producing ranges with decimal increments
- Numpy custom Cumsum function with upper/lower limits?
- Default value when indexing outside of a numpy array, even with non-trivial indexing
More Query from same tag
- Convolving 2D matrices of which one has many more data points
- Selecting RGB image pixels from an image using a mask
- Inserting a mini-array into a larger array at intervals (not changing size)
- Multiple conditions on datatimeindex
- Looking for a sequential pattern with condition
- Python copy.copy() vs NumPy np.copy()
- fastest way to create a nxn matrix with numpy containing zeros
- Shuffling NumPy array along a given axis
- making histogram from a csv file
- How do I create a custom 4x4 array using NumPy?
- Two dimensional function not returning array of values?
- Load an audio file and find the frequency
- Matrix multiplication of a ndarray
- ValueError: operands could not be broadcast together with shapes (224,224) (180,180)
- How to fit line of best fit to curve without multipolyfit or is there an alternate method