LoginSignup
2
2

More than 3 years have passed since last update.

【備忘録】タイタニック号のデータを完璧に前処理したい

Last updated at Posted at 2020-12-22

目的

Python初心者でタイタニック号の生死判定の宿題が出ましたので
前処理を完璧にするにはどうしたらいいんだろう・・・?という
自己満足の備忘録です。

与えられたデータ

元のデータをDataFrameに入れるとこんな感じです。

PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S
... ... ... ... ... ... ... ... ... ... ... ...
891 0 3 Dooley, Mr. Patrick male 32.0 0 0 370376 7.7500 NaN Q

ちょっと書くのめんどくさいんで最初と最後だけ。
皆が知ってるタイタニック号の学習用データです。
df.info()で情報を見ます。

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
 #   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  
 0   PassengerId  891 non-null    int64  
 1   Survived     891 non-null    int64  
 2   Pclass       891 non-null    int64  
 3   Name         891 non-null    object 
 4   Sex          891 non-null    object 
 5   Age          714 non-null    float64
 6   SibSp        891 non-null    int64  
 7   Parch        891 non-null    int64  
 8   Ticket       891 non-null    object 
 9   Fare         891 non-null    float64
 10  Cabin        204 non-null    object 
 11  Embarked     889 non-null    object 
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB

全データは12×891、Age,Cabin,Embarkedで欠損があるのと、
Name,Sex,Cabin,EmbarkedはObject(文字列)なのでこのままでは使えません。

SexとEmbarkedをObjectから数値に

たぶん全員やってるであろう処理です。
今回はこれをやってからランダムフォレストにかけて、それを基準にしたいと思います。

# Sex を数値化
df['Sex'] = df['Sex'].map({'male':0, 'female':1})
# Embarked を数値化
df['Embarked'] = df['Embarked'].map({'S':0, 'C':1, 'Q':2})
# Age は欠損値を埋めるため、とりあえずを平均値で補完 
df['Age'] = df['Age'].fillna(df['Age'].mean())
# Embarked はとりあえず一番多いS(0)で補完
df['Embarked'] = df['Embarked'].fillna(0)

# ObjectのままのName,Ticket,Cabinはとりあえずなしでデータを作成
df_data = df[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]
df_label = df[['Survived']]

(train_data, test_data, train_label, test_label) = tts(df_data, df_label, test_size=0.3, random_state=0)
# train_test_split=ttsとしてます

clf = RFC() # RandomForestClassifier=RFCとしてます
clf.fit(train_data, train_label)
clf.score(test_data, test_label)

今回データの前処理だけのビフォーアフターが見たいので
パラメータとかはdefaultのままです。

結果は・・・

0.8134328358208955

ちょっと基準が高かったです。。
これ以上高みを目指せるのでしょうか。。。

Ageの欠損値を埋めたい

年齢で生死が分かれるかというとそうじゃないと信じたいですが
とりあえず何かのデータで大体の年齢がわかったら嬉しいなと思いました。

まず既知のAgeとの相関関係を見てみる

dropnaでAgeの欠損値を抜いたものを、df.corr()で相関係数を見てみる。

PassengerId Survived Pclass Sex Age SibSp Parch Fare Embarked
Age 0.033207 -0.069809 -0.331339 -0.084153 1.000000 -0.232625 -0.179191 0.091566 0.007461

こう見るとPclass,SibSp,Parchが相関高そう。

途中まで・・・

感想

・記事の書き方もプログラマ仕様というか、Wordのような簡単なものではなかったので
 まず書き方を調べるところから始めた。
・コードは普段自分が適当に書いていたりするので、わかりやすいコード記述というのが結構難しかった。
・M-1優勝嬉しい。

追記(2020/12/24)

クリスマスイブだけど!
続きを書きました!→続き←

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2