LightGBM使用
https://bacterous.github.io/2018/09/13/LightGBM%E4%BD%BF%E7%94%A8/
深入理解LightGBM
https://zhuanlan.zhihu.com/p/99069186
https://github.com/Microstrong0305/WeChat-zhihu-csdnblog-code
https://github.com/microsoft/LightGBM
https://www.csdn.net/tags/MtTaEg5sMjE2MjE0LWJsb2cO0O0O.html
利用XGBoost实现对鸢尾花数据集(Iris.csv)的分类预测
https://blog.csdn.net/Cyril_KI/article/details/107660210
python将大csv文件划分成小csv文件做训练集和测试集
https://blog.csdn.net/Findingxu/article/details/86683743?utm_term=csv%E6%96%87%E4%BB%B6%E5%88%86%E8%AE%AD%E7%BB%83%E9%9B%86%E5%92%8C%E6%B5%8B%E8%AF%95%E9%9B%86&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-0-86683743-null-null&spm=3001.4430
https://wenku.baidu.com/view/9597ad3bc6da50e2524de518964bcf84b9d52db6.html
程序1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | import lightgbm as lgb import pandas as pd from sklearn.metrics import mean_squared_error from sklearn.model_selection import GridSearchCV from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.datasets import make_classification import csv import numpy as np def read_data(test_data = 'input/train.csv' , n = 0 , label = 1 ): ''' 加载数据的功能 n:特征数据起始位 label:是否是监督样本数据 ''' csv.field_size_limit( 500 * 1024 * 1024 ) #一定要加上这一句 csv_reader = csv.reader( open (test_data, encoding = "utf8" , errors = "ignore" )) data_list = [] for one_line in csv_reader: data_list.append(one_line) x_list = [] y_list = [] for one_line in data_list[ 1 :]: if label = = 1 : #如果是监督样本数据 y_list.append( int (one_line[ - 1 ])) # 标志位(最后一位都是标签位) one_list = [o for o in one_line[n: - 1 ]] x_list.append(one_list) else : one_list = [o for o in one_line[n:]] x_list.append(one_list) return x_list, y_list def split_data(data_list, y_list, ratio = 0.30 ): #70%训练集,30%测试集: 914285,391837 ''' 按照指定的比例,划分样本数据集 ratio: 测试数据的比率 ''' X_train, X_test, y_train, y_test = train_test_split(data_list, y_list, test_size = ratio, random_state = 50 ) """训练集""" with open ( 'input/sub_train.csv' , 'w' , encoding = "utf8" ,newline = " ", errors=" ignore ") as csvfile:#不加newline=" "的话会空一行出来 fieldnames = [ 'qid' , 'question_text' , 'target' ] write = csv.DictWriter(csvfile,fieldnames = fieldnames) write.writeheader() #写表头 for i in range ( len (X_train)): write.writerow({ 'qid' :X_train[i][ 0 ], 'question_text' :X_train[i][ 1 ], 'target' :y_train[i]}) """测试集""" #标签文件 with open ( 'input/sub_test_y' , 'w' ) as fp: json.dump(y_test, fp) #测试csv with open ( 'input/sub_test_x.csv' , 'w' , encoding = "utf8" ,newline = " ", errors=" ignore ") as csvfile:#不加newline=" "的话会空一行出来 fieldnames = [ 'qid' , 'question_text' ] write = csv.DictWriter(csvfile,fieldnames = fieldnames) write.writeheader() #写表头 for i in range ( len (X_test)): write.writerow({ 'qid' :X_test[i][ 0 ], 'question_text' :X_test[i][ 1 ]}) return X_train, X_test, y_train, y_test F_feature,F_label = read_data(test_data = 'D:\\20210706E\\2020-python\\light_GBM\\Iris.csv' , n = 1 , label = 1 ) print (np.array(F_feature)) print (np.array(F_label)) X_train,X_test,y_train,y_test = train_test_split(np.array(F_feature),np.array(F_label),test_size = 0.2 ) #X_train,X_test,y_train,y_test =train_test_split(F_train,F_train,test_size=0.2) print ( 70 * '*\n' ) '''df_train = pd.read_csv('D:\\20210706E\\2020-python\\light_GBM\\Iris.csv',sep=",") #print(df_train) df_train_label=df_train['Species'] # df_train_feature=df_train['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm'] df_train_feature=df_train[['SepalWidthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm']] #print(df_train_label.tolist()) #test = df_train_label.drop(, axis = 1) #print(test) print(df_train_feature)''' # df_train = pd.read_csv('../regression/regression.train', header=None, sep='\t') # df_test = pd.read_csv('../regression/regression.test', header=None, sep='\t') # y_train = df_train[0].values # y_test = df_test[0].values # X_train = df_train.drop(0, axis=1).values # X_test = df_test.drop(0, axis=1).values # 加载数据 print ( 'Load data...' ) iris = load_iris() data = iris.data target = iris.target #X_train,X_test,y_train,y_test =train_test_split(data,target,test_size=0.2) # df_train = pd.read_csv('../regression/regression.train', header=None, sep='\t') # df_test = pd.read_csv('../regression/regression.test', header=None, sep='\t') # y_train = df_train[0].values # y_test = df_test[0].values # X_train = df_train.drop(0, axis=1).values # X_test = df_test.drop(0, axis=1).values print ( 'Start training...' ) # 创建模型,训练模型 gbm = lgb.LGBMRegressor(objective = 'regression' ,num_leaves = 31 ,learning_rate = 0.05 ,n_estimators = 20 ) gbm.fit(X_train, y_train,eval_set = [(X_test, y_test)],eval_metric = 'l1' ,early_stopping_rounds = 5 ) <br> print ( 'Start predicting...' ) # 测试机预测 y_pred = gbm.predict(X_test, num_iteration = gbm.best_iteration_) # 模型评估 print ( 'The rmse of prediction is:' , mean_squared_error(y_test, y_pred) * * 0.5 ) # feature importances print ( 'Feature importances:' , list (gbm.feature_importances_)) # 网格搜索,参数优化 estimator = lgb.LGBMRegressor(num_leaves = 31 ) param_grid = { 'learning_rate' : [ 0.01 , 0.1 , 1 ], 'n_estimators' : [ 20 , 40 ] } gbm = GridSearchCV(estimator, param_grid) gbm.fit(X_train, y_train) print ( 'Best parameters found by grid search are:' , gbm.best_params_) |
from sklearn.metrics import roc_auc_score, accuracy_score
数据1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | Id ,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species 1 , 5.1 , 3.5 , 1.4 , 0.2 , 0 2 , 4.9 , 3.0 , 1.4 , 0.2 , 0 3 , 4.7 , 3.2 , 1.3 , 0.2 , 0 4 , 4.6 , 3.1 , 1.5 , 0.2 , 0 5 , 5.0 , 3.6 , 1.4 , 0.2 , 0 6 , 5.4 , 3.9 , 1.7 , 0.4 , 0 7 , 4.6 , 3.4 , 1.4 , 0.3 , 0 8 , 5.0 , 3.4 , 1.5 , 0.2 , 0 9 , 4.4 , 2.9 , 1.4 , 0.2 , 0 10 , 4.9 , 3.1 , 1.5 , 0.1 , 0 11 , 5.4 , 3.7 , 1.5 , 0.2 , 0 12 , 4.8 , 3.4 , 1.6 , 0.2 , 0 13 , 4.8 , 3.0 , 1.4 , 0.1 , 0 14 , 4.3 , 3.0 , 1.1 , 0.1 , 0 15 , 5.8 , 4.0 , 1.2 , 0.2 , 0 16 , 5.7 , 4.4 , 1.5 , 0.4 , 0 17 , 5.4 , 3.9 , 1.3 , 0.4 , 0 18 , 5.1 , 3.5 , 1.4 , 0.3 , 0 19 , 5.7 , 3.8 , 1.7 , 0.3 , 0 20 , 5.1 , 3.8 , 1.5 , 0.3 , 0 21 , 5.4 , 3.4 , 1.7 , 0.2 , 0 22 , 5.1 , 3.7 , 1.5 , 0.4 , 0 23 , 4.6 , 3.6 , 1.0 , 0.2 , 0 24 , 5.1 , 3.3 , 1.7 , 0.5 , 0 25 , 4.8 , 3.4 , 1.9 , 0.2 , 0 26 , 5.0 , 3.0 , 1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 129 , 6.4 , 2.8 , 5.6 , 2.1 , 2 130 , 7.2 , 3.0 , 5.8 , 1.6 , 2 131 , 7.4 , 2.8 , 6.1 , 1.9 , 2 132 , 7.9 , 3.8 , 6.4 , 2.0 , 2 133 , 6.4 , 2.8 , 5.6 , 2.2 , 2 134 , 6.3 , 2.8 , 5.1 , 1.5 , 2 135 , 6.1 , 2.6 , 5.6 , 1.4 , 2 136 , 7.7 , 3.0 , 6.1 , 2.3 , 2 137 , 6.3 , 3.4 , 5.6 , 2.4 , 2 138 , 6.4 , 3.1 , 5.5 , 1.8 , 2 139 , 6.0 , 3.0 , 4.8 , 1.8 , 2 140 , 6.9 , 3.1 , 5.4 , 2.1 , 2 141 , 6.7 , 3.1 , 5.6 , 2.4 , 2 142 , 6.9 , 3.1 , 5.1 , 2.3 , 2 143 , 5.8 , 2.7 , 5.1 , 1.9 , 2 144 , 6.8 , 3.2 , 5.9 , 2.3 , 2 145 , 6.7 , 3.3 , 5.7 , 2.5 , 2 146 , 6.7 , 3.0 , 5.2 , 2.3 , 2 147 , 6.3 , 2.5 , 5.0 , 1.9 , 2 148 , 6.5 , 3.0 , 5.2 , 2.0 , 2 149 , 6.2 , 3.4 , 5.4 , 2.3 , 2 150 , 5.9 , 3.0 , 5.1 , 1.8 , 2 |
from lightgbm import LGBMClassifier from sklearn.metrics import accuracy_score from sklearn.model_selection import GridSearchCV from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.externals import joblib # 加载数据 iris = load_iris() data = iris.data target = iris.target # 划分训练数据和测试数据 X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.2) # 模型训练 gbm = LGBMClassifier(num_leaves=31, learning_rate=0.05, n_estimators=20) gbm.fit(X_train, y_train, eval_set=[(X_test, y_test)], early_stopping_rounds=5) # 模型存储 joblib.dump(gbm, 'loan_model.pkl') # 模型加载 gbm = joblib.load('loan_model.pkl') # 模型预测 y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration_) # 模型评估 print('The accuracy of prediction is:', accuracy_score(y_test, y_pred)) # 特征重要度 print('Feature importances:', list(gbm.feature_importances_)) # 网格搜索,参数优化 estimator = LGBMClassifier(num_leaves=31) param_grid = { 'learning_rate': [0.01, 0.1, 1], 'n_estimators': [20, 40] } gbm = GridSearchCV(estimator, param_grid) gbm.fit(X_train, y_train) print('Best parameters found by grid search are:', gbm.best_params_)
ImportError: cannot import name ‘joblib‘ from ‘sklearn.externals‘
https://blog.csdn.net/weixin_45031468/article/details/113825131
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏