【发布时间】:2020-08-31 21:45:36
【问题描述】:
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)
我正在尝试创建一个多元线性回归模型,其中包含 2 个自变量“x”和 1 个因变量“y”。
似乎无法理解为什么这个错误TypeError: fit() missing 1 required positional argument: 'y' 不断出现。
数据类型
- type(data_all) = pandas DataFrame
- 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_)
提前致谢!
【问题讨论】:
-
此代码是否会导致该错误?在代码中,你有
X和y:regr.fit(X, Y),应该没问题 -
是的,我写的代码抛出了 TypeError。两个代码相似,我是否遗漏了什么导致代码失败?
-
试图运行它,对我来说很好@tom
标签: python pandas dataframe scikit-learn