Case classification decision tree algorithm

Import Data

#导人pandas用于数据分析。
import pandas as pd
#利用pandas的readcsv模块直接从互联网收集泰坦尼克号乘客数据。
titanic= pd.read_csv ('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')
#观察前几行数据,可以发现,数据种类各异,数值型.类别型,甚至还有缺失数据。
print(titanic.head())
   row.names pclass  survived  \
0          1    1st         1   
1          2    1st         0   
2          3    1st         0   
3          4    1st         0   
4          5    1st         1   

                                              name      age     embarked  \
0                     Allen, Miss Elisabeth Walton  29.0000  Southampton   
1                      Allison, Miss Helen Loraine   2.0000  Southampton   
2              Allison, Mr Hudson Joshua Creighton  30.0000  Southampton   
3  Allison, Mrs Hudson J.C. (Bessie Waldo Daniels)  25.0000  Southampton   
4                    Allison, Master Hudson Trevor   0.9167  Southampton   

                         home.dest room      ticket   boat     sex  
0                     St Louis, MO  B-5  24160 L221      2  female  
1  Montreal, PQ / Chesterville, ON  C26         NaN    NaN  female  
2  Montreal, PQ / Chesterville, ON  C26         NaN  (135)    male  
3  Montreal, PQ / Chesterville, ON  C26         NaN    NaN  female  
4  Montreal, PQ / Chesterville, ON  C22         NaN     11    male  
#使用pandas,数据都转人pandas独有的dataframe格式(二维数据表格),直接使用info() ,查看数据的统计特性。
titanic.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1313 entries, 0 to 1312
Data columns (total 11 columns):
row.names    1313 non-null int64
pclass       1313 non-null object
survived     1313 non-null int64
name         1313 non-null object
age          633 non-null float64
embarked     821 non-null object
home.dest    754 non-null object
room         77 non-null object
ticket       69 non-null object
boat         347 non-null object
sex          1313 non-null object
dtypes: float64(1), int64(2), object(8)
memory usage: 112.9+ KB

Information obtained: Total 1313 The passenger information data, and some characteristic data is complete (e.g. pclass, name), and some are missing; some numeric type, some are strings.

# 选择特征
x = titanic[['pclass', 'age', 'sex']]
y = titanic[ 'survived']
#对当前选择的特征进行探查。
x.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1313 entries, 0 to 1312
Data columns (total 3 columns):
pclass    1313 non-null object
age       633 non-null float64
sex       1313 non-null object
dtypes: float64(1), object(2)
memory usage: 30.9+ KB

Data preprocessing

  • The age data column, only 633, need-completion.
  • Pclass value sex and column are two categories of data type, it must be transformed into characteristic value, instead of by 0/1.
#首先我们补充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


C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py:5434: 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.cross_validation import train_test_split
x_train, x_test, y_train, y_test= train_test_split (x, y, test_size= 0.25,random_state = 33)
#使用scikit- learn. feature_ extraction中的特征转换器
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer (sparse= False)
#转换特征后,我们发现凡是类别型的特征都单独剥离出来,独成一列特征,数值型的则保持不变。
x_train = vec.fit_transform(x_train.to_dict(orient= 'record'))
print(vec.feature_names_)
['age', 'pclass=1st', 'pclass=2nd', 'pclass=3rd', 'sex=female', 'sex=male']
#同样需要对测试数据的特征进行转换。
x_test = vec.transform(x_test. to_dict (orient= 'record'))
#从sklearn.tree中导人决策树分类器。
from sklearn.tree import DecisionTreeClassifier
#使用默认配置初始化决策树分类器。
dtc = DecisionTreeClassifier()
#使用分割到的训练数据进行模型学习。
dtc.fit(x_train, y_train)
#用训练好的决策树模型对测试特征数据进行预测。
y_predict = dtc.predict(x_test)

Model Assessment

#从sklearn .metrics导人classification report.
from sklearn.metrics import classification_report
#输出预测准确性。
print(dtc.score(x_test, y_test))
# 输出更加详细的分类性能。
print(classification_report (y_predict, y_test, target_names= ['died','survived']))
0.7811550151975684
             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

The overall predictive accuracy of the decision tree model on the test set of about 78.12%. Detailed performance metrics further explained that the model in predicting the performance of victims; they need to accurately identify survivors
poor rate.

Published 58 original articles · won praise 77 · views 90000 +

Guess you like

Origin blog.csdn.net/weixin_41503009/article/details/104277257