Python中的joblib模塊詳解
背景
用已知的數(shù)據(jù)集經(jīng)過(guò)反復(fù)調(diào)優(yōu)后,訓(xùn)練出一個(gè)較為精準(zhǔn)的模型,想要用來(lái)對(duì)格式相同的新數(shù)據(jù)進(jìn)行預(yù)測(cè)或分類。
難道又要重復(fù)運(yùn)行用于訓(xùn)練模型的源數(shù)據(jù)和代碼?
常見(jiàn)的做法是將其訓(xùn)練好模型封裝成一個(gè)模型文件,直接調(diào)用此模型文件用于后續(xù)的訓(xùn)練 。
一、保存最佳模型
joblib.dump(value,filename,compress=0,protocol=None)
- value:任何Python對(duì)象,要存儲(chǔ)到磁盤的對(duì)象。
- filename:文件名,str.pathlib.Path 或文件對(duì)象。要在其中存儲(chǔ)文件的文件對(duì)象或文件路徑。與支持的文件擴(kuò)展名之一(“.z”,“.gz”,“bz2”,“.xz”,“.lzma”)
- compress:int從0到9或bool或2元組。數(shù)據(jù)的可選壓縮級(jí)別。0或False不壓縮,較高的值表示更多的壓縮,但同時(shí)也降低了讀寫時(shí)間。使用3值通常是一個(gè)很好的折衷方案。如果compress為
- True,則使用的壓縮級(jí)別為3。如果compress為2元組,則第一個(gè)元素必須對(duì)應(yīng)于受支持的壓縮器之間的字符串(例如’zlib’,‘gzip’,‘bz2’,‘lzma’,'xz '),第二個(gè)元素必須是0到9的整數(shù),對(duì)應(yīng)于壓縮級(jí)別。
- 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') # 測(cè)試集 val_pos="F:\\python_machine_learing_work\\501_model\\data\\測(cè)試集\\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è)測(cè)試集)
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ù)測(cè)
load joblib.load(filename, mmap_mode=None)
- filename:str.pathlib.Path或文件對(duì)象。要從中加載對(duì)象的文件或文件路徑。
- mmap_mode:{無(wú),‘r +’,‘r’,‘w +’,‘c’},可選如果不是“None”,則從磁盤對(duì)陣列進(jìn)行內(nèi)存映射。此模式對(duì)壓縮文件無(wú)效。請(qǐng)注意,在這種情況下,重建對(duì)象可能不再與原始對(duì)象完全匹配。
加載模型
# 加載模型
load_model_xgb_importance = joblib.load("F:\\python_machine_learing_work\\501_model\\data\\測(cè)試集\\train_xgb_importance_model.dat")
# 使用模型預(yù)測(cè)
y_pred_rf = model_predict(load_model_xgb_importance, X_test, alpha = alpha)到此這篇關(guān)于Python中的joblib模塊詳解的文章就介紹到這了,更多相關(guān)Python的joblib模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python并行計(jì)算庫(kù)Joblib高效使用指北
- Python使用Joblib模塊實(shí)現(xiàn)加快任務(wù)處理速度
- python利用joblib進(jìn)行并行數(shù)據(jù)處理的代碼示例
- Python的joblib模型固化函數(shù)解析
- Python并行庫(kù)joblib之delayed函數(shù)與Parallel函數(shù)詳解
- Python中的Joblib庫(kù)使用學(xué)習(xí)總結(jié)
- python如何利用joblib保存訓(xùn)練模型
- Python Joblib庫(kù)使用方法案例總結(jié)
- Python高效計(jì)算庫(kù)Joblib的入門教程
相關(guān)文章
Python使用pyaudio實(shí)現(xiàn)錄音功能
pyaudio是一個(gè)跨平臺(tái)的音頻I/O庫(kù),使用PyAudio可以在Python程序中播放和錄制音頻,本文將利用它實(shí)現(xiàn)錄音功能,并做到停止說(shuō)話時(shí)自動(dòng)結(jié)束2023-05-05
在VScode中配置Python開(kāi)發(fā)環(huán)境的超詳細(xì)指南
在使用VSCode編寫Python代碼前,我們需要先配置Python環(huán)境,這篇文章主要給大家介紹了關(guān)于在VScode中配置Python開(kāi)發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下2023-12-12
python實(shí)現(xiàn)文件+參數(shù)發(fā)送request的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)文件+參數(shù)發(fā)送request的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
python使用turtle庫(kù)與random庫(kù)繪制雪花
這篇文章主要為大家詳細(xì)介紹了python使用turtle庫(kù)與random庫(kù)繪制雪花,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
python 在threading中如何處理主進(jìn)程和子線程的關(guān)系
這篇文章主要介紹了python 在threading中如何處理主進(jìn)程和子線程的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
PyQt 5 設(shè)置Logo圖標(biāo)和Title標(biāo)題的操作
這篇文章主要介紹了PyQt 5 設(shè)置Logo圖標(biāo)和Title標(biāo)題的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
手把手帶你用Python實(shí)現(xiàn)一個(gè)計(jì)時(shí)器
雖然Python是一種有效的編程語(yǔ)言,但純Python程序比C、Rust和Java等編譯語(yǔ)言中的對(duì)應(yīng)程序運(yùn)行得更慢,為了更好地監(jiān)控和優(yōu)化Python程序,今天將為大家介紹如何使用?Python?計(jì)時(shí)器來(lái)監(jiān)控程序運(yùn)行的速度,以便正對(duì)性改善代碼性能2022-06-06

