您的位置:首页 > 编程语言 > Python开发

Python 决策树 泰坦尼克号乘客是否生还决策模型

2017-12-20 22:51 218 查看
与网上的其他内容均一样

import pandas as pd
titanic = pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')


titanic.head()


.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
}

.dataframe tbody tr th {
vertical-align: top;
}

row.namespclasssurvivednameageembarkedhome.destroomticketboatsex
011st1Allen, Miss Elisabeth Walton29.0000SouthamptonSt Louis, MOB-524160 L2212female
121st0Allison, Miss Helen Loraine2.0000SouthamptonMontreal, PQ / Chesterville, ONC26NaNNaNfemale
231st0Allison, Mr Hudson Joshua Creighton30.0000SouthamptonMontreal, PQ / Chesterville, ONC26NaN(135)male
341st0Allison, Mrs Hudson J.C. (Bessie Waldo Daniels)25.0000SouthamptonMontreal, PQ / Chesterville, ONC26NaNNaNfemale
451st1Allison, Master Hudson Trevor0.9167SouthamptonMontreal, PQ / Chesterville, ONC22NaN11male
titanic.info()


X = titanic[['pclass', 'age', 'sex']]
y = titanic['survived']


X.info()
X.head()


# 使用均值对AGE进行插值
X['age'].fillna(X['age'].mean(), inplace=True)
X.info()


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1313 entries, 0 to 1312
Data columns (total 3 columns):
pclass    1313 non-null object
age       1313 non-null float64
sex       1313 non-null object
dtypes: float64(1), object(2)
memory usage: 30.9+ KB

D:\Program Files\Anaconda35\lib\site-packages\pandas\core\generic.py:3660: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self._update_inplace(new_data)


# 数据分割
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=33)
X_train.head()
X_train.info()


<class 'pandas.core.frame.DataFrame'>
Int64Index: 984 entries, 1086 to 1044
Data columns (total 3 columns):
pclass    984 non-null object
age       984 non-null float64
sex       984 non-null object
dtypes: float64(1), object(2)
memory usage: 30.8+ KB


from sklearn.feature_extraction import DictVectorizer


# 原文 vec = DictVectorizer(sparse=False) 报错
vec = DictVectorizer()

X_train = vec.fit_transform(X_train.to_dict(orient = 'record'))
X_test = vec.transform(X_test.to_dict(orient = 'record'))
#X_train.to_dict(orient='record')
vec.feature_names_


['age', 'pclass=1st', 'pclass=2nd', 'pclass=3rd', 'sex=female', 'sex=male']


from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier()
dtc.fit(X_train, y_train)
y_predict = dtc.predict(X_test)


from sklearn.metrics import classification_report
print(dtc.score(X_test, y_test))


0.781155015198


print(classification_report(y_predict, y_test, target_names=['died', 'survived']))


precision    recall  f1-score   support

died       0.91      0.78      0.84       236
survived       0.58      0.80      0.67        93

avg / total       0.81      0.78      0.79       329
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python