像一位教练:按统一流程训练模型。
常写作 sklearn,里面装着许多常用算法和工具。
给模型很多“题目和答案”,让它自己找到输入与答案之间的规律。
| 面积 X₁ | 房龄 X₂ | 价格 y |
|---|---|---|
| 80㎡ | 5年 | 320万 |
| 120㎡ | 2年 | 520万 |
| 60㎡ | 20年 | 180万 |
X 是模型能看到的线索,例如面积和房龄;y 是希望预测的答案,例如房价。
答案是类别:垃圾邮件还是正常邮件?
答案是连续数字:房价、销量、温度。
没有标准答案,让模型自己寻找相似群体。
考试题提前泄露,分数就失去意义。机器学习也一样。
训练分数和测试分数都不错,面对新数据仍能工作。
训练几乎满分,测试很差。模型把练习题背下来了。
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
model = make_pipeline(
SimpleImputer(), StandardScaler(), LogisticRegression())
model.fit(X_train, y_train)
prediction = model.predict(X_test)
score = model.score(X_test, y_test)| 指标 | 适合回答 | 需要注意 |
|---|---|---|
| Accuracy 准确率 | 总共猜对多少比例? | 类别极不平衡时容易骗人 |
| Precision 精确率 | 判为“是”的里面有多少真是? | 误报代价高时重要 |
| Recall 召回率 | 真正的“是”找回了多少? | 漏掉代价高时重要 |
| F1 | 精确率和召回率的折中怎样? | 仍要结合业务看 |
| MAE | 数字预测平均差多少? | 单位与目标相同,直观 |