国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Decision Tree

2019-11-11 06:52:31
字體:
來源:轉載
供稿:網友

Decision Tree Classifier

Decision Tree Classifier

from sklearn.tree import DecisionTreeClassifier as DTCy = df.targetX = df.featuresdtc = DTC(criterion='entropy', mim_samples_slit=20, random_state=90)dtc.fit(X, y)

official example

from sklearn.datasets import load_irisfrom sklearn.model_selection import cross_val_scorefrom sklearn.tree import DecisionTreeClassifierclf = DecisionTreeClassifier(random_state=0)iris = load_iris()cross_val_score(clf, iris.data, iris.target, cv=10)

Visualizing the tree

article

in advance you should install Graphviz

from sklearn.tree import export_graphvizdef visualize_tree(tree, feature_names): """Create tree png using graphviz. Args ---- tree -- scikit-learn DecsisionTree. feature_names -- list of feature names. usage --- features = X.columns visualize_tree(dtc, features) """ with open("dt.dot", 'w') as f: export_graphviz(tree, out_file=f, feature_names=feature_names) #generate png command = ["dot", "-Tpng", "dt.dot", "-o", "dt.png"] #or pdf #command = ["dot", "-Tpdf", "dt.dot", "-o", "dt.pdf"] try: subPRocess.check_call(command) except: exit("Could not run dot, ie graphviz, to " "produce visualization") #open image from PIL import Image im = Image.open("od.png") im.show()

Decision Tree Regression

example

DecisionTreeRegressor

Decision Tree Regression with AdaBoost

from sklearn.tree import DecisionTreeRegressorregr = DecisionTreeRegressor(max_depth=2)regr.fit(X, y)y_predict = regr_1.predict(X_test)

ID3 (Iterative Dichotomiser)

屬性集合A={a1,a2,…,am} 如{身高,體重,是否近視}

樣本集合D={(x1;y1),(x2;y2),…,(xm;ym)} 如{(身高175,體重63,近視1;不符合應聘要求0),…}

根據某屬性a的劃分D1,D2,…

informathin entropy

Ent(D)=?∑k=1|m|pklog2pk

pk是每類樣本占當前樣本集合D中的比例

Ent越小純度越高

決策樹根節點的D包含所有樣本,如果y只有0,1兩個取值,正3個負2個,則 Ent(D)=?(25log225+35log235)

information gain

根據某屬性a劃分得到Dv(v=1,2,…,V) Gain(D,a)=Ent(D)?∑v=1V|Dv||D|Ent(Dv)

Gain越大劃分得到的純度提升越高

example

假設有A = {行為習慣,飲食偏好, 體育運動}三個屬性,判斷是否會得某種病。

總共6個得病9個不得

行為習慣 得病 不得病 得病占該習慣總數比例 該行為習慣占總人數的比例
抽煙 1 5 1/6 6/15
喝酒 2 3 2/5 5/15
吸毒 3 1 3/4 4/15

Ent(D)=?(615log265+95log295) 根據行為習慣劃分出抽煙,喝酒,吸毒三個子集D1,D2,D3 Ent(D1)=?(16log216+56log256)Ent(D2),Ent(D3)同理

Gain(D,行為習慣)=Ent(D)?(615Ent(D1)+515Ent(D2)+415Ent(D3))

之后再算Gain(D,飲食偏好)

假設Gain(D,行為習慣)>Gain(D,飲食偏好)>Gain(D,體育運動)

那么分別取D1,D2,D3為新的D,剩下的屬性為A={飲食偏好,體育運動} ,進行迭代算Gain(D,飲食偏好)Gain(D,體育運動)

C4.5

基于增益率(gain ratio)減少ID3偏好可取數目多帶來的影響。

剪枝

預剪枝

劃分訓練集和測試集。

如果使用某一劃分,算出驗證集精度。

如果停止劃分采用樣本中占多數的結果作為該分支結果,計算精度。

如果停止劃分精度反而更高則停止劃分。

后剪枝

從完整的決策樹的倒數第二個節點開始。

如果剪掉倒數第一個節點精度提高則剪掉。

依次往上類推

連續數據

采用二分法劃分,既劃分成≤t>t, t取遍兩個相鄰離散數據的平均值然后找出信息熵增Gain最小的。

缺失值處理

先算出對該屬性而言無缺失值的Entropy, 然后計算出對該屬性而言無缺失值的Gain。

然后乘上無缺失值樣本所占比例ρ最終的Gain。

多變量決策樹(multivariate decision tree)

使用斜線來劃分多變量。

既用多變量的線性組合W?Attribtes≤t? 來劃分。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 祁门县| 辽阳市| 娱乐| 延津县| 扬中市| 蒲江县| 怀来县| 烟台市| 遵义市| 新宁县| 岢岚县| 惠来县| 宁津县| 定南县| 辽阳市| 碌曲县| 天等县| 海阳市| 博白县| 丰城市| 定日县| 青冈县| 旬阳县| 大丰市| 册亨县| 左权县| 禹城市| 桐庐县| 宜阳县| 孟州市| 宝应县| 奈曼旗| 金寨县| 丰都县| 大新县| 汝城县| 徐水县| 娄底市| 新河县| 舒兰市| 洛扎县|