多元线性回归 - 类型错误:fit() 缺少 1 个必需的位置参数:'y'


import pandas as pd
from sklearn.linear_model import LinearRegression as lm
x = data_all[combi_list[0][1:]]
y = data_all[combi_list[0][0]]
lm.fit(x, y)

我试图创建一个多元线性回归模型,其中有两个自变量"x"和一个因变量"y"。

似乎无法理解为什么这个错误TypeError: fit() missing 1 required positional argument: 'y'不断出现。

数据类型

  1. type(data_all(=熊猫数据帧
  2. type(combi_list(=包含列表的列表

我尝试过使用类似的代码。https://datatofish.com/multiple-linear-regression-python/

import pandas as pd
from sklearn import linear_model
import statsmodels.api as sm
Stock_Market = {'Year': [2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016],
'Month': [12, 11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'Interest_Rate': [2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75],
'Unemployment_Rate': [5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'Stock_Index_Price': [1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,949,884,866,876,822,704,719]        
}
df = pd.DataFrame(Stock_Market,columns=['Year','Month','Interest_Rate','Unemployment_Rate','Stock_Index_Price'])
X = df[['Interest_Rate','Unemployment_Rate']] # here we have 2 variables for multiple regression. If you just want to use one variable for simple linear regression, then use X = df['Interest_Rate'] for example.Alternatively, you may add additional variables within the brackets
Y = df['Stock_Index_Price']

# Correct up to here
# with sklearn
regr = linear_model.LinearRegression()
regr.fit(X, Y)
print('Intercept: n', regr.intercept_)
print('Coefficients: n', regr.coef_)

提前感谢!

在您的代码中

from sklearn.linear_model import LinearRegression as lm
x = data_all[combi_list[0][1:]]
y = data_all[combi_list[0][0]]
lm.fit(x, y)

您还没有实例化sklearn.linear_model.LinearRegression类,但是您正在使用实例方法.fit()。在调用.fit()之前,您需要实例化对象。

from sklearn.linear_model import LinearRegression
x = data_all[combi_list[0][1:]]
y = data_all[combi_list[0][0]]
lm = LinearRegression()
lm.fit(x, y)

从一个相关问题的答案:

您会得到一个看似奇怪的错误,即y丢失了,因为.fit是一个实例方法,所以该函数的第一个参数实际上是self。在实例上调用.fit时,会自动传递self。如果在类上调用.fit(与实例相反(,则必须提供self

最新更新