我想使用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]]
第六列是十进制类型。
当我更改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()
错误是:
回溯(最近通话最近一次):
文件“ E:/test_opencv/MyTest.py”,第32行,在 compute_parameters()
文件“ E:/test_opencv/MyTest.py”,第24行,位于calculate_parameters中 para_a = linregress(X,y)
文件“ E:\ Anaconda3 \ lib \ site-packages \ scipy \ stats_stats_mstats_common.py”,行linregress ssxm,ssxym,ssyxm,ssym = np.cov(x,y,bias = 1).flat
cov中的文件“ E:\ Anaconda3 \ lib \ site-packages \ numpy \ lib \ function_base.py”,第3085行 平均,w_sum =平均值(X,轴= 1,权重= w,返回=正确)
文件“ E:\ Anaconda3 \ lib \ site-packages \ numpy \ lib \ function_base.py”,平均行1163 如果scl.shape!= avg.shape:
AttributeError:“浮动”对象没有属性“ shape”
如何解决该错误?
答案 0 :(得分:2)
您可以通过将X
强制转换为浮点数来实现此目的:
para_a = linregress(X.astype(float), y)
>>> para_a
LinregressResult(slope=0.016460552337949654, intercept=133.10871357512195, rvalue=0.5513099358619372, pvalue=0.6282563718649847, stderr=0.024909849163985552)