特徵工程

Developing Machine Learning Models for Production

Sinan Ozdemir

Data Scientist and Author

特徵工程簡介

  • 轉換訓練資料以提升 ML 流程效能
  • 降低計算複雜度
  • 範例
    • 彙整多來源資料
    • 建構新特徵
    • 套用特徵轉換

feature engineering pipeline

1 https://www.manning.com/books/feature-engineering-bookcamp
Developing Machine Learning Models for Production

彙整多來源資料

  • 合併不同資料集的資料
  • 使用多種資料型態(如數值與類別)

這能幫助你:

  • 提升模型準確度
  • 能夠使用更複雜的模型

graphs on a screen

Developing Machine Learning Models for Production

資料彙整範例

class DataAggregator:
    def __init__(self):
        pass

    def fit(self, X, y=None):
        return self  # nothing to fit

    def transform(self, X, y=None):
        # Load data from multiple sources
        data1 = pd.read_csv('data1.csv')
        data2 = pd.read_csv('data2.csv')
        data3 = pd.read_csv('data3.csv')

        # Combine data from all sources (including X) into a single data frame
        aggregated_data = pd.concat([X, data1, data2, data3], axis=0)

        return aggregated_data  # Return aggregated data
Developing Machine Learning Models for Production

特徵建構

  • 由現有特徵產生新特徵
  • 從既有資料衍生新特徵
  • 改善模型效能
  • 提升模型可解釋性
Developing Machine Learning Models for Production

特徵建構範例

class FeatureConstructor:
    def __init__(self):
        pass

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        # Calculate the mean of each column in the data
        mean_values = X.mean()

        # Create new features based on the mean values
        X['mean_col1'] = X['col1'] - mean_values['col1']
        X['mean_col2'] = X['col2'] - mean_values['col2']

        return X  # Return the augmented data set
Developing Machine Learning Models for Production

特徵轉換

就地轉換既有特徵

  • 正規化資料分佈
  • 移除離群值
  • 提升模型準確與效能
```py
```py

Developing Machine Learning Models for Production

特徵選擇

從大量特徵中挑選子集,移除冗餘與不相關特徵

  • 降低過度擬合
  • 強化模型準確與效能
  • 提升模型可解釋性

feature selection

1 https://www.manning.com/books/feature-engineering-bookcamp
Developing Machine Learning Models for Production

特徵工程範例(續)

```py
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectKBest, chi2

from sklearn.pipeline import Pipeline

pipeline = Pipeline([  # Define feature engineering pipeline

    ('aggregate', DataAggregator()),  # Aggregate data from multiple sources
    ('construction', FeatureConstructor()),  # Feature Construction

    ('scaler', StandardScaler()),  # Feature Transformation

    ('select', SelectKBest(chi2, k=10)),  # Feature Selection
])
X_transformed = pipeline.fit_transform(X)  # Fit and transform data using pipeline
Developing Machine Learning Models for Production

深入學習特徵工程

fe book

Developing Machine Learning Models for Production

一起來練習吧!

Developing Machine Learning Models for Production

Preparing Video For Download...