Titanic數(shù)據(jù)分析和建模

TITANIC DATA PREDICTATION

ok, 這是我自己做的titanic建模,主要過(guò)程是:
探索數(shù)據(jù):
看下數(shù)據(jù)維度,數(shù)據(jù)簡(jiǎn)單統(tǒng)計(jì)
數(shù)據(jù)類型(數(shù)值型,對(duì)象型)、
是否有空值,多少空值
用matplotlib繪圖探索數(shù)據(jù)
數(shù)據(jù)預(yù)處理:
填充空值數(shù)據(jù)
轉(zhuǎn)換分類變量
數(shù)據(jù)標(biāo)準(zhǔn)化,可在建模時(shí)轉(zhuǎn)換
feature很多時(shí)要降維 PCA SVD
數(shù)據(jù)建模:

模型評(píng)估:
分類:precision/recall ,f1 score, pr tradeoff圖
回歸: MAE/ MSE/ R平方

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline

import dataset

train = pd.read_csv("E:/figure/titanic/train.csv")
test = pd.read_csv("E:/figure/titanic/test.csv")
train.head()
Paste_Image.png

?Survived: Outcome of survival (0 = No; 1 = Yes)
?Pclass: Socio-economic class (1 = Upper class; 2 = Middle class; 3 = Lower class)
?Name: Name of passenger
?Sex: Sex of the passenger
?Age: Age of the passenger (Some entries contain NaN)
?SibSp: Number of siblings and spouses of the passenger aboard
?Parch: Number of parents and children of the passenger aboard
?Ticket: Ticket number of the passenger
?Fare: Fare paid by the passenger
?Cabin Cabin number of the passenger (Some entries contain NaN)
?Embarked: Port of embarkation of the passenger (C = Cherbourg; Q = Queenstown; S = Southampton)

explore dataset

print(train.shape)
print(test.shape)
(891, 12)
(418, 11)
train.dtypes
PassengerId      int64
Survived         int64
Pclass           int64
Name            object
Sex             object
Age            float64
SibSp            int64
Parch            int64
Ticket          object
Fare           float64
Cabin           object
Embarked        object
dtype: object
test.dtypes
PassengerId      int64
Pclass           int64
Name            object
Sex             object
Age            float64
SibSp            int64
Parch            int64
Ticket          object
Fare           float64
Cabin           object
Embarked        object
dtype: object
train.describe()
Paste_Image.png

merge training set and testing set

full =pd.merge(train,test,how="outer")
full.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1309 entries, 0 to 1308
Data columns (total 12 columns):
PassengerId    1309 non-null float64
Survived       891 non-null float64
Pclass         1309 non-null float64
Name           1309 non-null object
Sex            1309 non-null object
Age            1046 non-null float64
SibSp          1309 non-null float64
Parch          1309 non-null float64
Ticket         1309 non-null object
Fare           1308 non-null float64
Cabin          295 non-null object
Embarked       1307 non-null object
dtypes: float64(7), object(5)
memory usage: 107.4+ KB
#check missing value

full.isnull().sum()
PassengerId       0
Survived        418
Pclass            0
Name              0
Sex               0
Age             263
SibSp             0
Parch             0
Ticket            0
Fare              1
Cabin          1014
Embarked          2
dtype: int64
print(full["Pclass"].value_counts(),"\n",full["Sex"].value_counts(),"\n",full["Embarked"].value_counts())
3    709
1    323
2    277
Name: Pclass, dtype: int64 
 male      843
female    466
Name: Sex, dtype: int64 
 S    914
C    270
Q    123
Name: Embarked, dtype: int64

data preprocessing

#dummy variable
full=pd.get_dummies(full,columns=["Pclass","Embarked","Sex"])
#drop variable
full=full.drop(["Cabin","PassengerId","Name","Ticket"],axis=1)
full.dtypes
Survived      float64
Age           float64
SibSp         float64
Parch         float64
Fare          float64
Pclass_1.0    float64
Pclass_2.0    float64
Pclass_3.0    float64
Embarked_C    float64
Embarked_Q    float64
Embarked_S    float64
Sex_female    float64
Sex_male      float64
dtype: object
#replace "Age" missing value to -999
full["Age"].fillna(-999,inplace =True)
full["Age"]=full["Age"].astype(int)
full["Age"].hist(bins=10,range=(0,100))
<matplotlib.axes._subplots.AxesSubplot at 0x7ba3750>
Paste_Image.png
#replace nan value with mean
full["Fare"].loc[full["Fare"].isnull()]=full["Fare"].mean()
C:\Anaconda3\lib\site-packages\pandas\core\indexing.py:117: 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._setitem_with_indexer(indexer, value)
full.isnull().sum()
Survived      418
Age             0
SibSp           0
Parch           0
Fare            0
Pclass_1.0      0
Pclass_2.0      0
Pclass_3.0      0
Embarked_C      0
Embarked_Q      0
Embarked_S      0
Sex_female      0
Sex_male        0
dtype: int64
trainset =full.iloc[:891,]
testset=full.iloc[891:,]
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import f1_score
from sklearn.metrics import precision_recall_curve
x_train = trainset.drop("Survived",axis=1)
y_train = trainset["Survived"]
x_test = testset.drop("Survived",axis=1)
#logistic regression
logreg =LogisticRegression()
logreg.fit(x_train,y_train)
y_predlog = logreg.predict(x_test)
logreg.score(x_train,y_train)
0.7991021324354658

print f1 score and plot pr curve(it's on trainset)

f1_score(y_train,logreg.predict(x_train))
0.72248062015503878
prob = logreg.predict_proba(x_train)
precision, recall, thresholds = precision_recall_curve(y_train,prob[:,1])
plt.plot(precision,recall)
plt.xlabel("precision")
plt.ylabel("recall")
<matplotlib.text.Text at 0xbdc1f50>
Paste_Image.png
from sklearn.metrics import classification_report
print(classification_report(y_train,logreg.predict(x_train),target_names=["unsurvived","survived"]))
             precision    recall  f1-score   support

 unsurvived       0.81      0.87      0.84       549
   survived       0.77      0.68      0.72       342

avg / total       0.80      0.80      0.80       891
#SVC
svc =SVC()
svc.fit(x_train,y_train)
y_predsvc = svc.predict(x_test)
svc.score(x_train,y_train)
0.88776655443322106
#rrandom forest
rf = RandomForestClassifier()
rf.fit(x_train,y_train)
y_predrf = rf.predict(x_test)
rf.score(x_train,y_train)
0.97081930415263751

**feature importances

importanted = rf.feature_importances_
print(importanted)
plt.bar(range(x_train.shape[1]),importanted)
plt.xticks(range(x_train.shape[1]),tuple(x_train.columns),rotation=60)
[ 0.23063155  0.04861788  0.04546831  0.28729027  0.02962199  0.01930221
  0.03710312  0.00726902  0.00953395  0.0125742   0.18180419  0.09078331]





([<matplotlib.axis.XTick at 0xd1caed0>,
  <matplotlib.axis.XTick at 0xd1cafb0>,
  <matplotlib.axis.XTick at 0xd1c6210>,
  <matplotlib.axis.XTick at 0xd201630>,
  <matplotlib.axis.XTick at 0xd201bf0>,
  <matplotlib.axis.XTick at 0xd2061d0>,
  <matplotlib.axis.XTick at 0xd206790>,
  <matplotlib.axis.XTick at 0xd206d50>,
  <matplotlib.axis.XTick at 0xd209330>,
  <matplotlib.axis.XTick at 0xd2098f0>,
  <matplotlib.axis.XTick at 0xd209eb0>,
  <matplotlib.axis.XTick at 0xd20f490>],
 <a list of 12 Text xticklabel objects>)
Paste_Image.png

from the pic, you can see "age", "fare"and "sex" are more important variance.
There must be some relation between fare and Pclass, do that later.

#Gaussian NB
nb = GaussianNB()
nb.fit(x_train,y_train)
y_prednb=nb.predict(x_test)
nb.score(x_train,y_train)
0.78114478114478114
#standard 
from sklearn.preprocessing import StandardScaler
std = StandardScaler()
std.fit(x_train)
x_train_std = std.transform(x_train)
x_test_std = std.transform(x_test)
#train on this later
#save prediction as csv
#submission = pd.DataFrame({"PassengerId":test["PassengerId"],"Survived":y_predlog})
#submission.to_csv("titanicpred.csv",index=False)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,828評(píng)論 0 23
  • 思路 開(kāi)始的時(shí)候,我想的是將圖片直接加載到ImageView上,然后通過(guò) scrollTo()方法滑動(dòng)內(nèi)容,從而達(dá)...
    jacky123閱讀 944評(píng)論 0 0
  • 文│喬其 導(dǎo)語(yǔ):自我感覺(jué)行文方式有一丟丟“飛機(jī)壞品味櫻桃文”的味道。與之前喜歡的風(fēng)格好像不大一樣。現(xiàn)在覺(jué)得文字就該...
    叫我其其閱讀 321評(píng)論 0 0
  • 我是一只貓 露出了蜜汁微笑 你也可以叫我 蒙娜麗貓
    牛爸牛牛的爸爸閱讀 388評(píng)論 0 0
  • 我們都是上帝撒下的一粒種子,有了陽(yáng)光和雨露的滋潤(rùn),就會(huì)茁壯成長(zhǎng)。我們擁有健康的身體,能感受大自然的美麗與神奇...
    小冷小姐閱讀 343評(píng)論 0 2