1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Titanicデータを前処理ライブラリDataLinerで処理してみる(変換編)

Posted at

はじめに

Pythonの前処理ライブラリDataLinerの各処理を紹介する記事3回目です。
今回は変換系について紹介したいと思います。

リリース記事:
https://qiita.com/shallowdf20/items/36727c9a18f5be365b37

インストール

! pip install -U dataliner

データ準備

いつも通りTitanicのデータを準備します。

import pandas as pd
import dataliner as dl

df = pd.read_csv('train.csv')
target_col = 'Survived'

X = df.drop(target_col, axis=1)
y = df[target_col]
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
1 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.250 NaN S
2 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0 PC 17599 71.283 C85 C
3 3 Heikkinen, Miss. Laina female 26 0 0 STON/O2. 3101282 7.925 NaN S
4 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35 1 0 113803 53.100 C123 S
5 3 Allen, Mr. William Henry male 35 0 0 373450 8.050 NaN S

StandardScaling / StandardizeData(deprecated)

データを平均0分散1となるよう変換します。
Sklearnなどのライブラリと違い、カテゴリ列が含まれていても自動で数値列のみを判別する他、pandas DataFrameで返却されるのでその後の処理が楽です。
StandardizeDataは名前をStandardScalingに変更したのでdeprecationの警告が出る他、ver.1.3.0では削除予定です。

trans = dl.StandardScaling() 
Xt = trans.fit_transform(X)
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
-1.729 0.8269 Braund, Mr. Owen Harris male -0.5300 0.4326 -0.4734 A/5 21171 -0.5022 NaN S
-1.725 -1.5652 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 0.5714 0.4326 -0.4734 PC 17599 0.7864 C85 C
-1.721 0.8269 Heikkinen, Miss. Laina female -0.2546 -0.4743 -0.4734 STON/O2. 3101282 -0.4886 NaN S
-1.717 -1.5652 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 0.3649 0.4326 -0.4734 113803 0.4205 C123 S
-1.714 0.8269 Allen, Mr. William Henry male 0.3649 -0.4743 -0.4734 373450 -0.4861 NaN S

MinMaxScaling

データを0と1の間に収まるよう変換をかけます。

trans = dl.MinMaxScaling() 
Xt = trans.fit_transform(X)
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
0.000000 1 Braund, Mr. Owen Harris male 0.2712 0.125 0 A/5 21171 0.01415 NaN S
0.001124 0 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 0.4722 0.125 0 PC 17599 0.13914 C85 C
0.002247 1 Heikkinen, Miss. Laina female 0.3214 0.000 0 STON/O2. 3101282 0.01547 NaN S
0.003371 0 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 0.4345 0.125 0 113803 0.10364 C123 S
0.004494 1 Allen, Mr. William Henry male 0.4345 0.000 0 373450 0.01571 NaN S

BinarizeNaN

欠損値が含まれる列を検索し、その列が欠損していたかどうかという2値の新しい列を作成します。

trans = dl.BinarizeNaN() 
Xt = trans.fit_transform(X)
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked Age_NaNFlag Cabin_NaNFlag Embarked_NaNFlag
1 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.250 NaN S 0 1 0
2 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0 PC 17599 71.283 C85 C 0 0 0
3 3 Heikkinen, Miss. Laina female 26 0 0 STON/O2. 3101282 7.925 NaN S 0 1 0
4 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35 1 0 113803 53.100 C123 S 0 0 0
5 3 Allen, Mr. William Henry male 35 0 0 373450 8.050 NaN S 0 1 0

CountRowNaN

各データ点(行)に対して、欠損値がいくつ含まれるかをカウントし、欠損値の合計を新しい特徴量として追加します。

trans = dl.CountRowNaN() 
Xt = trans.fit_transform(X)
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked NaN_Totals
1 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.250 NaN S 1
2 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0 PC 17599 71.283 C85 C 0
3 3 Heikkinen, Miss. Laina female 26 0 0 STON/O2. 3101282 7.925 NaN S 1
4 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35 1 0 113803 53.100 C123 S 0
5 3 Allen, Mr. William Henry male 35 0 0 373450 8.050 NaN S 1

ImputeNaN

欠損値を補完します。デフォルトの引数では数値列は平均、カテゴリ列は最頻値で補完するようになっています。
num_strategy及びcat_strategyで変更することが可能です。

trans = dl.ImputeNaN() 
Xt = trans.fit_transform(X)
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
1 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.250 B96 B98 S
2 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0 PC 17599 71.283 C85 C
3 3 Heikkinen, Miss. Laina female 26 0 0 STON/O2. 3101282 7.925 B96 B98 S
4 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35 1 0 113803 53.100 C123 S
5 3 Allen, Mr. William Henry male 35 0 0 373450 8.050 B96 B98 S

ClipData

X分位数を定義し、上限以上と下限以下のデータを上限値と下限値で置き換えます。
引数thresholdでどの程度でクリップするかど調整することができ、デフォルトでは1% : 99%となっています。

trans = dl.ClipData() 
Xt = trans.fit_transform(X)
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
9.9 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.250 NaN S
9.9 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0 PC 17599 71.283 C85 C
9.9 3 Heikkinen, Miss. Laina female 26 0 0 STON/O2. 3101282 7.925 NaN S
9.9 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35 1 0 113803 53.100 C123 S
9.9 3 Allen, Mr. William Henry male 35 0 0 373450 8.050 NaN S

GroupRareCategory

カテゴリ変数の中で、出現頻度の低いカテゴリをまとめて"RareCategory"という文字列で置き換えてしまいます。
カーディナリティを下げるのに役立ちます。OneHotEncodingをかける前に使用すると効果的です。
引数thresholdで、データ数の何%以下を置き換えるか変更することができます。デフォルトでは1%。

trans = dl.GroupRareCategory() 
Xt = trans.fit_transform(X)
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
1 3 RareCategory male 22 1 0 RareCategory 7.250 NaN S
2 1 RareCategory female 38 1 0 RareCategory 71.283 RareCategory C
3 3 RareCategory female 26 0 0 RareCategory 7.925 NaN S
4 1 RareCategory female 35 1 0 RareCategory 53.100 RareCategory S
5 3 RareCategory male 35 0 0 RareCategory 8.050 NaN S

おわりに

ということで今回はDataLinerの変換系の項目を紹介しました。
現時点(ver.1.1.6)で実装されている前処理としては、次回紹介するAppend系で最後となります。

Datalinerリリース記事: https://qiita.com/shallowdf20/items/36727c9a18f5be365b37
GitHub: https://github.com/shallowdf20/dataliner
PyPI: https://pypi.org/project/dataliner/

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?