python数据分析练手小项目-汽车销售偷漏纳税人识别
生活随笔
收集整理的這篇文章主要介紹了
python数据分析练手小项目-汽车销售偷漏纳税人识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本項目主要掌握數據預處理和神經網絡、決策樹建模以及利用roc曲線進行模型評價。
import pandas as pd data=pd.read_excel(data/cardata.xls',index_col=0)#數據探索 import matplotlib.pyplot as plt data.describe() pd.value_counts(data[u'銷售類型']) pd.value_counts(data[u'銷售模式']) pd.crosstab(data[u'輸出'],data[u'銷售類型']) pd.crosstab(data[u'輸出'],data[u'銷售模式'])for col in data.columns:if not col in [u'銷售類型',u'銷售模式',u'輸出']:fig = plt.figure()data[col].hist(bins=20, by = data[u'輸出'])fig.show()#數據預處理 data=pd.merge(data,pd.get_dummies(data[u'銷售模式']),left_index=True,right_index=True) data=pd.merge(data,pd.get_dummies(data[u'銷售類型']),left_index=True,right_index=True) data['type']=pd.get_dummies(data[u'輸出'])[u'正常'] data = data.iloc[:,3:] del data[u'輸出'] from random import shuffle data_2=datadata[:5] data = data.as_matrix() #將表格轉換為矩陣 shuffle(data)p = 0.8 #設置訓練數據比例 train = data[:int(len(data)*p),:] #前80%為訓練集 test = data[int(len(data)*p):,:] #后20%為測試集 #混淆矩陣函數 def cm_plot(y, yp):from sklearn.metrics import confusion_matrixcm = confusion_matrix(y, yp) import matplotlib.pyplot as plt plt.matshow(cm, cmap=plt.cm.Greens) plt.colorbar() for x in range(len(cm)): for y in range(len(cm)):plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center')plt.ylabel('True label') plt.xlabel('Predicted label') return plt#構建CART決策樹模型 from sklearn.tree import DecisionTreeClassifier #導入決策樹模型treefile = 'output/cartree.pkl' #模型輸出名字 tree = DecisionTreeClassifier() #建立決策樹模型 tree.fit(train[:,:25], train[:,25]) #訓練,除了最后一列#保存模型 from sklearn.externals import joblib joblib.dump(tree, treefile) cm_plot(train[:,25], tree.predict(train[:,:25])).show() #顯示混淆矩陣可視化結果 #注意到Scikit-Learn使用predict方法直接給出預測結果; #分類準確率有62+37人 from sklearn.metrics import roc_curve #導入ROC曲線函數fpr, tpr, thresholds = roc_curve(test[:,25], tree.predict_proba(test[:,:25])[:,1], pos_label=1) plt.plot(fpr, tpr, linewidth=2, label = 'ROC of CART', color = 'green') #作出ROC曲線 plt.xlabel('False Positive Rate') #坐標軸標簽 plt.ylabel('True Positive Rate') #坐標軸標簽 plt.ylim(0,1.05) #邊界范圍 plt.xlim(0,1.05) #邊界范圍 plt.legend(loc=4) #圖例 plt.show() #顯示作圖結果總結
以上是生活随笔為你收集整理的python数据分析练手小项目-汽车销售偷漏纳税人识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 艾默生首席执行官范大为退休;液化空气将新
- 下一篇: oracle表空间权限赋予,Oracle