欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中的joblib模塊詳解

 更新時(shí)間:2023年08月24日 09:10:39   作者:sodaloveer  
這篇文章主要介紹了Python中的joblib模塊詳解,用已知的數(shù)據(jù)集經(jīng)過反復(fù)調(diào)優(yōu)后,訓(xùn)練出一個(gè)較為精準(zhǔn)的模型,想要用來對格式相同的新數(shù)據(jù)進(jìn)行預(yù)測或分類,常見的做法是將其訓(xùn)練好模型封裝成一個(gè)模型文件,直接調(diào)用此模型文件用于后續(xù)的訓(xùn)練,需要的朋友可以參考下

背景

用已知的數(shù)據(jù)集經(jīng)過反復(fù)調(diào)優(yōu)后,訓(xùn)練出一個(gè)較為精準(zhǔn)的模型,想要用來對格式相同的新數(shù)據(jù)進(jìn)行預(yù)測或分類。

難道又要重復(fù)運(yùn)行用于訓(xùn)練模型的源數(shù)據(jù)和代碼?

常見的做法是將其訓(xùn)練好模型封裝成一個(gè)模型文件,直接調(diào)用此模型文件用于后續(xù)的訓(xùn)練 。

一、保存最佳模型

joblib.dump(value,filename,compress=0,protocol=None)
  • value:任何Python對象,要存儲到磁盤的對象。
  • filename:文件名,str.pathlib.Path 或文件對象。要在其中存儲文件的文件對象或文件路徑。與支持的文件擴(kuò)展名之一(“.z”,“.gz”,“bz2”,“.xz”,“.lzma”)
  • compress:int從0到9或bool或2元組。數(shù)據(jù)的可選壓縮級別。0或False不壓縮,較高的值表示更多的壓縮,但同時(shí)也降低了讀寫時(shí)間。使用3值通常是一個(gè)很好的折衷方案。如果compress為
  • True,則使用的壓縮級別為3。如果compress為2元組,則第一個(gè)元素必須對應(yīng)于受支持的壓縮器之間的字符串(例如’zlib’,‘gzip’,‘bz2’,‘lzma’,'xz '),第二個(gè)元素必須是0到9的整數(shù),對應(yīng)于壓縮級別。
  • protocol:不用管了,與pickle里的protocol參數(shù)一樣

舉例

  • 導(dǎo)入數(shù)據(jù)
import pandas as pd
# 訓(xùn)練集
file_pos="F:\\python_machine_learing_work\\501_model\\data\\訓(xùn)練集\\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')
  • 劃分?jǐn)?shù)據(jù)
# 重要變量
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']
#拆分?jǐn)?shù)據(jù)集(一個(gè)訓(xùn)練集一個(gè)測試集)
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)
  • 訓(xùn)練模型
from sklearn.model_selection import GridSearchCV
def model_train(X_train,y_train,model):
    ## 導(dǎo)入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') 
  • 保存模型
# 導(dǎo)入包
import joblib 
# 保存模型
joblib.dump(xgb_model, 'train_rf_importance_model.dat', compress=3) 

二、加載模型并用于預(yù)測

load joblib.load(filename, mmap_mode=None)
  • filename:str.pathlib.Path或文件對象。要從中加載對象的文件或文件路徑。
  • mmap_mode:{無,‘r +’,‘r’,‘w +’,‘c’},可選如果不是“None”,則從磁盤對陣列進(jìn)行內(nèi)存映射。此模式對壓縮文件無效。請注意,在這種情況下,重建對象可能不再與原始對象完全匹配。

加載模型

# 加載模型
load_model_xgb_importance = joblib.load("F:\\python_machine_learing_work\\501_model\\data\\測試集\\train_xgb_importance_model.dat")
# 使用模型預(yù)測
y_pred_rf = model_predict(load_model_xgb_importance, X_test, alpha = alpha)

到此這篇關(guān)于Python中的joblib模塊詳解的文章就介紹到這了,更多相關(guān)Python的joblib模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論