【问题标题】:adaBoost voting data and target form in pythonpython中的adaBoost投票数据和目标表单
【发布时间】:2020-09-13 03:24:18
【问题描述】:

我正在尝试测试投票 adaBoost 分类器的这种实现。

我的数据集有 650 个三元组 G1、G2、G3 的形式,其中 G1 和 G2 包含在 [1-20] 中,G3 基于 G1 和 G2 为 1 或 0。

从我读过的内容来看,cross_val_score 自己将输入数据拆分为训练组和测试组,但我做错了 X,y 初始化。如果我尝试使用整个数据集初始化 X 和 y,则准确度为 100%,这似乎有点偏离。

我尝试只将 G3 值放在 y 中,但我得到了相同的结果。

通常我将数据分成训练集和测试集,这会让事情变得更容易。

我在 python 或机器学习方面没有太多经验,但我决定试一试。

您能否解释一下 X 和 y 初始化应该是什么样子才能正常工作?

import os
import subprocess
import pandas as pd
import numpy as np

from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import VotingClassifier

input_file = "student_data_grades_only.csv"

data = pd.read_csv(input_file, header = 0)

X, y = data, data['G3']
print(X,y)

clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(n_estimators=50, random_state=1)
clf3 = GaussianNB()

eclf = VotingClassifier(
    estimators=[('lr', clf1), ('rf', clf2), ('gnb', clf3)],
    voting='hard')

for clf, label in zip([clf1, clf2, clf3, eclf], ['Logistic Regression', 'Random Forest', 'naive Bayes', 'Ensemble']):
    scores = cross_val_score(clf, X, y, scoring='accuracy', cv=2)
    print("Accuracy: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))

【问题讨论】:

  • 嗨,您已将标签包含在 X 中:您必须从训练集“X”中排除“G3”列。希望这会有所帮助,问候。

标签: python machine-learning classification adaboost


【解决方案1】:

您应该从 X 变量中删除 G3 列,因为这是您要预测的内容。

X,y = data.drop('G3'), data['G3']

这段代码可以工作,应该可以帮助你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    相关资源
    最近更新 更多