案例:利用保存svm训练模型 用pickle或者joblib

 pickle版本

from sklearn import svm
from sklearn.datasets import samples_generator
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import pickle 

print(__doc__)

# import some data to play with
X, y = samples_generator.make_classification(
    n_features=20, n_informative=3, n_redundant=0, n_classes=4,
    n_clusters_per_class=2)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# 
# # ANOVA SVM-C
# # 1) anova filter, take 3 best ranked features
# anova_filter = SelectKBest(f_regression, k=3)
# # 2) svm
# clf = svm.SVC(kernel='linear')
# 
# anova_svm = make_pipeline(anova_filter, clf)
# anova_svm.fit(X_train, y_train)

##保存模型
# # 以写二进制的方式打开文件
# file = open("model.pickle", "wb")
# # 把模型写入到文件中
# pickle.dump(anova_svm, file)
# # 关闭文件
# file.close()

##读取模型
# 以读二进制的方式打开文件
file = open("model.pickle", "rb")
# 把模型从文件中读取出来
anova_svm = pickle.load(file)
# 关闭文件
file.close()

y_pred = anova_svm.predict(X_test)
print(classification_report(y_test, y_pred))

输出:

None
D:\F\Anaconda3\lib\site-packages\sklearn\utils\__init__.py:54: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int32 == np.dtype(int).type`.
  if np.issubdtype(mask.dtype, np.int):
             precision    recall  f1-score   support

          0       0.29      0.33      0.31         6
          1       0.25      0.25      0.25         8
          2       0.40      0.33      0.36         6
          3       0.40      0.40      0.40         5

avg / total       0.32      0.32      0.32        25

joblib版本、

from sklearn import svm
from sklearn.datasets import samples_generator
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.externals import joblib

print(__doc__)

# import some data to play with
X, y = samples_generator.make_classification(
    n_features=20, n_informative=3, n_redundant=0, n_classes=4,
    n_clusters_per_class=2)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
 
# # ANOVA SVM-C
# # 1) anova filter, take 3 best ranked features
# anova_filter = SelectKBest(f_regression, k=3)
# # 2) svm
# clf = svm.SVC(kernel='linear')
#  
# anova_svm = make_pipeline(anova_filter, clf)
# anova_svm.fit(X_train, y_train)
# 保存模型
# joblib.dump(anova_svm, 'anova_svm.pkl') 

# 提取模型
anova_svm = joblib.load('anova_svm.pkl') 

y_pred = anova_svm.predict(X_test)
print(classification_report(y_test, y_pred))

猜你喜欢

转载自blog.csdn.net/zhuisaozhang1292/article/details/81455630