我有一个这样的数据框:
import pandas as pd
import numpy as np
time = pd.date_range('2018-05-14 00:00:00','2018-05-14 01:00:00',freq='5T')
mile = np.linspace(0,100,10)
x = list(time)*len(mile)
y = np.repeat(mile,len(time))
z = []
for i in range(0,10,1):
z.extend(np.random.normal(loc=i*5, scale=5, size=13))
origin_data = pd.DataFrame({'x':x, 'y':y ,'z':z})
origin_data
包含原始点'位置(x和y)及其值(z)。我想使用双线性插值在{strong>新位置 z
和[{1}}插入x = pd.date_range('2018-05-14 00:00:00','2018-05-14 01:00:00',freq='1T')
值。
我了解了有关y = np.linspace(0,91,1)
的官方文件。但它的x类型是数字,我的是日期时间。此外,教程的scipy.interpolate.interp2d
值是在已经给出的情况下计算的,因此我不知道如何处理输入z
值的顺序。有人能给我一个例子,其中包含基于我上面提供的数据帧的插值结果图吗?谢谢你的关注!
答案 0 :(得分:0)
这是我发现这个问题的方式:
import pandas as pd
import numpy as np
from scipy import interpolate
import itertools
time = pd.date_range('2018-05-14 00:00:00','2018-05-14 01:00:00',freq='5T')
mile = np.arange(0,100,10)
x = list(time)*len(mile)
y = np.repeat(mile,len(time))
z = []
for i in range(0,10,1):
z.extend(np.random.normal(loc=i*5, scale=5, size=13))
origin_data = pd.DataFrame({'x':x, 'y':y ,'z':z})
from ggplot import *
ggplot(aes(x = 'x', y = 'y', colour = 'z'), data = origin_data) +\
geom_point(size = 100) +\
scale_x_date(labels = date_format("%Y-%m-%d %H:%M:S"))
x_numeric = [x.timestamp() for x in origin_data['x']]
x_cors = pd.unique(x_numeric)
y_cors = pd.unique(origin_data['y'])
cors = list(itertools.product(x_cors,y_cors))
interp_func = interpolate.LinearNDInterpolator(cors, z)
interp_func = interpolate.CloughTocher2DInterpolator(cors, z)
new_x = [x.timestamp() for x in pd.date_range('2018-05-14 00:00:00','2018-05-14 01:00:00',freq='1T')]
new_y = np.arange(0,91,1)
new_cors = list(itertools.product(new_x,new_y))
new_z = interp_func(new_cors)
new_data = pd.DataFrame({'x':[x[0] for x in new_cors],
'y':[x[1] for x in new_cors],
'z':new_z})
import datetime
new_data['x'] = [pd.Timestamp(x,unit = 's') for x in new_data['x']]
ggplot(aes(x='x',y='y',colour='z'),data=new_data) +\
geom_point(size=100) +\
scale_x_date(labels = date_format("%Y-%m-%d %H:%M:S"))