赞
踩
python 求线性回归的函数是 linregress(x,y) 函数;
slope是斜率,
intercept是截距,
r_value 是相关系数
from scipy import stats
import numpy as np
import pylab
x = np.array([1, 2, 5, 7, 10, 15])
y = np.array([2, 6, 7, 9, 14, 19])
slope, intercept, r_value, p_value, slope_std_error = stats.linregress(x, y)
predict_y = intercept + slope * x
pred_error = y - predict_y
degrees_of_freedom = len(x) - 2
residual_std_error = np.sqrt(np.sum(pred_error**2) / degrees_of_freedom)
# Plotting
pylab.plot(x, y, 'o')
pylab.plot(x, predict_y, 'k-')
pylab.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。