python 數據預處理

1.缺失值填充

import numpy as np
import pandas as pd

df = pd.DataFrame({'a': [3, 1, 3, 2, 4, 3, 2, 4, 3],
                   'b': [4, 6, np.nan, 6, 2, 7, np.nan, 3, 5],
                   'c': [np.nan, 8, 2, 4, np.nan, 7, 6, 3, 5]})
print(df)
df['b'].fillna(df['b'].mean(), inplace=True)  # 均值填充
df['c'].fillna(df['c'].median(), inplace=True)  # 中位數填充
print(df)

2.oneHot編碼

import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelEncoder

df = pd.DataFrame({'a': [4, 2, 2, 1, 3, 2, 4, 2, 1],
                   'b': ['a', 'd', 'c', 'b', 'b', 'a', 'a', 'd', 'b']})
print(df)

b = LabelEncoder().fit_transform(df['b'])
b_onehot = pd.DataFrame(OneHotEncoder(sparse=False).fit_transform(b.reshape(len(b), 1)))
df = pd.concat([df, b_onehot], ignore_index=True, axis=1)
print(df)

3.數據歸一化

import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler

df = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6, 7, 8, 9],
                   'b': [4, 3, 5, 3, 2, 4, 2, 1, 5]})
scaler = StandardScaler()
print(scaler.fit_transform(pd.DataFrame(df['a'])))

min_max_scaler = MinMaxScaler()
print(min_max_scaler.fit_transform(pd.DataFrame(df['a'])))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章