在数据中经常会有遗失或者dirty的数据,这里介绍一种插值的方法:
假如我的数据中 值为 -1 的数字是需要填补的数字;
这里使用的是numpy的 interp(x,x_p,y_p) 方法;
talk is cheapppppppppppppppp,
show me the codeeeeeeeeeeeeeeeeeee!
import numpy as np import matplotlib.pyplot as plt temp = np.array([-1,3,4,6,8,10,-1,15,-1,20,80-1,90,-1]) x_p = np.where(temp != -1)[0] y_p = temp[x_p] x = np.where(temp == -1)[0] result = np.interp(x,x_p,y_p) print(temp) temp[x] = result print(temp) plt.plot(x_p,y_p,color = 'g') plt.scatter(x,result,color = 'r') plt.show()
运行结果:
绘制图:
图中红点就是插值得到的数据!
写完很晚了,有问题可随时联系!



