【发布时间】:2018-05-08 17:33:41
【问题描述】:
我正在为一个班级制作一个项目,我正在尝试使用线性回归预测 nfl socre 游戏并预测来自 sklearn 的函数,当我想将训练数据拟合到 de fit 函数时,我的问题就出现了,这是我的代码:
onehotdata_x1 = pd.get_dummies(goal_model_data,columns=['team','opponent'])
# Crea el object de regression linear
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(onehotdata_x1[['home','team','opponent']], onehotdata_x1['goals'])
这是dataframe(goal_model_data)的结构:
team opponent goals home
NE KC 27 1
BUF NYJ 21 1
CHI ATL 17 1
CIN BAL 0 1
CLE PIT 18 1
DET ARI 35 1
HOU JAX 7 1
TEN OAK 16 1
这是我运行程序时遇到的错误:
Traceback (most recent call last):
File "predictnflgames.py", line 76, in <module>
regr.fit(onehotdata_x1[['home','team','opponent']], onehotdata_x1['goals'])
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2133, in __getitem__
return self._getitem_array(key)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2177, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1269, in _convert_to_indexer
.format(mask=objarr[mask]))
KeyError: "['team' 'opponent'] not in index"
【问题讨论】:
-
可以添加
onehotdata_x1.head()的输出 -
您正在尝试访问使用 pd 后不存在的列。 get_dummies。详情请看我的回答
标签: python pandas scikit-learn