【发布时间】:2019-04-11 12:17:46
【问题描述】:
我想用LinearRegression和linregress来计算Intercept,X_Variable_1,R_Square,Significance_F,就像Excel中的回归分析一样。
当我用这段代码做的时候,没有错误。
from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
from scipy.stats import linregress
from decimal import *
def calculate_parameters():
list_a=[['2018', '3', 'aa', 'aa', 93,1884.7746222667, 165.36153386251098], ['2018', '3', 'bb', 'bb', 62, 665.6392779848, 125.30386609565328], ['2018', '3', 'cc', 'cc', 89, 580.2259903521, 160.19280253775514]]
df = pd.DataFrame(list_a)
X = df.iloc[:, 5]
y = df.iloc[:, 6]
X1 = X.values.reshape(-1, 1)
y1 = y.values.reshape(-1, 1)
clf = LinearRegression()
clf.fit(X1, y1)
yhat = clf.predict(X1)
para_Intercept = clf.intercept_[0]
para_X_Variable_1 = clf.coef_[0][0]
SS_Residual = sum((y1 - yhat) ** 2)
SS_Total = sum((y1 - np.mean(y1)) ** 2)
para_R_Square = 1 - (float(SS_Residual)) / SS_Total
adjusted_r_squared = 1 - (1 - para_R_Square) * (len(y1) - 1) / (len(y1) - X1.shape[1] - 1)
para_a = linregress(X, y)
para_Significance_F = para_a[3]
print("Intercept:"+str(para_Intercept))
print("X_Variable_1:"+str(para_X_Variable_1))
print("R_Square:" + str(para_R_Square[0]))
print("Significance_F:" + str(para_Significance_F))
if __name__ == "__main__":
calculate_parameters()
输出是:
拦截:133.10871357512195
X_Variable_1:0.016460552337949654
R_Square:0.3039426453800934
Significance_F:0.6282563718649847
但其实list_a喜欢这个:
list_a = [['2018', '3', 'aa', 'aa', 93, Decimal('1884.7746222667'), 165.36153386251098],
['2018', '3', 'bb', 'bb', 62, Decimal('665.6392779848'), 125.30386609565328],
['2018', '3', 'cc', 'cc', 89, Decimal('580.2259903521'), 160.19280253775514]]
第6列是十进制类型。
当我更改list_a时,喜欢这个:
from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
from scipy.stats import linregress
from decimal import *
def calculate_parameters():
# list_a=[['2018', '3', 'aa', 'aa', 93,1884.7746222667, 165.36153386251098], ['2018', '3', 'bb', 'bb', 62, 665.6392779848, 125.30386609565328], ['2018', '3', 'cc', 'cc', 89, 580.2259903521, 160.19280253775514]]
list_a=[['2018', '3', 'aa', 'aa', 93,Decimal('1884.7746222667'), 165.36153386251098], ['2018', '3', 'bb', 'bb', 62, Decimal('665.6392779848'), 125.30386609565328], ['2018', '3', 'cc', 'cc', 89, Decimal('580.2259903521'), 160.19280253775514]]
df = pd.DataFrame(list_a)
X = df.iloc[:, 5]
y = df.iloc[:, 6]
X1 = X.values.reshape(-1, 1)
y1 = y.values.reshape(-1, 1)
clf = LinearRegression()
clf.fit(X1, y1)
yhat = clf.predict(X1)
para_Intercept = clf.intercept_[0]
para_X_Variable_1 = clf.coef_[0][0]
SS_Residual = sum((y1 - yhat) ** 2)
SS_Total = sum((y1 - np.mean(y1)) ** 2)
para_R_Square = 1 - (float(SS_Residual)) / SS_Total
adjusted_r_squared = 1 - (1 - para_R_Square) * (len(y1) - 1) / (len(y1) - X1.shape[1] - 1)
para_a = linregress(X, y)
para_Significance_F = para_a[3]
print("Intercept:"+str(para_Intercept))
print("X_Variable_1:"+str(para_X_Variable_1))
print("R_Square:" + str(para_R_Square[0]))
print("Significance_F:" + str(para_Significance_F))
if __name__ == "__main__":
calculate_parameters()
错误是:
Traceback(最近一次调用最后一次):
文件“E:/test_opencv/MyTest.py”,第 32 行,在 计算参数()
文件“E:/test_opencv/MyTest.py”,第 24 行,在 calculate_parameters para_a = linregress(X, y)
文件“E:\Anaconda3\lib\site-packages\scipy\stats_stats_mstats_common.py”,第 79 行,在 linregress ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat
文件“E:\Anaconda3\lib\site-packages\numpy\lib\function_base.py”,第 3085 行,在 cov avg, w_sum = average(X, axis=1, weights=w, returned=True)
文件“E:\Anaconda3\lib\site-packages\numpy\lib\function_base.py”,第 1163 行,平均 如果 scl.shape != avg.shape:
AttributeError: 'float' 对象没有属性 'shape'
如何修复错误?
【问题讨论】:
标签: python scikit-learn linear-regression