I really like the Pandas Package and i am looking forward to get deeper into this great Python package.
Interpolation of Data Points between measured points using
Pandas for IO - Data Operation and Scipy Splines
In [1]:
import numpy as np
In [2]:
import scipy as sc
import matplotlib.pyplot as plt
In [3]:
from pandas import read_csv,DataFrame
In [4]:
test=read_csv('test.txt', sep='\t', index_col=False,usecols=[0,1])#we only take the first two columns to be save
In [5]:
test
Out[5]:
In [29]:
test['y']
Out[29]:
In [7]:
x=test['x']
y=test['y']
#x=np.arange(0,11,1)
#y=np.array([0,0.1,0.12,0.122,0.126,0.13,0.132,0.133,0.138,0.14,0.15])
In [8]:
x
Out[8]:
Plotting our data is not very smooth !
In [31]:
plt.scatter(x,y)
plt.plot(x,y)
plt.xlabel(test.columns.values[0]+' measured')
plt.ylabel(test.columns.values[1]+' measured')
plt.show()
Let us use Splines to get a smooth Curve around measured points
In [11]:
from scipy.interpolate import splrep, splev
inter= splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None,full_output=0, per=0, quiet=1)#spline with standard parameters
In [12]:
incr=0.1 #points between measured points
x_between=np.arange(x[0],x[10],incr)
In [13]:
x_between
Out[13]:
In [14]:
inter
Out[14]:
In [15]:
extr=splev(x_between, inter, der=0, ext=0)#'inter' retruns tree parameters/arrays we feed our Spline_evaluation function with this
In [33]:
plt.plot(x_between,extr,label='Spline')
plt.scatter(x,y,label='Data Points')
plt.xlabel(test.columns.values[0])
plt.ylabel(test.columns.values[1])
plt.legend(loc='best')
plt.show()
In [25]:
extr[:10]#extr holds the spline values - interpolation between x- points
Out[25]:
Now we want to write back our interpolated Data
In [19]:
interp_points=np.concatenate((x_between[None].T,extr[None].T),axis=1)
In [20]:
inter_col_names=test.columns.values.tolist()#making a list from column names
inter_col_names[0]=inter_col_names[0]+'_between' #an renaming them
inter_col_names[1]=inter_col_names[1]+'_interpolated'
In [21]:
inter_col_names
Out[21]:
In [22]:
DF=DataFrame(interp_points,columns=inter_col_names)#making a DataFrame of our new Data
In [36]:
DF[:5]#showing that now 10 points are added between every x-point
Out[36]:
In [24]:
DF.to_csv('test_interp.txt',index=False)#rewriting with tab delimiter and new column names
Keine Kommentare:
Kommentar veröffentlichen