前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ML学习笔记之XGBoost实现对鸢尾花数据集分类预测

ML学习笔记之XGBoost实现对鸢尾花数据集分类预测

作者头像
Jetpropelledsnake21
发布2019-12-24 14:38:21
8080
发布2019-12-24 14:38:21
举报
文章被收录于专栏:JetpropelledSnake
代码语言:javascript
复制
import xgboost as xgb
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
 
if __name__ == '__main__':
    iris_feature_E = "sepal lenght", "sepal width", "petal length", "petal width"
    iris_feature = "the length of sepal", "the width of sepal", "the length of petal", "the width of petal"
    iris_class = "Iris-setosa", "Iris-versicolor", "Iris-virginica"
    
    data = pd.read_csv("iris.data", header=None)
    iris_types = data[4].unique()
    for i, type in enumerate(iris_types):
        data.set_value(data[4] == type, 4, i)
    x, y = np.split(data.values, (4,), axis=1)
 
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.7, random_state=1)
 
    data_train = xgb.DMatrix(x_train, label=y_train)
    data_test = xgb.DMatrix(x_test, label=y_test)
    watchlist = [(data_test, 'eval'), (data_train, 'train')]
    param = {'max_depth':3, 'eta':1, 'silent':1, 'objective':'multi:softmax', 'num_class':3}
 
    bst = xgb.train(param, data_train, num_boost_round=10, evals=watchlist)
    y_hat = bst.predict(data_test)
    result = y_test.reshape(1, -1) == y_hat
    print('the accuracy:\t', float(np.sum(result)) / len(y_hat))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档