超參數調優
Vibe Prompt
「用遺傳演算法最佳化 Random Forest 的超參數:n_estimators(10-200)、max_depth(3-20)、min_samples_split(2-10),目標最大化交叉驗證準確率。」
import random
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_digits
from sklearn.model_selection import cross_val_score
data = load_digits()
X, y = data.data, data.target
def evaluate(params):
n_est, depth, min_split = params
rf = RandomForestClassifier(
n_estimators=int(n_est),
max_depth=int(depth),
min_samples_split=int(min_split),
random_state=42, n_jobs=-1
)
scores = cross_val_score(rf, X, y, cv=3, scoring='accuracy')
return scores.mean()
# 簡單遺傳演算法調參
bounds = [(10, 200), (3, 20), (2, 10)]
pop = [[random.uniform(b[0], b[1]) for b in bounds] for _ in range(20)]
for gen in range(20):
scored = [(evaluate(p), p) for p in pop]
scored.sort(reverse=True)
pop = [p for _, p in scored]
print(f"第 {gen+1} 代最佳: {scored[0][0]:.4f} (n_est={int(scored[0][1][0])}, depth={int(scored[0][1][1])})")
next_pop = pop[:2]
while len(next_pop) < 20:
p1, p2 = random.choices(pop[:10], k=2)
child = [random.choice([p1[i], p2[i]]) for i in range(3)]
child = [c + random.uniform(-5, 5) for c in child]
child = [max(b[0], min(b[1], child[i])) for i, b in enumerate(bounds)]
next_pop.append(child)
pop = next_pop
print(f"\n最佳參數: n_est={int(pop[0][0])}, depth={int(pop[0][1])}, min_split={int(pop[0][2])}")