python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python的joblib模块

Python中的joblib模块详解

作者:sodaloveer

这篇文章主要介绍了Python中的joblib模块详解,用已知的数据集经过反复调优后,训练出一个较为精准的模型,想要用来对格式相同的新数据进行预测或分类,常见的做法是将其训练好模型封装成一个模型文件,直接调用此模型文件用于后续的训练,需要的朋友可以参考下

背景

用已知的数据集经过反复调优后,训练出一个较为精准的模型,想要用来对格式相同的新数据进行预测或分类。

难道又要重复运行用于训练模型的源数据和代码?

常见的做法是将其训练好模型封装成一个模型文件,直接调用此模型文件用于后续的训练 。

一、保存最佳模型

joblib.dump(value,filename,compress=0,protocol=None)

举例

import pandas as pd
# 训练集
file_pos="F:\\python_machine_learing_work\\501_model\\data\\训练集\\train_data_only_one.csv"
data_pos=pd.read_csv(file_pos,encoding='utf-8')
# 测试集
val_pos="F:\\python_machine_learing_work\\501_model\\data\\测试集\\test_data_table_only_one.csv"
data_val=pd.read_csv(val_pos,encoding='utf-8')
# 重要变量
ipt_col=['called_rate', 'calling_called_act_hour', 'calling_called_distinct_rp', 'calling_called_distinct_cnt', 'star_level_int', 'online_days', 'calling_called_raom_cnt', 'cert_cnt', 'white_flag_0', 'age', 'calling_called_cdr_less_15_cnt', 'white_flag_1', 'calling_called_same_area_rate', 'volte_cnt', 'cdr_duration_sum', 'calling_hour_cnt', 'cdr_duration_avg', 'calling_pre7_rate', 'cdr_duration_std', 'calling_disperate', 'calling_out_area_rate', 'calling_distinct_out_op_area_cnt','payment_type_2.0', 'package_price_group_2.0', 'is_vice_card_1.0']
#拆分数据集(一个训练集一个测试集)
def train_test_spl(train_data,val_data):
    global ipt_col
    X_train=train_data[ipt_col]
    X_test=val_data[ipt_col]
    y_train=train_data[target_col]
    y_test=val_data[target_col]
    return X_train, X_test, y_train, y_test
	X_train, X_test, y_train, y_test =train_test_spl(data_pos_4,data_val_4)
from sklearn.model_selection import GridSearchCV
def model_train(X_train,y_train,model):
    ## 导入XGBoost模型
    from xgboost.sklearn import XGBClassifier
    if  model=='XGB':
        parameters = {'max_depth': [3,5, 10, 15, 20, 25],
          			  'learning_rate':[0.1, 0.3, 0.6],
          			  'subsample': [0.6, 0.7, 0.8, 0.85, 0.95],
              		  'colsample_bytree': [0.5, 0.6, 0.7, 0.8, 0.9]}
        xlf= XGBClassifier(n_estimators=50)
        grid = GridSearchCV(xlf, param_grid=parameters, scoring='accuracy', cv=3)
        grid.fit(X_train, y_train)
        best_params=grid.best_params_
        res_model=XGBClassifier(max_depth=best_params['max_depth'],learning_rate=best_params['learning_rate'],subsample=best_params['subsample'],colsample_bytree=best_params['colsample_bytree'])
        res_model.fit(X_train, y_train)
    else:
        pass
    return res_model
xgb_model= model_train(X_train, y_train, model='XGB') 
# 导入包
import joblib 
# 保存模型
joblib.dump(xgb_model, 'train_rf_importance_model.dat', compress=3) 

二、加载模型并用于预测

load joblib.load(filename, mmap_mode=None)

加载模型

# 加载模型
load_model_xgb_importance = joblib.load("F:\\python_machine_learing_work\\501_model\\data\\测试集\\train_xgb_importance_model.dat")
# 使用模型预测
y_pred_rf = model_predict(load_model_xgb_importance, X_test, alpha = alpha)

到此这篇关于Python中的joblib模块详解的文章就介绍到这了,更多相关Python的joblib模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文