def corr_fit_plot(s, k):
"""
?????????lag=k?
???
"""
n = len(s)
x = []; y = []
for i in range(0,n-k):
x.append([s[i]])
y.append([s[i+k]])
plt.scatter(x,y)
# # using sklearn
# re = LinearRegression()
# re.fit(x,y)
# pred = re.predict(x)
# coef = re.coef_
# plt.plot(x,pred,'r-')
# least square by myself
x = np.array(x)
y = np.array(y)
one = np.ones((x.shape[0],1))
x = np.concatenate((one,np.array(x)),axis=1)
coefs = np.dot(pinv(x),y)
pred = coefs[0]*x[:,0] + coefs[1]*x[:,1]
coef = coefs[1]
plt.plot(x[:,1],pred,'r-')
plt.title('Corr=%s'%coef+' Lag=%s'%k)
plt.show()
return coef
#corr_fit_plot(s_power_consumption.values,1)
#corr_fit_plot(s_power_consumption.values,3)
#corr_fit_plot(s_power_consumption.values,5)
#corr_fit_plot(s_power_consumption.values,7)
评论列表
文章目录