Feature engineering

การพัฒนาโมเดล Machine Learning สำหรับ Production

Sinan Ozdemir

Data Scientist and Author

แนะนำ feature engineering

  • แปลงข้อมูลสำหรับฝึกโมเดลเพื่อเพิ่มประสิทธิภาพ ML pipeline
  • ลดความซับซ้อนในการคำนวณ
  • ตัวอย่าง
    • รวมข้อมูลจากหลายแหล่ง
    • สร้าง feature ใหม่
    • ใช้การแปลง feature

feature engineering pipeline

1 https://www.manning.com/books/feature-engineering-bookcamp
การพัฒนาโมเดล Machine Learning สำหรับ Production

การรวมข้อมูลจากหลายแหล่ง

  • รวมข้อมูลจากชุดข้อมูลต่าง ๆ
  • ใช้ข้อมูลหลายประเภท (เช่น ตัวเลขและหมวดหมู่)

ประโยชน์ที่ได้รับ:

  • เพิ่มความแม่นยำของโมเดล
  • รองรับโมเดลที่ซับซ้อนยิ่งขึ้น

กราฟบนหน้าจอ

การพัฒนาโมเดล Machine Learning สำหรับ 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
การพัฒนาโมเดล Machine Learning สำหรับ Production

Feature construction

  • สร้าง feature ใหม่จาก feature ที่มีอยู่
  • สร้าง feature เพิ่มเติมจากข้อมูลเดิม
  • เพิ่มประสิทธิภาพของโมเดล
  • ทำให้โมเดลตีความได้ง่ายขึ้น
การพัฒนาโมเดล Machine Learning สำหรับ Production

ตัวอย่าง feature construction

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
การพัฒนาโมเดล Machine Learning สำหรับ Production

Feature transformations

แปลง feature ที่มีอยู่โดยตรง

  • ปรับการกระจายของข้อมูลให้เป็นปกติ
  • กำจัด outlier
  • เพิ่มความแม่นยำและประสิทธิภาพของโมเดล
```py
```py

การพัฒนาโมเดล Machine Learning สำหรับ Production

Feature selection

คัดเลือก feature บางส่วนจาก feature ทั้งหมด โดยตัด feature ที่ซ้ำซ้อนหรือไม่เกี่ยวข้องออก

  • ลด overfitting
  • เพิ่มความแม่นยำและประสิทธิภาพของโมเดล
  • ทำให้โมเดลตีความได้ง่ายขึ้น

feature selection

1 https://www.manning.com/books/feature-engineering-bookcamp
การพัฒนาโมเดล Machine Learning สำหรับ Production

ตัวอย่าง feature engineering (ต่อ)

```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
การพัฒนาโมเดล Machine Learning สำหรับ Production

เรียนรู้เพิ่มเติมเกี่ยวกับ feature engineering

หนังสือ feature engineering

การพัฒนาโมเดล Machine Learning สำหรับ Production

มาฝึกกันเถอะ!

การพัฒนาโมเดล Machine Learning สำหรับ Production

Preparing Video For Download...