Python中的joblib模塊詳解
背景
用已知的數(shù)據(jù)集經(jīng)過反復(fù)調(diào)優(yōu)后,訓練出一個較為精準的模型,想要用來對格式相同的新數(shù)據(jù)進行預(yù)測或分類。
難道又要重復(fù)運行用于訓練模型的源數(shù)據(jù)和代碼?
常見的做法是將其訓練好模型封裝成一個模型文件,直接調(diào)用此模型文件用于后續(xù)的訓練 。
一、保存最佳模型
joblib.dump(value,filename,compress=0,protocol=None)
- value:任何Python對象,要存儲到磁盤的對象。
- filename:文件名,str.pathlib.Path 或文件對象。要在其中存儲文件的文件對象或文件路徑。與支持的文件擴展名之一(“.z”,“.gz”,“bz2”,“.xz”,“.lzma”)
- compress:int從0到9或bool或2元組。數(shù)據(jù)的可選壓縮級別。0或False不壓縮,較高的值表示更多的壓縮,但同時也降低了讀寫時間。使用3值通常是一個很好的折衷方案。如果compress為
- True,則使用的壓縮級別為3。如果compress為2元組,則第一個元素必須對應(yīng)于受支持的壓縮器之間的字符串(例如’zlib’,‘gzip’,‘bz2’,‘lzma’,'xz '),第二個元素必須是0到9的整數(shù),對應(yīng)于壓縮級別。
- protocol:不用管了,與pickle里的protocol參數(shù)一樣
舉例
- 導(dǎo)入數(shù)據(jù)
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')
- 劃分數(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'] #拆分數(shù)據(jù)集(一個訓練集一個測試集) 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): ## 導(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”,則從磁盤對陣列進行內(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)文章
Python while true實現(xiàn)爬蟲定時任務(wù)
這篇文章主要介紹了Python爬蟲定時任務(wù)簡單實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06python GUI庫圖形界面開發(fā)之PyQt5狀態(tài)欄控件QStatusBar詳細使用方法實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5狀態(tài)欄控件QStatusBar詳細使用方法實例,需要的朋友可以參考下2020-02-02