【sklearn】XGBoost应用

本文通过一个简单实例,展示如何在sklearn中应用XGBoost进行机器学习。首先,利用pandas加载数据,数据的最后一列作为标签。接着,将数据切分为训练集和测试集,初步评估XGBoost的基础性能。然后,采用GridSearchCV进行参数调优,以寻找最佳模型。最后,调用优化后的XGBoost模型进行预测。
摘要由CSDN通过智能技术生成

本文给个简单的例子,来看看sklearn是怎么用xgboost的。

1.先加载数据,这里用pandas,假设数据文件的最后一列是样本的标签。

from xgboost import XGBClassifier
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold

# load data from CSV file,
def loadData(fileFullPath):
    if (fileFullPath == None):
        print('file path is empty, please check path!')
    # use pandas read CSV file, return DataFrame
    dataSet = pd.read_csv(fileFullPath)
    pd.set_option('display.max_columns',20)
    # split features and labels
    featureNum = dataSet.shape[1]
    trainData = dataSet.iloc[:,0:featureNum-1]
    trainLabel = dataSet.iloc[:,-1]
    return trainData, trainLabel

样本集长下面这样:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值