预测模型:逻辑回归模型,一种常用的分类模型

我试图根据获胜党派进行预测。我选择的数据集中的列是候选党派和候选人的得票数。我的代码如下:

# 加载和清理数据集df4 = pd.read_csv('Election-Results-2018 - Parlimen_Results_By_Candidate.csv')df4['Votes for Candidate'] = df4['Votes for Candidate'].str.replace(',','').astype(float)df4['Total Votes Cast'] = df4['Total Votes Cast'].str.replace(',','').astype(float)df4['% of total Votes'] = df4['% of total Votes'].str.replace('%','').astype(float)# 步骤1 - 导入模型 from sklearn.linear_model import LogisticRegression# 步骤2 - 定义训练数据columns = ['Candidate Party', 'Votes for Candidate']# 步骤3 - 创建训练数据集X = df[columns]y = df['New Results']*

运行这些代码后,我收到了如下错误:

KeyError: "None of [Index(['Candidate Party', 'Votes for Candidate'], dtype='object')] are in the [columns]"

我是机器学习的初学者,希望能得到任何人的帮助和指导。谢谢


回答:

这是一个简单的错误,你使用了错误的名称 df 而不是 df4,这样应该可以工作:

df4 = pd.read_csv('Election-Results-2018 - Parlimen_Results_By_Candidate.csv')df4['Votes for Candidate'] = df4['Votes for Candidate'].str.replace(',','').astype(float)df4['Total Votes Cast'] = df4['Total Votes Cast'].str.replace(',','').astype(float)df4['% of total Votes'] = df4['% of total Votes'].str.replace('%','').astype(float)# 步骤1 - 导入模型 from sklearn.linear_model import LogisticRegression# 步骤2 - 定义训练数据columns = ['Candidate Party', 'Votes for Candidate']# 步骤3 - 创建训练数据集X = df4[columns]y = df4['New Results']

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注