Feature engineering และการคัดเลือก feature

Machine Learning แบบ End-to-End

Joshua Stapleton

Machine Learning Engineer

Feature engineering

การสร้าง feature

  • ทำให้ปัญหาง่ายขึ้น
  • เพิ่มประสิทธิภาพโมเดล

เทคนิค

  • ปรับแต่ง feature ที่มีอยู่
  • ออกแบบ feature ใหม่

ประโยชน์

  • การ deploy, บำรุงรักษา และฝึกโมเดลทำได้ง่ายขึ้น
  • เพิ่มความสามารถในการตีความ

ขั้นตอนปัจจุบันในวงจรชีวิต machine learning: feature engineering

Machine Learning แบบ End-to-End

Normalization

  • ปรับสเกล feature เชิงตัวเลขให้อยู่ในช่วง [0, 1]
  • มีประโยชน์เมื่อ feature มีสเกลหรือช่วงค่าที่แตกต่างกัน
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import Normalizer

# Split the data
X_train, X_test = train_test_split(df, test_size=0.2, random_state=42)
# Createnormalizer object, fit on training data, normalize, and transform test set
norm = Normalizer()
X_train_norm = norm.fit_transform(X_train)
X_test_norm = norm.transform(X_test)
Machine Learning แบบ End-to-End

Standardization

  • ปรับข้อมูลให้มีค่าเฉลี่ย = 0 และความแปรปรวน = 1
  • เหมาะสำหรับอัลกอริทึมที่ต้องการค่าเฉลี่ยและความแปรปรวนใกล้เคียงกัน
from sklearn.preprocessing import StandardScaler

# Split the data
X_train, X_test = train_test_split(df, test_size=0.2, random_state=42)
# Create a scaler object and fit training data to standardize it
sc = StandardScaler()
X_train_stzd = sc.fit_transform(X_train)
# Only standardize the test data
X_test_stzd = sc.transform(X_test)
Machine Learning แบบ End-to-End

feature ที่ดีควรมีลักษณะอย่างไร?

  • ใช้ feature ที่เกี่ยวข้องกับปัญหา
  • สภาพอากาศในวันนัดพบแพทย์ไม่ควรมีผลต่อการวินิจฉัย

 

ภาพอากาศพายุแสดงหลักการของความเกี่ยวข้องของ feature ในการคัดเลือก feature

  • ใช้ feature ที่แตกต่างกัน (orthogonal)
  • อายุในหน่วยเดือนและอายุในหน่วยปีเป็น feature ที่ซ้ำซ้อนกัน

 

ไดอะแกรมแสดงหลักการของ orthogonality ในการคัดเลือก feature

Machine Learning แบบ End-to-End

sklearn.feature_selection

 

from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import train_test_split

# Splitting data into train and test subsets first to avoid data leakage X_train, X_test, y_train, y_test = train_test_split( heart_disease_df_X, heart_disease_df_y, test_size=0.2, random_state=42)
Machine Learning แบบ End-to-End

sklearn.feature_selection (ต่อ)

 

# Define and fit the random forest model
rf = RandomForestClassifier(n_jobs=-1, class_weight='balanced', max_depth=5)
rf.fit(X_train, y_train)

# Define and run feature selection model = SelectFromModel(rf, prefit=True) features_bool = model.get_support() features = heart_disease_df.columns[features_bool]
Machine Learning แบบ End-to-End

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

Machine Learning แบบ End-to-End

Preparing Video For Download...