【问题标题】:Fit dataframe into linear regression sklearn将数据框拟合到线性回归 sklearn
【发布时间】: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


【解决方案1】:

问题是pd.get_dummies 之后没有teamopponent 列。

我在示例中使用 txt 格式的数据:https://ufile.io/e2vtv(与您的相同)。


试试这个看看:

import pandas as pd
from sklearn.linear_model import LinearRegression

goal_model_data = pd.read_table('goal_model_data.txt', delim_whitespace=True)

onehotdata_x1 = pd.get_dummies(goal_model_data,columns=['team','opponent'])

regr = LinearRegression()

#see the columns in onehotdata_x1
onehotdata_x1.columns

#see the data (only 2 rows of the data for the example)
onehotdata_x1.head(2)

结果:

Index([u'goals', u'home', u'team_BUF', u'team_CHI', u'team_CIN', u'team_CLE',
       u'team_DET', u'team_HOU', u'team_NE', u'team_TEN', u'opponent_ARI',
       u'opponent_ATL', u'opponent_BAL', u'opponent_JAX', u'opponent_KC',
       u'opponent_NYJ', u'opponent_OAK', u'opponent_PIT'],
       dtype='object')

goals  home  team_BUF  team_CHI  team_CIN  team_CLE  team_DET  team_HOU  \
0     27     1         0         0         0         0         0         0
1     21     1         1         0         0         0         0         0

team_NE  team_TEN  opponent_ARI  opponent_ATL  opponent_BAL  opponent_JAX  \
0        1         0             0             0             0             0
1        0         0             0             0             0             0

opponent_KC  opponent_NYJ  opponent_OAK  opponent_PIT
0            1             0             0             0
1            0             1             0             0

编辑 1

根据原始代码,您可能需要执行以下操作:

import pandas as pd
from sklearn.linear_model import LinearRegression

data = pd.read_table('data.txt', delim_whitespace=True)

onehotdata = pd.get_dummies(data,columns=['team','opponent'])

regr = LinearRegression()

#in x get all columns except goals column
x = onehotdata.loc[:, onehotdata.columns != 'goals']

#use goals column as target variable
y= onehotdata['goals']

regr.fit(x,y)
regr.predict(x)

希望这会有所帮助。

【讨论】:

  • 实际上pd.get_dummies() 只会删除teamopponent 列。 goalshome 将保留。您还可以看到 KeyError 仅针对 teamopponent 返回。
  • 是的,这就是我在示例中展示的内容。在pd.get_dummies 之后没有teamopponent 列。欢呼
  • 你应该更正你的第一句话,否则你说的地方。
  • 这不仅仅是一个愚蠢的错误。它代表着更大的东西。一个让你成为你自己的思维错误。 (开个玩笑,看看你写的个人资料:))
  • 那么我如何使用创建的假人?
【解决方案2】:

当您使用 pd.get_dummies(goal_model_data,columns=['team','opponent']) 时,teamopponent 列将从您的数据框中删除,onehotdata_x1 将不包含这两列。

然后,当您执行onehotdata_x1[['home','team','opponent']] 时,您会得到一个KeyError,因为teamopponentonehotdata_x1 数据框中不作为列存在。

使用玩具数据框,会发生以下情况:

【讨论】:

  • 附带说明,在拟合线性回归时,无论如何都不应该将 teamopponent 列用作自变量,因为它们是分类的,并且线性回归仅适用于兜帽。您应该使用创建的虚拟变量。
  • 但是如何使用创建的虚拟变量?
  • @xtrios 你应该打印onehotdata_x1 来了解这里发生了什么。与原始 goal_model_data 数据框相比,您会看到创建了几个新列。这些新列是虚拟变量。您要选择这些以及 home 变量作为特征,并使用 goals 列作为目标。
猜你喜欢
  • 2020-03-30
  • 2020-04-09
  • 2018-11-18
  • 2014-05-05
  • 2020-10-22
  • 2018-05-16
  • 2013-04-28
  • 2017-08-20
相关资源
最近更新 更多