# grid_serch_best_estimator.py
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor

# 1. 生成示例数据集(回归问题)
np.random.seed(42)
n_samples = 1000
n_features = 5

# 创建特征
X = np.random.randn(n_samples, n_features)
# 创建目标值(回归问题)
y = (X[:, 0] * 2 + X[:, 1] * 1.5 + X[:, 2] * 0.5 + np.random.randn(n_samples) * 0.5)

# 转换为DataFrame并保存(可选)
df = pd.DataFrame(X, columns=[f'feature_{i}' for i in range(1, n_features+1)])
df['target'] = y
df.to_csv('dataset.csv', index=False)
print("dataset.csv 已生成")
print(df.head(), "\n")

# 2. 加载数据(如果已有数据集,可以跳过上面的生成步骤)
data = pd.read_csv('dataset.csv')
X = data.drop('target', axis=1)  # 特征
y = data['target']                # 标签

# 3. 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# 4. 特征标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

print(f"训练集形状: {X_train_scaled.shape}")
print(f"测试集形状: {X_test_scaled.shape}\n")

# 5. 参数网格
param_grid = {
    'n_estimators': [50, 100, 200],
    'max_depth': [None, 10, 20, 30],
    'min_samples_split': [2, 5, 10]
}

# 6. 创建模型
rf = RandomForestRegressor(random_state=42)

# 7. 网格搜索
grid_search = GridSearchCV(
    rf, param_grid, 
    cv=5,           # 5折交叉验证
    scoring='r2',   # 使用R²评分
    n_jobs=-1       # 使用所有CPU核心
)

print("开始网格搜索...")
grid_search.fit(X_train_scaled, y_train)

# 8. 输出结果
print(f"\n最佳参数: {grid_search.best_params_}")
print(f"最佳交叉验证得分 (R²): {grid_search.best_score_:.4f}")

# 9. 使用最佳模型
best_model = grid_search.best_estimator_

# 10. 在测试集上评估
from sklearn.metrics import mean_squared_error, r2_score

test_predictions = best_model.predict(X_test_scaled)
test_r2 = r2_score(y_test, test_predictions)
test_mse = mean_squared_error(y_test, test_predictions)

print(f"\n测试集 R² 得分: {test_r2:.4f}")
print(f"测试集 MSE: {test_mse:.4f}")

# 11. 保存最佳模型
import joblib
joblib.dump(best_model, 'best_random_forest_model.pkl')
print("\n最佳模型已保存为 best_random_forest_model.pkl")

运行结果:

(ai_env) $ python3 grid_serch_best_estimator.py
dataset.csv 已生成
   feature_1  feature_2  feature_3  feature_4  feature_5    target
0   0.496714  -0.138264   0.647689   1.523030  -0.234153  0.897996
1  -0.234137   1.579213   0.767435  -0.469474   0.542560  2.057556
2  -0.463418  -0.465730   0.241962  -1.913280  -1.724918 -2.402270
3  -0.562288  -1.012831   0.314247  -0.908024  -1.412304 -2.651743
4   1.465649  -0.225776   0.067528  -1.424748  -0.544383  2.992812 

训练集形状: (800, 5)
测试集形状: (200, 5)

开始网格搜索...

最佳参数: {'max_depth': None, 'min_samples_split': 2, 'n_estimators': 200}
最佳交叉验证得分 (): 0.9310

测试集 R² 得分: 0.9426
测试集 MSE: 0.3775

最佳模型已保存为 best_random_forest_model.pkl
(ai_env) $ 
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐