0
2

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 1 year has passed since last update.

pandasをマスターするまでやめない第4話

Posted at

これまで

データクレンジングとは

破損したデータや不正確なデータ,無意味なデータを特定し最適な扱いをすることで予測する際や可視化する際に使いやすくすること.

重複行

重複している行があると特徴量に偏りが生まれるため消すことが必要.
df.duplicated()

df.duplicated() -> pd.Series

"""
0    False
1    True
2    False
3    False
4    False
....
n    False
"""

重複している行のカウント

df.duplicated().value_counts()
value_count()はbool型のSeriesの中にあるTrueの数の合計

重複している行の抽出

条件抽出とやり方は一緒

df_duplicated = df[df.duplicated()]

重複している行の削除

df.drop_dupulicated(inplace=False)

欠損値の処理

欠損値はnan(Not a Number)と呼ばれる.これがあると特徴量が足りず偏りが生まれる

欠損値の判定

df.isnull()
中身がbool型になり,nanならTrue値があればFalseとなる

nanのある列の判定

df.isnull().any()
any()はTrueのある列をTrueとしてcolumnsをindexとしたSeriesを返す

tf = df.isnull().any()
col_name = tf[tf].index.tolist()

各カラムのnan個数をカウント

df.isnull().sum()
nanではない個数の場合はdf.count()

各行にnanがあるかどうか

df.isnull().any(axis=1)

nanがある行の抽出

df_nan = df[df.isnull().any(axis=1)]

nanのある行を削除

df.dropna(inplace=False)

#### nanの補完
```df.fillna(object)```
```python
mean = df[columns].mean()
df[columns] = df[columns].fillna(mean) #平均で補完
df[columns] = df[columns].fillna(0)    #特定(0)の値で補完

nanの予測

missingpy.MissForest().fit_transform()

from missingpy import MissForest
cols = df[df.isnull().any()].columns.tolist() #予測したいcolumns
df = df[cols]
res  = MissForest().fit_transform(df) -> np.array

df_inpute = pd.Dataframe(res,columns=[cols])

エンコーディング

機械学習では文字列を数値に変換しなくてはいけない.

labelエンコーディング

sklearn.preprocessing.LabelEncoder

from sklearn.preprocessing import LabelEncoding
le = LabelEncoding() #インスタンス生成

le.fit(df[column]) #予測したいカラムで学習

df[new_column] = le.transform(df[column]) #ラベル->ラベルID

one-hotエンコーディング

pd.get_dummes(df)

おわり

ここら辺は無限にやり方あるし実際にkaggleとかでデータ加工の経験詰むのが一番よさそうあざした

0
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?