机器学习特征工程实战
4 阅读
预计 3 分钟
特征工程是机器学习中最重要的环节。好的特征比好的模型更重要。
## 数值特征处理
```python
from sklearn.preprocessing import StandardScaler, MinMaxScaler
scaler = StandardScaler()
df['feature_scaled'] = scaler.fit_transform(df[['feature']])
df['feature_log'] = np.log1p(df['feature'])
df['age_bin'] = pd.cut(df['age'], bins=[0, 18, 35, 50, 100])
```
## 类别特征处理
```python
df = pd.get_dummies(df, columns=['city'], drop_first=True)
target_mean = df.groupby('city')['target'].mean()
df['city_target_enc'] = df['city'].map(target_mean)
```
## 特征选择
```python
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=100)
rf.fit(X, y)
importance = pd.Series(rf.feature_importances_, index=X.columns)
```
特征工程是数据科学家的核心竞争力。
0 条评论 欢迎参与讨论