模型训练

端到端机器学习

Joshua Stapleton

Machine Learning Engineer

奥卡姆剃刀

  • 最简单且足够的解释最佳
  • 选模时偏向简单模型

展示奥卡姆剃刀原理的示例图片

端到端机器学习

建模选项

逻辑回归

  • 寻找类别间的决策边界
  • sklearn.linear_model.LogisticRegression

支持向量分类器

  • 寻找分隔类别的超平面
  • sklearn.svm.SVC

决策树

  • 通过简单"规则"进行分类
  • sklearn.tree.DecisionTreeClassifier

随机森林

  • 集成多棵决策树
  • sklearn.ensemble.RandomForestClassifier
端到端机器学习

其他模型

深度学习模型

  • 神经网络
  • 卷积神经网络
  • 生成式预训练变换模型(GPT)

K 近邻(KNN)

  • 监督学习算法

XGBoost

端到端机器学习

训练原则

模型:

  • 使用清洗并已处理特征的数据集
  • 从训练数据中学习模式
  • 旨在预测心脏病诊断的目标

原则:

  • 模型需对未见数据泛化(训练集之外)
  • 预留一部分数据,训练后用于测试
  • 训练/测试通常为 70/30 或 80/20
  • 可用 sklearn.model_selection.train_test_split
端到端机器学习

训练模型

# Importing necessary libraries
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

# Split the data into training and testing sets (80:20) X_train, X_test, y_train, y_test = train_test_split(features, heart_disease_y, test_size=0.2, random_state=42)
# Define the models logistic_model = LogisticRegression(max_iter=200)
# Train the model logistic_model.fit(X_train, y_train)
端到端机器学习

获取模型预测

# Jane Doe 的健康数据,例如:[年龄、胆固醇、血压等]
jane_doe_data = [45, 230, 120, ...]

# 重塑为二维,因为 scikit-learn 需要二维类数组输入 jane_doe_data = jane_doe_data.reshape(1, -1)
# 用模型预测 Jane 的心脏病诊断概率 jane_doe_probabilities = logistic_model.predict_proba(jane_doe_data) jane_doe_prediction = logistic_model.predict(jane_doe_data)
端到端机器学习

获取模型预测(续)

# Print the probabilities
print(f"Jane Doe's predicted probabilities: {jane_doe_probabilities[0]}")
print(f"Jane Doe's predicted health condition: {jane_doe_prediction[0]}")
Jane Doe 的预测健康状况概率: [0.2 0.8]

Jane Doe 的预测健康状况: 1
端到端机器学习

Vamos praticar!

端到端机器学习

Preparing Video For Download...