Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linear interpolation between two data points

I have two data points x and y:

  x = 5 (value corresponding to 95%)
  y = 17 (value corresponding to 102.5%)

No I would like to calculate the value for xi which should correspond to 100%.

 x = 5 (value corresponding to 95%)
 xi = ?? (value corresponding to 100%)
 y = 17 (value corresponding to 102.5%)

How should I do this using python?

like image 908
Al_Iskander Avatar asked Jul 09 '16 14:07

Al_Iskander


2 Answers

is that what you want?

In [145]: s = pd.Series([5, np.nan, 17], index=[95, 100, 102.5])

In [146]: s
Out[146]:
95.0      5.0
100.0     NaN
102.5    17.0
dtype: float64

In [147]: s.interpolate(method='index')
Out[147]:
95.0      5.0
100.0    13.0
102.5    17.0
dtype: float64
like image 200
MaxU - stop WAR against UA Avatar answered Oct 02 '22 20:10

MaxU - stop WAR against UA


You can use numpy.interp function to interpolate a value

import numpy as np
import matplotlib.pyplot as plt

x = [95, 102.5]
y = [5, 17]

x_new = 100

y_new = np.interp(x_new, x, y)
print(y_new)
# 13.0

plt.plot(x, y, "og-", x_new, y_new, "or");

enter image description here

like image 25
Vlad Bezden Avatar answered Oct 02 '22 18:10

Vlad Bezden