大模型应用
训练分类/回归或神经网络模型时,用规范数据划分…
适用于使用 scikit-learn、PyTorch 或 TensorFlow/Keras 训练和评估机器学习模型,并处理过拟合、欠拟合、收敛和调参问题。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 49/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 85/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
训练分类/回归或神经网络模型时,用规范数通用
解决思路
- 按训练流程执行:Data Preparation → Feature Engineering → Model Selection → Training → Evaluation。
- 数据准备:用 `pandas.read_csv('data.csv')` 加载数据,用 `df.dropna()` 清洗缺失值。
- 对类别变量用 `LabelEncoder` 编码,例如 `df['category'] = le.fit_transform(df['category'])`。
- 划分数据为 70/15/15:先 `train_test_split(X, y, test_size=0.3)`,再对临时集 `train_test_split(X_temp, y_temp, test_size=0.5)` 得到 train/val/test。
- 特征缩放:用 `StandardScaler`,只在 `X_train` 上 `fit_transform`,对 `X_val`、`X_test` 只 `transform`。
- Scikit-learn 训练:用 `RandomForestClassifier(n_estimators=100, random_state=42)`,执行 `model.fit(X_train, y_train)`,在验证集 `predict`,输出 `classification_report(y_val, y_pred)` 和 `accuracy_score`。
- PyTorch 训练:定义 `nn.Module` 模型类,用 `nn.Sequential` 组合 `nn.Linear(input_dim, 64)`、`nn.ReLU()`、`nn.Dropout(0.3)`、`nn.Linear(64, 32)`、`nn.ReLU()`、`nn.Linear(32, 1)`、`nn.Sigmoid()`。
- PyTorch 训练:初始化 `Model(X_train.shape[1])`,使用 `torch.optim.Adam(model.parameters(), lr=0.001)` 和 `nn.BCELoss()`。
- PyTorch 训练循环:`for epoch in range(100)`,每轮 `model.train()`、`optimizer.zero_grad()`、前向 `output = model(X_train_tensor)`、计算 `loss = criterion(output, y_train_tensor)`、`loss.backward()`、`optimizer.step()`。
- 评估指标选择:分类用 Accuracy、Precision、Recall、F1、AUC-ROC;回归用 MSE、RMSE、MAE、R²。
- 需要完整 PyTorch 训练框架时,加载 `references/pytorch-training.md`;其中包含自定义模型类(BatchNorm 和 Dropout)、带 early stopping 的训练/验证循环、学习率调度、模型 checkpointing、完整评估与 classification report。
- 需要完整 TensorFlow/Keras 训练框架时,加载 `references/tensorflow-keras.md`;其中包含 Sequential 模型架构、Callbacks(EarlyStopping、ReduceLROnPlateau、ModelCheckpoint、TensorBoard)、训练历史可视化、TFLite 转换用于移动端部署、自定义训练循环。
- Best Practices Do:使用交叉验证进行稳健评估。
- Best Practices Do:用 MLflow 跟踪实验。
- Best Practices Do:定期保存模型 checkpoints。
- Best Practices Do:监控过拟合。
- Best Practices Do:记录超参数。
- Best Practices Do:使用 70/15/15 train/val/test split。
- Best Practices Don't:不要在没有验证集时训练。
- Best Practices Don't:不要忽略类别不平衡。
- Best Practices Don't:不要跳过特征缩放。
- Best Practices Don't:不要用测试集做超参数调优。
- Best Practices Don't:不要忘记设置随机种子。
- 预防 Data Leakage:始终先划分数据,再仅用训练数据 fit transformer,然后 transform 训练/验证/测试;不要在所有数据上 `fit_transform`,否则测试集信息会泄漏。
- 预防类别不平衡:用类别权重或重采样;可 `compute_class_weight('balanced', classes=np.unique(y_train), y=y_train)`;用 `RandomForestClassifier(class_weight='balanced')`;或用 `SMOTE()` 对少数类过采样并 `fit_resample(X_train, y_train)`。
- 预防过拟合:添加正则化;PyTorch 用 `nn.Dropout(0.3)`;scikit-learn 用 `RandomForestClassifier(max_depth=10, min_samples_split=20)` 等复杂度控制;Keras 用 `EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)`,并在 `model.fit(X_train, y_train, validation_data=(X_val, y_val), callbacks=[early_stop])` 中启用。
- 保证可复现:设置 `random.seed(42)`、`np.random.seed(42)`、`torch.manual_seed(42)`;若 `torch.cuda.is_available()`,再执行 `torch.cuda.manual_seed_all(42)`。
- 避免用测试集调参:用验证集调参,测试集只做最终评估;用 `GridSearchCV(RandomForestClassifier(), param_grid, cv=5)`,`param_grid = {'n_estimators': [50, 100, 200], 'max_depth': [5, 10, 15]}`,执行 `grid_search.fit(X_train, y_train)`,取 `best_model = grid_search.best_estimator_`,最后在 `X_test, y_test` 上评估。
- 按需加载参考文件:需要 PyTorch 实现细节时加载 `references/pytorch-training.md`;需要 TensorFlow/Keras patterns 时加载 `references/tensorflow-keras.md`。
适用边界 · 注意事项
- 没有标注数据、只需推理/调用已有模型,或问题不属于模型训练任务时,不适用。
- 需要大规模分布式训练、LLM 预训练、专用生产 MLOps/部署平台时,本技能只覆盖常规 scikit-learn/PyTorch/TensorFlow 训练。
- 项目不允许引入 scikit-learn/PyTorch/TensorFlow/MLflow/imblearn 等依赖时,不适用。