ML 決策樹 鐵達尼號

2021-10-24 13:42:19 字數 3410 閱讀 9956

1.資料載入

2.資料特徵

3.資料預處理:均值填充,最大值填充

4.特徵選擇

5.構造id3樹

6.決策樹訓練

7.決策樹**

8.準確率輸出

import pandas as pd

from sklearn.feature_extraction import dictvectorizer

from sklearn.tree import decisiontreeclassifier

# 資料載入

train_data = pd.read_csv(

'c:\\users\\yxj\\documents\\tencent files\\1064584707\\filerecv\\titanic_data-master\\train.csv'

)test_data = pd.read_csv(

'c:\\users\\yxj\\documents\\tencent files\\1064584707\\filerecv\\titanic_data-master\\train.csv'

)# 資料探索

print

(train_data.info())

#資料資訊

print

('-'*30

)print

(train_data.describe())

#資料描述

print

('-'*30

)print

(train_data.describe(include=

['o'])

)print

('-'*30

)print

(train_data.head())

#前五條資料

print

('-'*30

)print

(train_data.tail())

#後五條資料

print

('-'*30

)# 資料清洗

# 使用平均年齡來填充年齡中的 nan 值

train_data[

'age'

].fillna(train_data[

'age'

].mean(

), inplace=

true

)test_data[

'age'

].fillna(test_data[

'age'

].mean(

),inplace=

true

)# 使用票價的均值填充票價中的 nan 值

train_data[

'fare'

].fillna(train_data[

'fare'

].mean(

), inplace=

true

)test_data[

'fare'

].fillna(test_data[

'fare'

].mean(

),inplace=

true

)print

(train_data[

'embarked'

].value_counts())

# 使用登入最多的港口來填充登入港口的 nan 值

train_data[

'embarked'

].fillna(

's', inplace=

true

)test_data[

'embarked'

].fillna(

's',inplace=

true

)# 特徵選擇

features =

['pclass'

,'***'

,'age'

,'sibsp'

,'parch'

,'fare'

,'embarked'

]train_features = train_data[features]

train_labels = train_data[

'survived'

]test_features = test_data[features]

#特徵向量化

#將dict型別的list資料,轉換成numpy array,具有屬性vec.feature_names_,檢視提取後的特徵名。

#pclass和***兩列分類變數轉換為了數值型變數(只有0和1),age列數值型保持不變

dvec=dictvectorizer(sparse=

false

)#sparse=false意思是不產生稀疏矩陣

train_features=dvec.fit_transform(train_features.to_dict(orient=

'record'))

#特徵名字

print

(dvec.feature_names_)

# 構造 id3 決策樹

clf = decisiontreeclassifier(criterion=

'entropy'

)# 決策樹訓練

clf.fit(train_features, train_labels)

test_features=dvec.transform(test_features.to_dict(orient=

'record'))

# 決策樹**

pred_labels = clf.predict(test_features)

# 得到決策樹準確率

acc_decision_tree =

round

(clf.score(train_features, train_labels),6

)print

(u'score 準確率為 %.4lf'

% acc_decision_tree)

#0.9820

##交叉驗證

```python

import numpy as np

from sklearn.model_selection import cross_val_score

#使用k折交叉驗證,統計決策樹準確率

score=cross_val_score(clf,train_features,train_labels,cv=10)

print

(score)

print

(u'score 準確率為 %.4lf'

% np.mean(score)

)#score 準確率為 0.7801

採用決策樹方法,對鐵達尼號乘客生存問題進行**,準確率達0.9820,使用k折交叉驗證後準確率為0.7801

經典案例 鐵達尼號

import pandas as pd import graphviz 1.讀取資料,獲取特徵值 data pd.read excel r tietan.xls print data 2.pclass,age,feature data.loc pclass age print feature.inf...

Kaggle滑水 泰坦尼克之災(決策樹)

本文以kaggle新手習題 titanic machine learning from disaster為場景開展實驗,以此熟悉kaggle平台。本文的原始碼託管於我的github practice of machine learning code kaggle titanic 歡迎檢視交流。tit...

用Python分析經典鐵達尼號專案

一共有891個樣本 survived的標籤是通過0或1來區分 大概38 的樣本是survived 大多數乘客 76 沒有與父母或是孩子一起旅行 大約30 的乘客有親屬和 或配偶一起登船 票價的差別非常大,少量的乘客 1 付了高達 512的費用 很少的乘客 1 年紀在64 80之間 import pa...