多元线性回归-TypeError:fit()缺少1个必需的位置参数:“ y”

时间:2020-08-31 13:32:36

标签: python pandas dataframe scikit-learn

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'上有2个独立变量,在'y'上有1个因变量。

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

数据类型

  1. type(data_all)=熊猫DataFrame
  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_)

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的代码中

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 class,但是您正在使用实例方法.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)

来自an answer to a related question

由于y是实例方法,您会收到一个看似奇怪的错误,即缺少.fit,因此此函数的第一个参数实际上是self。在实例上调用.fit时,将自动传递self。如果在类(而不是实例)上调用.fit,则必须提供self