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}")
相关推荐
陈天伟教授23 分钟前
人工智能应用- AI 增强显微镜:08.实时辅助诊断
人工智能·神经网络·机器学习·推荐算法
研知有术一站式科研平台1 小时前
研知有术论文辅导丨文献综述的三种类型和写法(附直接能用的写作模板)
人工智能·经验分享·机器学习·数据挖掘·论文笔记
高洁011 小时前
【无标题】如何利用知识图谱实现推理和计算
人工智能·机器学习·数据挖掘·transformer·知识图谱
梦想很大很大1 小时前
一个推荐系统是如何“长大”的(工程演进)
人工智能·机器学习·架构
FluxMelodySun2 小时前
机器学习(二十七) 降维:度量学习与随机梯度下降法求解
人工智能·学习·机器学习
AI-Ming5 小时前
程序员转行学习 AI 大模型: 模型微调| 附清晰概念分类
人工智能·pytorch·深度学习·机器学习·chatgpt·nlp·gpt-3
禁默5 小时前
从零吃透大语言模型 LLM,AI 应用开发必懂底层逻辑
人工智能·机器学习·语言模型·大模型
AI科技星5 小时前
基于v≡c光速螺旋理论的正确性证明:严格遵循科学方法论的完整路径
c语言·开发语言·人工智能·线性代数·算法·机器学习·数学建模
RFdragon9 小时前
分享本周所学——三维重建算法3D Gaussian Splatting(3DGS)
人工智能·线性代数·算法·机器学习·计算机视觉·矩阵·paddlepaddle
chaser&upper15 小时前
【一起啃西瓜书】机器学习-期末复习(不挂科)
人工智能·机器学习·期末复习·学霸笔记