使用多个 pd 运行 MLR。数据帧:值错误:无法将字符串转换为浮点数:'Fnatic'



我正在尝试在LCS数据上运行MLR,以预测如果我要从LEC投入不同的团队进入LCS,他们会如何做。我正在使用sklearn运行回归。当我使用LEC团队数据运行模型时,我得到了一个错误。下面是代码:

import pandas as pd
import numpy as np
from sklearn import linear_model as sklm
lcsTeam  = pd.read_csv("LCS_2021_Summer_Team.csv")
#sets your x and y variables
x = lcsTeam[["GP","AGT",'KD','CKPM','GPR','FD%','DRG%','ELD%','FBN%','BN%','LNE%','JNG%','WPM','CWPM','WCPM']]
y = lcsTeam['W']
#next step is to make the regression and fit it to the new averaged data
regr = sklm.LinearRegression()
regr.fit(x,y)
#read in LEC data
LEC = pd.read_csv("LEC_2021_Summer_Team.csv")

#next step is to grab the team you want to test
LEC_teamnames = ['Team']
LEC_ = pd.DataFrame(LEC.loc[:, LEC_teamnames])
#LEC_.set_index('Team')
print(LEC_ , ' Choose from one of these teams')
team = (input())
#Use input team name to get the data to test in the LCS
LEC_team = LEC[LEC['Team'] == team]
#check to see if it works
print(LEC_team)
#use MLR
LECWins = regr.predict([LEC_team])
print(team + ' would have ' + LECWins + ' in the LCS this season!')

当我运行代码时,我得到错误ValueError:无法将字符串转换为float: 'Fnatic'。我哪里做错了?

如果您的x包含no_numeric因子变量,那么您必须通过一个热编码将它们更改为数值变量。你可以试试pandas.get_dummies

除此之外,如果你的x数据帧有所有的数字特征,那么你可以试试这个:

##  Assuming LEC data frame has same column names as in x    
LEC_team = LEC[LEC['Team'] == team][x.columns] 
LECWins = regr.predict([LEC_team]) 

最新更新