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

复制代码
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))
复制代码
posted @   时光飞逝,逝者如斯  阅读(1526)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示