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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

决策树案例

發(fā)布時(shí)間:2023/12/8 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 决策树案例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

注:本案例為黑馬的課堂案例,上傳僅為方便查看

# 1.獲取數(shù)據(jù) # 2.數(shù)據(jù)基本處理 # 2.1 確定特征值,目標(biāo)值 # 2.2 缺失值處理 # 2.3 數(shù)據(jù)集劃分 # 3.特征工程(字典特征抽取) # 4.機(jī)器學(xué)習(xí)(決策樹) # 5.模型評(píng)估 import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.feature_extraction import DictVectorizer from sklearn.tree import DecisionTreeClassifier, export_graphviz # 1.獲取數(shù)據(jù),此數(shù)據(jù)連接已失效 data = pd.read_csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt") data row.namespclasssurvivednameageembarkedhome.destroomticketboatsex01234567891011121314151617181920212223242526272829...128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
11st1Allen, Miss Elisabeth Walton29.0000SouthamptonSt Louis, MOB-524160 L2212female
21st0Allison, Miss Helen Loraine2.0000SouthamptonMontreal, PQ / Chesterville, ONC26NaNNaNfemale
31st0Allison, Mr Hudson Joshua Creighton30.0000SouthamptonMontreal, PQ / Chesterville, ONC26NaN(135)male
41st0Allison, Mrs Hudson J.C. (Bessie Waldo Daniels)25.0000SouthamptonMontreal, PQ / Chesterville, ONC26NaNNaNfemale
51st1Allison, Master Hudson Trevor0.9167SouthamptonMontreal, PQ / Chesterville, ONC22NaN11male
61st1Anderson, Mr Harry47.0000SouthamptonNew York, NYE-12NaN3male
71st1Andrews, Miss Kornelia Theodosia63.0000SouthamptonHudson, NYD-713502 L7710female
81st0Andrews, Mr Thomas, jr39.0000SouthamptonBelfast, NIA-36NaNNaNmale
91st1Appleton, Mrs Edward Dale (Charlotte Lamson)58.0000SouthamptonBayside, Queens, NYC-101NaN2female
101st0Artagaveytia, Mr Ramon71.0000CherbourgMontevideo, UruguayNaNNaN(22)male
111st0Astor, Colonel John Jacob47.0000CherbourgNew York, NYNaN17754 L224 10s 6d(124)male
121st1Astor, Mrs John Jacob (Madeleine Talmadge Force)19.0000CherbourgNew York, NYNaN17754 L224 10s 6d4female
131st1Aubert, Mrs Leontine PaulineNaNCherbourgParis, FranceB-3517477 L69 6s9female
141st1Barkworth, Mr Algernon H.NaNSouthamptonHessle, YorksA-23NaNBmale
151st0Baumann, Mr John D.NaNSouthamptonNew York, NYNaNNaNNaNmale
161st1Baxter, Mrs James (Helene DeLaudeniere Chaput)50.0000CherbourgMontreal, PQB-58/60NaN6female
171st0Baxter, Mr Quigg Edmond24.0000CherbourgMontreal, PQB-58/60NaNNaNmale
181st0Beattie, Mr Thomson36.0000CherbourgWinnipeg, MNC-6NaNNaNmale
191st1Beckwith, Mr Richard Leonard37.0000SouthamptonNew York, NYD-35NaN5male
201st1Beckwith, Mrs Richard Leonard (Sallie Monypeny)47.0000SouthamptonNew York, NYD-35NaN5female
211st1Behr, Mr Karl Howell26.0000CherbourgNew York, NYC-148NaN5male
221st0Birnbaum, Mr Jakob25.0000CherbourgSan Francisco, CANaNNaN(148)male
231st1Bishop, Mr Dickinson H.25.0000CherbourgDowagiac, MIB-49NaN7male
241st1Bishop, Mrs Dickinson H. (Helen Walton)19.0000CherbourgDowagiac, MIB-49NaN7female
251st1Bjornstrm-Steffansson, Mr Mauritz Hakan28.0000SouthamptonStockholm, Sweden / Washington, DCNaNDmale
261st0Blackwell, Mr Stephen Weart45.0000SouthamptonTrenton, NJNaNNaN(241)male
271st1Blank, Mr Henry39.0000CherbourgGlen Ridge, NJA-31NaN7male
281st1Bonnell, Miss Caroline30.0000SouthamptonYoungstown, OHC-7NaN8female
291st1Bonnell, Miss Elizabeth58.0000SouthamptonBirkdale, England Cleveland, OhioC-103NaN8female
301st0Borebank, Mr John JamesNaNSouthamptonLondon / Winnipeg, MBD-21/2NaNNaNmale
.................................
12843rd0Vestrom, Miss Hulda Amanda AdolfinaNaNNaNNaNNaNNaNNaNfemale
12853rd0Vonk, Mr JenkoNaNNaNNaNNaNNaNNaNmale
12863rd0Ware, Mr FrederickNaNNaNNaNNaNNaNNaNmale
12873rd0Warren, Mr Charles WilliamNaNNaNNaNNaNNaNNaNmale
12883rd0Wazli, Mr YousifNaNNaNNaNNaNNaNNaNmale
12893rd0Webber, Mr JamesNaNNaNNaNNaNNaNNaNmale
12903rd1Wennerstrom, Mr August EdvardNaNNaNNaNNaNNaNNaNmale
12913rd0Wenzel, Mr LinhartNaNNaNNaNNaNNaNNaNmale
12923rd0Widegren, Mr Charles PeterNaNNaNNaNNaNNaNNaNmale
12933rd0Wiklund, Mr Jacob AlfredNaNNaNNaNNaNNaNNaNmale
12943rd1Wilkes, Mrs EllenNaNNaNNaNNaNNaNNaNfemale
12953rd0Willer, Mr AaronNaNNaNNaNNaNNaNNaNmale
12963rd0Willey, Mr EdwardNaNNaNNaNNaNNaNNaNmale
12973rd0Williams, Mr Howard HughNaNNaNNaNNaNNaNNaNmale
12983rd0Williams, Mr LeslieNaNNaNNaNNaNNaNNaNmale
12993rd0Windelov, Mr EinarNaNNaNNaNNaNNaNNaNmale
13003rd0Wirz, Mr AlbertNaNNaNNaNNaNNaNNaNmale
13013rd0Wiseman, Mr PhillippeNaNNaNNaNNaNNaNNaNmale
13023rd0Wittevrongel, Mr CamielNaNNaNNaNNaNNaNNaNmale
13033rd1Yalsevac, Mr IvanNaNNaNNaNNaNNaNNaNmale
13043rd0Yasbeck, Mr AntoniNaNNaNNaNNaNNaNNaNmale
13053rd1Yasbeck, Mrs AntoniNaNNaNNaNNaNNaNNaNfemale
13063rd0Youssef, Mr GeriosNaNNaNNaNNaNNaNNaNmale
13073rd0Zabour, Miss HileniNaNNaNNaNNaNNaNNaNfemale
13083rd0Zabour, Miss TaminiNaNNaNNaNNaNNaNNaNfemale
13093rd0Zakarian, Mr ArtunNaNNaNNaNNaNNaNNaNmale
13103rd0Zakarian, Mr MapriederNaNNaNNaNNaNNaNNaNmale
13113rd0Zenn, Mr PhilipNaNNaNNaNNaNNaNNaNmale
13123rd0Zievens, ReneNaNNaNNaNNaNNaNNaNfemale
13133rd0Zimmerman, LeoNaNNaNNaNNaNNaNNaNmale

1313 rows × 11 columns

data.describe() row.namessurvivedagecountmeanstdmin25%50%75%max
1313.0000001313.000000633.000000
657.0000000.34196531.194181
379.1747620.47454914.747525
1.0000000.0000000.166700
329.0000000.00000021.000000
657.0000000.00000030.000000
985.0000001.00000041.000000
1313.0000001.00000071.000000
# 2.數(shù)據(jù)基本處理 # 2.1 確定特征值,目標(biāo)值 x = data[["pclass", "age", "sex"]] x.head() pclassagesex01234
1st29.0000female
1st2.0000female
1st30.0000male
1st25.0000female
1st0.9167male
y = data["survived"] y.head() 0 1 1 0 2 0 3 0 4 1 Name: survived, dtype: int64 # 2.2 缺失值處理 x["age"].fillna(value=data["age"].mean(), inplace=True) x.head() pclassagesex01234
1st29.0000female
1st2.0000female
1st30.0000male
1st25.0000female
1st0.9167male
# 2.3 數(shù)據(jù)集劃分,test_size是測(cè)試集所占的比例 x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22, test_size=0.2) # 3.特征工程(字典特征抽取) x.head() pclassagesex01234
1st29.0000female
1st2.0000female
1st30.0000male
1st25.0000female
1st0.9167male
x_train = x_train.to_dict(orient="records") x_test = x_test.to_dict(orient="records") x_train [{'pclass': '3rd', 'age': 45.0, 'sex': 'female'},{'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},{'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'}, ........] transfer = DictVectorizer()x_train = transfer.fit_transform(x_train) x_test = transfer.fit_transform(x_test) x_train <1050x6 sparse matrix of type '<class 'numpy.float64'>'with 3150 stored elements in Compressed Sparse Row format> # 4.機(jī)器學(xué)習(xí)(決策樹) estimator = DecisionTreeClassifier(max_depth=5)estimator.fit(x_train, y_train) DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=5,max_features=None, max_leaf_nodes=None,min_impurity_decrease=0.0, min_impurity_split=None,min_samples_leaf=1, min_samples_split=2,min_weight_fraction_leaf=0.0, presort=False, random_state=None,splitter='best') # 5.模型評(píng)估 y_pre = estimator.predict(x_test) print(y_pre) [0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 1 0 1 0 0 00 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 01 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 10 1 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 11 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 00 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 00 0 1 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 00 1 0 1] ret = estimator.score(x_test, y_test) ret 0.7984790874524715 # 決策樹可視化 export_graphviz(estimator, out_file="./data/tree.dot", feature_names=['age', 'pclass=1st', 'pclass=2nd', 'pclass=3rd', '女性', '男性'])

總結(jié)

以上是生活随笔為你收集整理的决策树案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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