零基礎入門數據分析 task3

Datawhale 零基礎入門數據挖掘-Task3 特徵工程¶

三、 特徵工程目標

Tip:此部分爲零基礎入門數據挖掘的 Task3 特徵工程 部分,帶你來了解各種特徵工程以及分析方法,歡迎大家後續多多交流。

賽題:零基礎入門數據挖掘 - 二手車交易價格預測

地址:https://tianchi.aliyun.com/competition/entrance/231784/introduction?spm=5176.12281957.1004.1.38b02448ausjSX

3.1 特徵工程目標

對於特徵進行進一步分析,並對於數據進行處理

完成對於特徵工程的分析,並對於數據進行一些圖表或者文字總結並打卡。

3.2 內容介紹

常見的特徵工程包括:

  1. 異常處理:
    • 通過箱線圖(或 3-Sigma)分析刪除異常值;
    • BOX-COX 轉換(處理有偏分佈);
    • 長尾截斷;
  2. 特徵歸一化/標準化:
    • 標準化(轉換爲標準正態分佈);
    • 歸一化(抓換到 [0,1] 區間);
    • 針對冪律分佈,可以採用公式: log(1+x1+median)log(1+x1+median)
  3. 數據分桶:
    • 等頻分桶;
    • 等距分桶;
    • Best-KS 分桶(類似利用基尼指數進行二分類);
    • 卡方分桶;
  4. 缺失值處理:
    • 不處理(針對類似 XGBoost 等樹模型);
    • 刪除(缺失數據太多);
    • 插值補全,包括均值/中位數/衆數/建模預測/多重插補/壓縮感知補全/矩陣補全等;
    • 分箱,缺失值一個箱;
  5. 特徵構造:
    • 構造統計量特徵,報告計數、求和、比例、標準差等;
    • 時間特徵,包括相對時間和絕對時間,節假日,雙休日等;
    • 地理信息,包括分箱,分佈編碼等方法;
    • 非線性變換,包括 log/ 平方/ 根號等;
    • 特徵組合,特徵交叉;
    • 仁者見仁,智者見智。
  6. 特徵篩選
    • 過濾式(filter):先對數據進行特徵選擇,然後在訓練學習器,常見的方法有 Relief/方差選擇發/相關係數法/卡方檢驗法/互信息法;
    • 包裹式(wrapper):直接把最終將要使用的學習器的性能作爲特徵子集的評價準則,常見方法有 LVM(Las Vegas Wrapper) ;
    • 嵌入式(embedding):結合過濾式和包裹式,學習器訓練過程中自動進行了特徵選擇,常見的有 lasso 迴歸;
  7. 降維
    • PCA/ LDA/ ICA;
    • 特徵選擇也是一種降維。

3.3 代碼示例

3.3.0 導入數據

 

1

import pandas as pd

2

import numpy as np

3

import matplotlib

4

import matplotlib.pyplot as plt

5

import seaborn as sns

6

from operator import itemgetter

7

8

%matplotlib inline

 

1

path = './datalab/231784/'

2

Train_data = pd.read_csv(path+'used_car_train_20200313.csv', sep=' ')

3

Test_data = pd.read_csv(path+'used_car_testA_20200313.csv', sep=' ')

4

print(Train_data.shape)

5

print(Test_data.shape)
(150000, 31)
(50000, 30)

 

1

Train_data.head()

[33]:

  SaleID name regDate model brand bodyType fuelType gearbox power kilometer ... v_5 v_6 v_7 v_8 v_9 v_10 v_11 v_12 v_13 v_14
0 0 736 20040402 30.0 6 1.0 0.0 0.0 60 12.5 ... 0.235676 0.101988 0.129549 0.022816 0.097462 -2.881803 2.804097 -2.420821 0.795292 0.914762
1 1 2262 20030301 40.0 1 2.0 0.0 0.0 0 15.0 ... 0.264777 0.121004 0.135731 0.026597 0.020582 -4.900482 2.096338 -1.030483 -1.722674 0.245522
2 2 14874 20040403 115.0 15 1.0 0.0 0.0 163 12.5 ... 0.251410 0.114912 0.165147 0.062173 0.027075 -4.846749 1.803559 1.565330 -0.832687 -0.229963
3 3 71865 19960908 109.0 10 0.0 0.0 1.0 193 15.0 ... 0.274293 0.110300 0.121964 0.033395 0.000000 -4.509599 1.285940 -0.501868 -2.438353 -0.478699
4 4 111080 20120103 110.0 5 1.0 0.0 0.0 68 5.0 ... 0.228036 0.073205 0.091880 0.078819 0.121534 -1.896240 0.910783 0.931110 2.834518 1.923482

5 rows × 31 columns

 

1

Train_data.columns

[34]:

Index(['SaleID', 'name', 'regDate', 'model', 'brand', 'bodyType', 'fuelType',
       'gearbox', 'power', 'kilometer', 'notRepairedDamage', 'regionCode',
       'seller', 'offerType', 'creatDate', 'price', 'v_0', 'v_1', 'v_2', 'v_3',
       'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'v_10', 'v_11', 'v_12',
       'v_13', 'v_14'],
      dtype='object')

 

1

Test_data.columns

[35]:

Index(['SaleID', 'name', 'regDate', 'model', 'brand', 'bodyType', 'fuelType',
       'gearbox', 'power', 'kilometer', 'notRepairedDamage', 'regionCode',
       'seller', 'offerType', 'creatDate', 'v_0', 'v_1', 'v_2', 'v_3', 'v_4',
       'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'v_10', 'v_11', 'v_12', 'v_13',
       'v_14'],
      dtype='object')

3.3.1 刪除異常值

 

1

# 這裏我包裝了一個異常值處理的代碼,可以隨便調用。

2

def outliers_proc(data, col_name, scale=3):

3

    """

4

    用於清洗異常值,默認用 box_plot(scale=3)進行清洗

5

    :param data: 接收 pandas 數據格式

6

    :param col_name: pandas 列名

7

    :param scale: 尺度

8

    :return:

9

    """

10

11

    def box_plot_outliers(data_ser, box_scale):

12

        """

13

        利用箱線圖去除異常值

14

        :param data_ser: 接收 pandas.Series 數據格式

15

        :param box_scale: 箱線圖尺度,

16

        :return:

17

        """

18

        iqr = box_scale * (data_ser.quantile(0.75) - data_ser.quantile(0.25))

19

        val_low = data_ser.quantile(0.25) - iqr

20

        val_up = data_ser.quantile(0.75) + iqr

21

        rule_low = (data_ser < val_low)

22

        rule_up = (data_ser > val_up)

23

        return (rule_low, rule_up), (val_low, val_up)

24

25

    data_n = data.copy()

26

    data_series = data_n[col_name]

27

    rule, value = box_plot_outliers(data_series, box_scale=scale)

28

    index = np.arange(data_series.shape[0])[rule[0] | rule[1]]

29

    print("Delete number is: {}".format(len(index)))

30

    data_n = data_n.drop(index)

31

    data_n.reset_index(drop=True, inplace=True)

32

    print("Now column number is: {}".format(data_n.shape[0]))

33

    index_low = np.arange(data_series.shape[0])[rule[0]]

34

    outliers = data_series.iloc[index_low]

35

    print("Description of data less than the lower bound is:")

36

    print(pd.Series(outliers).describe())

37

    index_up = np.arange(data_series.shape[0])[rule[1]]

38

    outliers = data_series.iloc[index_up]

39

    print("Description of data larger than the upper bound is:")

40

    print(pd.Series(outliers).describe())

41

    

42

    fig, ax = plt.subplots(1, 2, figsize=(10, 7))

43

    sns.boxplot(y=data[col_name], data=data, palette="Set1", ax=ax[0])

44

    sns.boxplot(y=data_n[col_name], data=data_n, palette="Set1", ax=ax[1])

45

    return data_n

 

1

# 我們可以刪掉一些異常數據,以 power 爲例。  

2

# 這裏刪不刪同學可以自行判斷

3

# 但是要注意 test 的數據不能刪 = = 不能掩耳盜鈴是不是

4

5

Train_data = outliers_proc(Train_data, 'power', scale=3)
Delete number is: 963
Now column number is: 149037
Description of data less than the lower bound is:
count    0.0
mean     NaN
std      NaN
min      NaN
25%      NaN
50%      NaN
75%      NaN
max      NaN
Name: power, dtype: float64
Description of data larger than the upper bound is:
count      963.000000
mean       846.836968
std       1929.418081
min        376.000000
25%        400.000000
50%        436.000000
75%        514.000000
max      19312.000000
Name: power, dtype: float64

3.3.2 特徵構造

 

1

# 訓練集和測試集放在一起,方便構造特徵

2

Train_data['train']=1

3

Test_data['train']=0

4

data = pd.concat([Train_data, Test_data], ignore_index=True)

 

1

# 使用時間:data['creatDate'] - data['regDate'],反應汽車使用時間,一般來說價格與使用時間成反比

2

# 不過要注意,數據裏有時間出錯的格式,所以我們需要 errors='coerce'

3

data['used_time'] = (pd.to_datetime(data['creatDate'], format='%Y%m%d', errors='coerce') - 

4

                            pd.to_datetime(data['regDate'], format='%Y%m%d', errors='coerce')).dt.days

 

1

# 看一下空數據,有 15k 個樣本的時間是有問題的,我們可以選擇刪除,也可以選擇放着。

2

# 但是這裏不建議刪除,因爲刪除缺失數據佔總樣本量過大,7.5%

3

# 我們可以先放着,因爲如果我們 XGBoost 之類的決策樹,其本身就能處理缺失值,所以可以不用管;

4

data['used_time'].isnull().sum()

[40]:

15072

 

1

# 從郵編中提取城市信息,相當於加入了先驗知識

2

data['city'] = data['regionCode'].apply(lambda x : str(x)[:-3])

3

data = data

 

1

# 計算某品牌的銷售統計量,同學們還可以計算其他特徵的統計量

2

# 這裏要以 train 的數據計算統計量

3

Train_gb = Train_data.groupby("brand")

4

all_info = {}

5

for kind, kind_data in Train_gb:

6

    info = {}

7

    kind_data = kind_data[kind_data['price'] > 0]

8

    info['brand_amount'] = len(kind_data)

9

    info['brand_price_max'] = kind_data.price.max()

10

    info['brand_price_median'] = kind_data.price.median()

11

    info['brand_price_min'] = kind_data.price.min()

12

    info['brand_price_sum'] = kind_data.price.sum()

13

    info['brand_price_std'] = kind_data.price.std()

14

    info['brand_price_average'] = round(kind_data.price.sum() / (len(kind_data) + 1), 2)

15

    all_info[kind] = info

16

brand_fe = pd.DataFrame(all_info).T.reset_index().rename(columns={"index": "brand"})

17

data = data.merge(brand_fe, how='left', on='brand')

 

1

# 數據分桶 以 power 爲例

2

# 這時候我們的缺失值也進桶了,

3

# 爲什麼要做數據分桶呢,原因有很多,= =

4

# 1. 離散後稀疏向量內積乘法運算速度更快,計算結果也方便存儲,容易擴展;

5

# 2. 離散後的特徵對異常值更具魯棒性,如 age>30 爲 1 否則爲 0,對於年齡爲 200 的也不會對模型造成很大的干擾;

6

# 3. LR 屬於廣義線性模型,表達能力有限,經過離散化後,每個變量有單獨的權重,這相當於引入了非線性,能夠提升模型的表達能力,加大擬合;

7

# 4. 離散後特徵可以進行特徵交叉,提升表達能力,由 M+N 個變量編程 M*N 個變量,進一步引入非線形,提升了表達能力;

8

# 5. 特徵離散後模型更穩定,如用戶年齡區間,不會因爲用戶年齡長了一歲就變化

9

10

# 當然還有很多原因,LightGBM 在改進 XGBoost 時就增加了數據分桶,增強了模型的泛化性

11

12

bin = [i*10 for i in range(31)]

13

data['power_bin'] = pd.cut(data['power'], bin, labels=False)

14

data[['power_bin', 'power']].head()

[43]:

  power_bin power
0 5.0 60
1 NaN 0
2 16.0 163
3 19.0 193
4 6.0 68

 

1

# 刪除不需要的數據

2

data = data.drop(['creatDate', 'regDate', 'regionCode'], axis=1)

 

1

print(data.shape)

2

data.columns
(199037, 39)

[45]:

Index(['SaleID', 'bodyType', 'brand', 'fuelType', 'gearbox', 'kilometer',
       'model', 'name', 'notRepairedDamage', 'offerType', 'power', 'price',
       'seller', 'train', 'v_0', 'v_1', 'v_10', 'v_11', 'v_12', 'v_13', 'v_14',
       'v_2', 'v_3', 'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'used_time',
       'city', 'brand_amount', 'brand_price_average', 'brand_price_max',
       'brand_price_median', 'brand_price_min', 'brand_price_std',
       'brand_price_sum', 'power_bin'],
      dtype='object')

 

1

# 目前的數據其實已經可以給樹模型使用了,所以我們導出一下

2

data.to_csv('data_for_tree.csv', index=0)

 

1

# 我們可以再構造一份特徵給 LR NN 之類的模型用

2

# 之所以分開構造是因爲,不同模型對數據集的要求不同

3

# 我們看下數據分佈:

4

data['power'].plot.hist()

[47]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fe026cf0c18>

 

1

# 我們剛剛已經對 train 進行異常值處理了,但是現在還有這麼奇怪的分佈是因爲 test 中的 power 異常值,

2

# 所以我們其實剛剛 train 中的 power 異常值不刪爲好,可以用長尾分佈截斷來代替

3

Train_data['power'].plot.hist()

[48]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fe02d7a8eb8>

 

1

# 我們對其取 log,在做歸一化

2

from sklearn import preprocessing

3

min_max_scaler = preprocessing.MinMaxScaler()

4

data['power'] = np.log(data['power'] + 1) 

5

data['power'] = ((data['power'] - np.min(data['power'])) / (np.max(data['power']) - np.min(data['power'])))

6

data['power'].plot.hist()

[49]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fe026c342b0>

 

1

# km 的比較正常,應該是已經做過分桶了

2

data['kilometer'].plot.hist()

[50]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fe029ad2080>

 

1

# 所以我們可以直接做歸一化

2

data['kilometer'] = ((data['kilometer'] - np.min(data['kilometer'])) / 

3

                        (np.max(data['kilometer']) - np.min(data['kilometer'])))

4

data['kilometer'].plot.hist()

[51]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fe029b1d668>

 

1

# 除此之外 還有我們剛剛構造的統計量特徵:

2

# 'brand_amount', 'brand_price_average', 'brand_price_max',

3

# 'brand_price_median', 'brand_price_min', 'brand_price_std',

4

# 'brand_price_sum'

5

# 這裏不再一一舉例分析了,直接做變換,

6

def max_min(x):

7

    return (x - np.min(x)) / (np.max(x) - np.min(x))

8

9

data['brand_amount'] = ((data['brand_amount'] - np.min(data['brand_amount'])) / 

10

                        (np.max(data['brand_amount']) - np.min(data['brand_amount'])))

11

data['brand_price_average'] = ((data['brand_price_average'] - np.min(data['brand_price_average'])) / 

12

                               (np.max(data['brand_price_average']) - np.min(data['brand_price_average'])))

13

data['brand_price_max'] = ((data['brand_price_max'] - np.min(data['brand_price_max'])) / 

14

                           (np.max(data['brand_price_max']) - np.min(data['brand_price_max'])))

15

data['brand_price_median'] = ((data['brand_price_median'] - np.min(data['brand_price_median'])) /

16

                              (np.max(data['brand_price_median']) - np.min(data['brand_price_median'])))

17

data['brand_price_min'] = ((data['brand_price_min'] - np.min(data['brand_price_min'])) / 

18

                           (np.max(data['brand_price_min']) - np.min(data['brand_price_min'])))

19

data['brand_price_std'] = ((data['brand_price_std'] - np.min(data['brand_price_std'])) / 

20

                           (np.max(data['brand_price_std']) - np.min(data['brand_price_std'])))

21

data['brand_price_sum'] = ((data['brand_price_sum'] - np.min(data['brand_price_sum'])) / 

22

                           (np.max(data['brand_price_sum']) - np.min(data['brand_price_sum'])))

 

1

# 對類別特徵進行 OneEncoder

2

data = pd.get_dummies(data, columns=['model', 'brand', 'bodyType', 'fuelType',

3

                                     'gearbox', 'notRepairedDamage', 'power_bin'])

 

1

print(data.shape)

2

data.columns
(199037, 370)

[54]:

Index(['SaleID', 'kilometer', 'name', 'offerType', 'power', 'price', 'seller',
       'train', 'v_0', 'v_1',
       ...
       'power_bin_20.0', 'power_bin_21.0', 'power_bin_22.0', 'power_bin_23.0',
       'power_bin_24.0', 'power_bin_25.0', 'power_bin_26.0', 'power_bin_27.0',
       'power_bin_28.0', 'power_bin_29.0'],
      dtype='object', length=370)

 

1

# 這份數據可以給 LR 用

2

data.to_csv('data_for_lr.csv', index=0)

3.3.3 特徵篩選

1) 過濾式

 

1

# 相關性分析

2

print(data['power'].corr(data['price'], method='spearman'))

3

print(data['kilometer'].corr(data['price'], method='spearman'))

4

print(data['brand_amount'].corr(data['price'], method='spearman'))

5

print(data['brand_price_average'].corr(data['price'], method='spearman'))

6

print(data['brand_price_max'].corr(data['price'], method='spearman'))

7

print(data['brand_price_median'].corr(data['price'], method='spearman'))
0.572828519605
-0.408256970162
0.0581566100256
0.383490957606
0.259066833881
0.386910423934

 

1

# 當然也可以直接看圖

2

data_numeric = data[['power', 'kilometer', 'brand_amount', 'brand_price_average', 

3

                     'brand_price_max', 'brand_price_median']]

4

correlation = data_numeric.corr()

5

6

f , ax = plt.subplots(figsize = (7, 7))

7

plt.title('Correlation of Numeric Features with Price',y=1,size=16)

8

sns.heatmap(correlation,square = True,  vmax=0.8)

[57]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fe0283a6dd8>

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