Kaggle Intermediate ML Part Four——Cross-Validation

What is it?

Cross-validation is a technique used to evaluate the generalizability of a machine learning model. In simpler terms, it helps you understand how well your model will perform on unseen data, which is crucial for real-world applications.

Here's how it works:

  1. Split the data: Your original dataset is divided into folds (usually equally sized).
  2. Train-Test Split: In each fold, one fold is kept for testing (hold-out set), while the remaining folds are used for training the model.
  3. Evaluate and Repeat: The model is trained on the training data and evaluated on the hold-out set. This process is repeated for each fold, ensuring every data point is used for both training and testing.
  4. Combine and Analyze: The performance metrics (e.g., accuracy, precision, recall) from each fold are combined to get an overall estimate of the model's performance on unseen data.

Common Cross-Validation Techniques:

  • K-Fold Cross-validation: The data is split into k folds, and the training-testing process is repeated k times.
  • Stratified K-Fold: Similar to k-fold, but ensures each fold has a similar distribution of class labels (important for imbalanced datasets).
  • Leave-One-Out Cross-validation (LOOCV): Each data point is used as the testing set once, while all other points are used for training. This is computationally expensive for large datasets.

Production Use and Examples:

  • Model Selection: Compare different models and choose the one with the best cross-validation performance.
  • Hyperparameter Tuning: Optimize hyperparameters (model settings) by evaluating their impact on cross-validation performance.
  • Feature Selection: Identify and remove irrelevant or redundant features that may lead to overfitting.

python 复制代码
from sklearn.model_selection import KFold
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score

# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target

# Define the model
model = LogisticRegression()

# Define the K-Fold cross-validation strategy
kfold = KFold(n_splits=5, shuffle=True, random_state=42)

# Track performance metrics
auc_scores = []

# Iterate through each fold
for train_index, test_index in kfold.split(X):
    # Split data into training and testing sets
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

    # Train the model on the training data
    model.fit(X_train, y_train)

    # Make predictions on the testing data
    y_proba = model.predict_proba(X_test)[:, 1]  # Probability of positive class

    # Calculate AUC
    auc = roc_auc_score(y_test, y_proba)
    auc_scores.append(auc)

# Print the average AUC across all folds
print(f"Average AUC: {sum(auc_scores) / len(auc_scores):.2f}")
相关推荐
Christo329 分钟前
windows系统配置openclaw
人工智能·机器学习
小李独爱秋35 分钟前
机器学习与深度学习实验项目3 卷积神经网络实现图片分类
人工智能·深度学习·机器学习·分类·cnn·mindspore·模式识别
audyxiao0011 小时前
AI一周重要会议和活动概览(2.16-2.22)
人工智能·机器学习·一周会议与活动
龙山云仓2 小时前
No156:AI中国故事-对话司马迁——史家绝唱与AI记忆:时间叙事与因果之链
大数据·开发语言·人工智能·python·机器学习
HyperAI超神经3 小时前
视觉真实之外:清华WorldArena全新评测体系揭示具身世界模型的能力鸿沟
人工智能·深度学习·神经网络·机器学习·计算机视觉·机器人
MoonOutCloudBack5 小时前
VeRL 框架 RL 微调大语言模型,algorithm.use_pf_ppo 参数详解
人工智能·机器学习·语言模型·自然语言处理
Project_Observer6 小时前
项目管理中如何跟踪工时?
数据库·深度学习·机器学习
geneculture6 小时前
智慧系统工程实践:从人机互助至人机协同
大数据·人工智能·机器学习·知识图谱·融智学的重要应用·哲学与科学统一性·融智时代(杂志)
智能交通技术7 小时前
iTSTech:从AGI到AMI——自动驾驶的新方向 2026
人工智能·机器学习·自动驾驶·agi
2501_926978337 小时前
重整化群理论:从基础到前沿应用的综述(公式版)---AGI理论系统基础2.2
人工智能·经验分享·深度学习·机器学习·agi