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}")
相关推荐
周杰伦_Jay9 小时前
【自动驾驶开源仿真平台】Carla、AirSim、Udacity self-driving-car-sim、Apollo、Autoware。
人工智能·机器学习·自动驾驶
初级炼丹师(爱说实话版)14 小时前
算法面经常考题整理(1)机器学习
人工智能·算法·机器学习
koo36415 小时前
李宏毅机器学习笔记33
人工智能·笔记·机器学习
无风听海15 小时前
神经网络之密集的词向量如何能够代表稀疏的词向量
人工智能·神经网络·机器学习
淡漠的蓝精灵15 小时前
深度解析Weights & Biases:让AI实验管理变得如此简单
人工智能·其他·机器学习
音视频牛哥16 小时前
低空经济的实时神经系统:空地一体化音视频架构的技术演进
机器学习·计算机视觉·音视频·低空经济·人工智能+·evtol·ai感知网络
Zyx200716 小时前
用 JavaScript 打造 AI 大脑:前端开发者的新时代——基于 Brain.js 的浏览器端 NLP 实战
javascript·机器学习
Hs_QY_FX17 小时前
幸福指数数据分析与预测:从数据预处理到模型构建完整案例
开发语言·python·机器学习
hrrrrb19 小时前
【机器学习】监督学习
人工智能·学习·机器学习
长桥夜波20 小时前
【第十九周】机器学习笔记08
人工智能·笔记·机器学习