python利用joblib保存训练模型

在机器学习中我们训练模型后,需要把模型保存到本地,这里我们采用joblib来保存

from sklearn.externals import joblib

#保存模型
def Save_Model(self, model, filepath):
    joblib.dump(model, filename=filepath)

def Decision_Tree_classifier(self,x_train,y_train,max_depth=None,min_samples_split=2,min_samples_leaf=1):
    Decision_Tree=tree.DecisionTreeClassifier(max_depth=max_depth,min_samples_split=min_samples_split,min_samples_leaf=min_samples_leaf)
    Decision_Tree.fit(x_train,y_train)
    self.save_model(Decision_Tree,os.path.join(c_config.UPLOAD_FOLODER,'model','Decision_Tree.m'))
    return Decision_Tree

然后再通过 joblib.load 把模型加载回来

def Load_Model(self, filepath):
    model = joblib.load(filepath)
    return model

 

你可能感兴趣的:(python学习笔记,机器学习)