机器学习自动调参小试

2024-01-20 23:50

本文主要是介绍机器学习自动调参小试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 安装环境

1.1 安装anconda 环境

Linux-Centos7下安装Anaconda(2019年新版)

 

1.2 安装xgboost

conda install py-xgboost

 

1.3 安装ray

pip install --upgrade pip

pip install ray -i https://pypi.doubanio.com/simple

 

2. 脚本

2.1 第一版

#-*- encoding=utf-8 -*-def get_dataset(input_path):df = pd.read_csv(input_path)columns_list = [col for col in df.columns if col != 'label']data, label = df[columns_list], df['label']return data, labeldef objective_function(config, checkpoint_dir=None, path=None):"""需要优化的目标函数:config: 优化对象,超参范围:path: (训练集,OOT文件路径)"""train_path, oot_path = pathtrain_data, train_label = get_dataset(train_path)train_mat = xgboost.DMatrix(train_data, train_label)param = config.copy()...

2.2 第二版

def objective_function(config, checkpoint_dir=None, path=None):"""需要优化的目标函数:config: 优化对象,超参范围:path: (训练集,OOT文件路径)"""train_path, oot_path = pathtrain_mat = xgboost.DMatrix(train_path)...

2.3 完整代码

#-*- encoding=utf-8 -*-import os
import time
import pickle
import numpy as np
import xgboost
import sklearn.metrics as metrics
from ray import tune
from ray.tune.suggest.bohb import TuneBOHB
from ray.tune.schedulers import HyperBandForBOHBdef get_auc_ks(scores, labels):"""计算KS,AUC值:param scores: list-like, model scores;:param labels: list-like, labels;:return: tuple(float, float), auc & ks ;"""flg = Falseif isinstance(labels, xgboost.DMatrix):flg = Truelabels = labels.get_label()fpr, tpr, thresholds = metrics.roc_curve(labels, scores, pos_label=1)auc = metrics.auc(fpr, tpr)ks = np.max(np.abs(tpr - fpr))if flg:return [('my_auc', auc), ('KS', ks)]else:return auc, ksdef metric_ks(pred, dtrain):"""ks metric:param estimator: 模型:param X: 特征:param y: label"""scores = predy = dtrain.get_label()fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=1)ks = np.max(np.abs(tpr - fpr))return 'ks', ksdef custom_metric(pred, dtrain):labels = dtrain.get_label()scores = predfpr, tpr, thresholds = metrics.roc_curve(labels, scores, pos_label=1)auc = metrics.auc(fpr, tpr)ks = np.max(np.abs(tpr - fpr))return [('auc', auc), ('KS', ks)]def objective_function(config, checkpoint_dir=None, path=None):"""需要优化的目标函数:config: 优化对象,超参范围:path: (训练集,OOT文件路径)"""train_path, oot_path = pathtrain_mat = xgboost.DMatrix(train_path)param = config.copy()param["max_depth"] = int(param["max_depth"])n_estimators = int(param.pop("n_estimators"))result = {}cv_results = xgboost.cv(param, dtrain=train_mat, num_boost_round=n_estimators, nfold=5, metrics='logloss', feval=custom_metric, maximize=True, callbacks=[record_evaluation(result, oot_path)])test_score = (result["detail_metrics"]["my_oot"]["auc"][-1], result["detail_metrics"]["my_oot"]["KS"][-1])valid_score = (result["detail_metrics"]["my_valid"]["auc"][-1], result["detail_metrics"]["my_valid"]["KS"][-1])train_score = (result["detail_metrics"]["my_train"]["auc"][-1], result["detail_metrics"]["my_train"]["KS"][-1])nfold = len(valid_score[0])monitor_metric = sum(valid_score[0]) / nfoldwith tune.checkpoint_dir(step=1) as checkpoint_dir:path = os.path.join(checkpoint_dir, "cv_result")with open(path, 'wb') as f:pickle.dump(cv_results, f)return tune.report(valid_auc=monitor_metric,test_score=test_score,valid_score=valid_score,train_score=train_score,done=True)def record_evaluation(eval_result, oot_path):"""callback记录xgboost.cv的指标结果,包含train, valid, oot:eval_result: dict A dictionary to store the evaluation results.:oot_path: OOT Data file path"""if not isinstance(eval_result, dict):raise TypeError('eval_result has to be a dictionary')eval_result.clear()oot_mat = xgboost.DMatrix(oot_path)def init(env):"""internal function"""for item in env.evaluation_result_list:k = item[0]pos = k.index('-')key = k[:pos]metric = k[pos + 1:]if key not in eval_result:eval_result[key] = {}if metric not in eval_result[key]:eval_result[key][metric] = []if 'detail_metrics' not in eval_result:eval_result['detail_metrics'] = {"my_train": {}, "my_valid": {}, "my_oot": {}}def callback(env):"""internal function"""if not eval_result:init(env)for item in env.evaluation_result_list:k, v = item[0], item[1]pos = k.index('-')key = k[:pos]metric = k[pos + 1:]eval_result[key][metric].append(v)tmp = {"my_train": {}, "my_valid": {}, "my_oot": {}}for cvpack in env.cvfolds:bst = cvpack.bstpred_train = bst.predict(cvpack.dtrain)pred_valid = bst.predict(cvpack.dtest)pred_oot = bst.predict(oot_mat)metrics_result_train = dict(custom_metric(pred_train, cvpack.dtrain))metrics_result_valid = dict(custom_metric(pred_valid, cvpack.dtest))metrics_result_oot = dict(custom_metric(pred_oot, oot_mat))for k in metrics_result_oot:tmp["my_train"][k] = tmp["my_train"].get(k, [])+ [metrics_result_train[k]]tmp["my_valid"][k] = tmp["my_valid"].get(k, [])+ [metrics_result_valid[k]]tmp["my_oot"][k] = tmp["my_oot"].get(k, [])+ [metrics_result_oot[k]]for k1 in tmp:for k2 in tmp[k1]:eval_result["detail_metrics"][k1].setdefault(k2, []).append(tmp[k1][k2])return callbackdef hyperopt(param_space, trainpath, testpath, num_eval, name, obj_funcs, log_path='~/ray_results'):"""贝叶斯自动寻参数:param_space: 参数范围,组合范围:X_train: 训练集特征:y_train: 寻链接标签:X_test: 测试集特征:y_test: 测试集标签:num_eval: 寻参次数:log_path: log文件存储路径"""start = time.time()path = (trainpath, testpath)opt = TuneBOHB(max_concurrent=2)bohb = HyperBandForBOHB(time_attr="training_iteration",max_t=num_eval)analysis = tune.run(tune.with_parameters(obj_funcs, path=path), config=param_space, num_samples=num_eval, local_dir=log_path,metric='valid_auc', mode='max', search_alg=opt, scheduler=bohb,resources_per_trial={"cpu": 5}, name=name)best_params = analysis.get_best_config(metric="valid_auc", mode="max")best_params["max_depth"] = int(best_params["max_depth"])n_estimators = int(best_params.pop("n_estimators"))train_mat = xgboost.DMatrix(trainpath)test_mat = xgboost.DMatrix(testpath)model = xgboost.train(best_params, train_mat, n_estimators)    pred_test = model.predict(test_mat)pred_train = model.predict(train_mat)print("-----Results-----")print("Best model & parameters: {}".format(best_params))print("Train Score: {}".format(get_auc_ks(pred_train, train_mat.get_label())))print("Test Score: {}".format(get_auc_ks(pred_test, test_mat.get_label())))print("Time elapsed: {}".format(time.time() - start))print("Parameter combinations evaluated: {}".format(num_eval))return Noneif __name__ == "__main__":trainfile_path = "./train.buffer"testfile_path = "./oot.buffer"name = 'ppdnew_V2'control_overfitting = Falseparam = {'booster': "gbtree",'eta': tune.uniform(0.01, 1),'seed': 1,'max_depth': tune.uniform(3, 5),'n_estimators': tune.uniform(50, 500),'min_child_weight': tune.uniform(1, 300),'colsample_bytree': tune.uniform(0.6, 1.0),'subsample': tune.uniform(0.5, 1),'lambda': tune.uniform(0.0, 100),'alpha': tune.uniform(0.0, 100),'scale_pos_weight': tune.uniform(1, 5),'n_jobs': 5}print("begin tuning")hyperopt(param, trainfile_path, testfile_path, 100, name, obj_funcs=objective_function)

 

3. 运行结果

3.1 运行过程中的结果

 

3.2 查看最终结果

from ray import tune
path = "~/ray_results/ppdnew_V2"
ana = tune.Analysis(path)
df = ana.dataframe().sort_values("valid_auc", ascending=False)print("path: ", path)diff_time = df.timestamp.max() - df.timestamp.min()
print("total time(h): ", diff_time / 60 / 60)
print("")
print(df.sort_values("valid_auc", ascending=False))
print("average running time, ", df["time_this_iter_s"].mean())
print(df.iloc[0])

 

4. 小结

4.1 环境

配置环境时,踩了很多坑,执行代码时可能会出现“dlopen:cannot load any more object with static TLS:”,找了很多方法尝试无果后,打算配置个conda环境来试试,然后就绕过了这个问题。

 

4.2 代码

(1)刚开始采用xgboost.DMatrix直接加载csv文件,但不清楚如何指定label,导致返回的auc等指标为nan,排查后发现不指定label会默认设置成0;

(2)加载数据采用pandas读取,然后传给xgboost.DMatrix(train_data, train_label)的格式,这种方式速度较慢。

(3)先对csv文件进行处理,转换成xgboost.DMatrix可以加载的二进制文件,代码如下:

#-*- encoding=utf-8 -*-import sys
import pandas as pd
import xgboost as xgbif __name__ == "__main__":input_path = sys.argv[1]output_path = sys.argv[2]df = pd.read_csv(input_path)df.to_csv("./temp.csv", index=False, header=None)mat = xgb.DMatrix("./temp.csv?format=csv&label_column=0")mat.save_binary(output_path)

 

这篇关于机器学习自动调参小试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/627742

相关文章

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp