4 python linear-regression scikit-learn
我想使用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()
Run Code Online (Sandbox Code Playgroud)
输出是:
拦截:133.10871357512195
X_Variable_1:0.016460552337949654
R_Square:0.3039426453800934
意义_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]]
Run Code Online (Sandbox Code Playgroud)
第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()
Run Code Online (Sandbox Code Playgroud)
错误是:
回溯(最近一次调用最后一次):
文件“E:/test_opencv/MyTest.py”,第 32 行,在calculate_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”,第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 = 平均值(X,轴=1,权重=w,返回=True)
文件“E:\Anaconda3\lib\site-packages\numpy\lib\function_base.py”,第 1163 行,平均如果 scl.shape != avg.shape:
属性错误:“浮动”对象没有属性“形状”
如何修复错误?
您可以通过简单地转换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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5634 次 |
| 最近记录: |