赞
踩
- # -*- coding: utf-8 -*-
- """
- Created on Fri Jun 12 16:20:17 2020
- @author: weiping
- """
-
- import xgboost as xgb
- import lightgbm as lgb
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import *
- from sklearn.datasets import load_iris
-
- iris = load_iris()
-
- data_x = iris.data
- data_y = iris.target
-
- x_tr,x_te,y_tr,y_te = train_test_split(data_x,data_y,train_size = 0.7,random_state =22)
- #XGBboost模型
- xgb_model = xgb.XGBClassifier()
- xgb_model.fit(x_tr,y_tr)
- xgb_predict = xgb_model.predict(x_te)
-
- print("xgb准确率:" ,str(xgb_model.score(x_te,y_te)))
- #print("roc_auc_score:",str(roc_auc_score(y_te,xgb_predict))) 不支持多分类
- print("precision_score:",str(precision_score(y_te,xgb_predict,average = 'weighted')))
- print("recall_score:" , str(recall_score(y_te,xgb_predict,average = 'weighted')))
- print("f1_score:",str(f1_score(y_te,xgb_predict,average = 'weighted')))
- '''
- xgb准确率: 0.9333333333333333
- precision_score: 0.9344662309368191
- recall_score: 0.9333333333333333
- f1_score: 0.9332681655262302
- '''
-
- #LGB模型
- '''
- 模型参数
- objective: (objective_type, app, application)
- 回归任务:
- 'regression'(默认)
- 'poisson'
- 'tweedie'
- 分类任务:
- 'binary':二分类
- 'multiclass':多分类
- boosting_type:(boosting_type, boost)
- 'gbdt':传统的梯度提升决策树 (默认)
- 'rf':随机森林
- 'dart':Dropouts meet Multiple Additive Regression Trees
- 'goss':Gradient-based One-Side Sampling 训练更快,可能欠拟合
- data:训练数据
- valid:验证数据集
- num_iteration:(num_trees, n_estimators)
- 迭代次数,100(默认)
- learning_rate:(eta, shrinkage_rate)
- 衰减因子,0.1(默认)
- seed:(random_state, random_seed)
- num_threads:(n_jobs, nthreads)
- num_leaves:
- 单棵树的最大叶子数,31(默认)
- 控制学习参数:
- 'max_depth':
- 树的最大深度,-1(默认),无限制
- 可用于控制过拟合
- 'lambda_l2':(reg_lambda, lambda)
- L2正则化,0(默认)
- 'lambda_l1':(reg_alpha)
- L1正则化,0(默认)
- 'min_data_in_leaf'(min_data, min_child_samples)
- 一个叶子的最小数据量,20(默认)
- 可用于控制过拟合
- 'subsample': (sub_row, bagging, bagging_fraction)
- 对样本进行采样,1(默认)
- 可用于控制过拟合
- 'sub_feature': (colsample_bytree, feature_fraction)
- 对特征进行采样,1(默认)
- 加速训练,控制过拟合
- early_stopping: (early_stopping_round)
- '''
-
- lgb_model = lgb.LGBMClassifier()
- lgb_model.fit(x_tr,y_tr)
- lgb_predict = lgb_model.predict(x_te)
-
-
- print("lgb准确率:" ,str(lgb_model.score(x_te,y_te)))
- #print("roc_auc_score:",str(roc_auc_score(y_te,xgb_predict))) 不支持多分类
- print("precision_score:",str(precision_score(y_te,lgb_predict,average = 'weighted')))
- print("recall_score:" , str(recall_score(y_te,lgb_predict,average = 'weighted')))
- print("f1_score:",str(f1_score(y_te,lgb_predict,average = 'weighted')))
- '''
- lgb准确率: 0.9555555555555556
- precision_score: 0.9555555555555556
- recall_score: 0.9555555555555556
- f1_score: 0.9555555555555556
- '''
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。