Python决策树代码
說(shuō)明
這個(gè)是今天上課的代碼,記錄一下吧,以前都沒(méi)有這種意識(shí),學(xué)過(guò)就忘了。
環(huán)境
這里用的是anaconda,要用到這里面的代碼,還需要下載額外的軟件。當(dāng)你下載好了anaconda之后在“開(kāi)始”處可以找到“所有程序”中的anaconda文件夾中會(huì)有下面的文件。
一般我們打開(kāi)jupyter notebook,在里面new一個(gè)Python就可以開(kāi)始寫(xiě)代碼了。
要能夠畫(huà)圖還需要下載一個(gè)軟件
安裝時(shí)可以根據(jù)下面這篇配置環(huán)境
https://jingyan.baidu.com/article/020278115032461bcc9ce598.html
最后那步顯示圖片的可以先放著,不用跟著最后一步做。
配置好之后就依次在Anaconda Prompt命令窗口輸入
當(dāng)出現(xiàn)下面的圖時(shí),說(shuō)明安裝成功
數(shù)據(jù)
看一下這里用的數(shù)據(jù)
這里的D到Q列我們作為屬性輸入,S列是根據(jù)R列true為1,false為0轉(zhuǎn)換過(guò)來(lái)的,用來(lái)作為結(jié)果,因?yàn)镻ython中用于處理的數(shù)據(jù)要是數(shù)值型的,所以要轉(zhuǎn)化,如果這里要用到ABC列的數(shù)據(jù)也需要轉(zhuǎn)化。
代碼
# Importing the libraries #導(dǎo)入包 import numpy as np import matplotlib.pyplot as plt import pandas as pd# Importing the dataset #取數(shù)據(jù) dataset = pd.read_csv('C:/Users/Administrator/Desktop/chap4.csv') #這里取桌面的chap4的代碼 #這里取D到Q列 X = dataset.iloc[:,3:16].values #這里取S列 y = dataset.iloc[:,18].values# Splitting the dataset into the Training set and Test set #將數(shù)據(jù)集拆分為訓(xùn)練集和測(cè)試集 from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)# Feature Scalingfrom sklearn.preprocessing import StandardScaler sc_X = StandardScaler() X_train = sc_X.fit_transform(X_train) X_test = sc_X.transform(X_test)# Fitting Classifier to the Training set #訓(xùn)練集的分類(lèi)器擬合 from sklearn.tree import DecisionTreeClassifier classifier = DecisionTreeClassifier(criterion = 'entropy', random_state=0,class_weight={0:1,1:10}) #這里的class_weight={0:1,1:10},max_depth=20參數(shù)可以不用加入 # classifier = DecisionTreeClassifier(criterion = 'entropy', random_state=0) classifier.fit(X_train,y_train)# Predicting the Test set results #預(yù)測(cè)測(cè)試集結(jié)果 y_pred = classifier.predict(X_test)# Making the Confusion Matrix #制作混淆矩陣 from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, y_pred)scoretest = classifier.score(X_test,y_test)from IPython.display import Image import pydotplus from sklearn import tree # 可視化決策樹(shù) dot_data = tree.export_graphviz(classifier, out_file=None,filled=True, rounded=True,special_characters=True) graph = pydotplus.graph_from_dot_data(dot_data) # 顯示圖片 # graph.get_nodes()[7].set_fillcolor("#FFF2DD") graph.write_png("C:/Users/Administrator/Desktop/out.png") #這里我把圖片輸出到桌面的out.png出現(xiàn)結(jié)果為T(mén)rue說(shuō)明代碼運(yùn)行成功。
代碼里面有這個(gè)方法DecisionTreeClassifier(criterion = ‘entropy’,random_state=0,class_weight={0:1,1:10}),這里的class_weight是為了處理結(jié)果不平衡的問(wèn)題,該方法中還可以使用很多參數(shù),可以參考API文檔。可以根據(jù)自己的需要加入?yún)?shù),地址是:
https://scikitlearn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier
結(jié)果
看一下細(xì)節(jié)
總結(jié)
以上是生活随笔為你收集整理的Python决策树代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Qml环形倒计时
- 下一篇: python中如何画出决策树_使用Pyt