1、保存模型,避免每次都要训练模型
from sklearn.externals import joblib
joblib.dump(model,'music-recommender.joblib')
后面使用时调用:
model = joblib.load('music-recommender.joblib')
predictions = model.predict([[21,1]])
2、可视化决策树
from sklearn import tree
tree.export_graphviz(model,out_file = 'music-recommender.dot',
feature_names = ['age','gender'],
class_names = sorted(y.unique()),
label='all',
rounded=True,
filled=True)
生成文件music-recommender.dot,用VS打开,搜索dot来安装拓展包graphviz,网页运行来可视化决策树。