日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

as_matrix、保存训练模型

發(fā)布時間:2025/3/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 as_matrix、保存训练模型 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#-*- coding: utf-8 -*- #構(gòu)建并測試CART決策樹模型import pandas as pd #導(dǎo)入數(shù)據(jù)分析庫 from random import shuffle #導(dǎo)入隨機(jī)函數(shù)shuffle,用來打亂數(shù)據(jù) import matplotlib.pyplot as plt #導(dǎo)入Matplotlib datafile = '../data/model.xls' #數(shù)據(jù)名 data = pd.read_excel(datafile) #讀取數(shù)據(jù),數(shù)據(jù)的前三列是特征,第四列是標(biāo)簽 #print(data) # 電量趨勢下降指標(biāo) 線損指標(biāo) 告警類指標(biāo) 是否竊漏電 # 0 4 1 1 1 # 1 4 0 4 1 # 2 2 1 1 1 # 3 9 0 0 0 data = data.as_matrix() #將表格轉(zhuǎn)換為矩陣 #print(data) # [[4 1 1 1] # [4 0 4 1] # [2 1 1 1] shuffle(data) #隨機(jī)打亂數(shù)據(jù) p = 0.8 #設(shè)置訓(xùn)練數(shù)據(jù)比例 train = data[:int(len(data)*p),:] #前80%為訓(xùn)練集 test = data[int(len(data)*p):,:] #后20%為測試集#構(gòu)建CART決策樹模型 from sklearn.tree import DecisionTreeClassifier #導(dǎo)入決策樹模型 treefile = '../tmp/tree.pkl' #模型輸出名字 tree = DecisionTreeClassifier() #建立決策樹模型 tree.fit(train[:,:3], train[:,3]) #訓(xùn)練#保存模型 from sklearn.externals import joblib joblib.dump(tree, treefile)# from cm_plot import * #導(dǎo)入自行編寫的混淆矩陣可視化函數(shù) # cm_plot(train[:,3], tree.predict(train[:,:3])).show() #顯示混淆矩陣可視化結(jié)果 #注意到Scikit-Learn使用predict方法直接給出預(yù)測結(jié)果。from sklearn.metrics import roc_curve #導(dǎo)入ROC曲線函數(shù) fpr, tpr, thresholds = roc_curve(test[:,3], tree.predict_proba(test[:,:3])[:,1], pos_label=1) plt.plot(fpr, tpr, linewidth=2, label = 'ROC of CART', color = 'green') #作出ROC曲線 plt.xlabel('False Positive Rate') #坐標(biāo)軸標(biāo)簽 plt.ylabel('True Positive Rate') #坐標(biāo)軸標(biāo)簽 plt.ylim(0,1.05) #邊界范圍 plt.xlim(0,1.05) #邊界范圍 plt.legend(loc=4) #圖例 plt.show() #顯示作圖結(jié)果

?

轉(zhuǎn)載于:https://www.cnblogs.com/ggzhangxiaochao/p/9115273.html

總結(jié)

以上是生活随笔為你收集整理的as_matrix、保存训练模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。