初级
第12章 监督学习方法总结 - 交叉验证实现
未完成
初级参考
完整示例代码供参考,建议自己理解后重新输入
import numpy as np
def k_fold_split(X, y, k=5, shuffle=True, random_state=None):
n_samples = len(X)
indices = np.arange(n_samples)
if shuffle:
if random_state is not None:
np.random.seed(random_state)
np.random.shuffle(indices)
fold_sizes = np.full(k, n_samples // k)
fold_sizes[:n_samples % k] += 1
folds = []
current = 0
for fold_size in fold_sizes:
test_indices = indices[current:current + fold_size]
train_indices = np.concatenate([indices[:current], indices[current + fold_size:]])
folds.append((train_indices, test_indices))
current += fold_size
return folds
def cross_val_score(model, X, y, k=5, shuffle=True, random_state=None):
folds = k_fold_split(X, y, k, shuffle, random_state)
scores = []
for train_idx, test_idx in folds:
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
model.fit(X_train, y_train)
score = model.score(X_test, y_test)
scores.append(score)
return np.array(scores)
👑
升级 VIP
解锁全部题目,畅通无阻地学习
- ✓ 解锁全部训练包所有题目
- ✓ 查看完整参考代码和提示
- ✓ 浏览器内直接运行 Python 代码
- ✓ 自动批改 + 进度追踪
30天
¥18
1年
¥99
2年
¥158
3年
¥199