偏差-方差权衡

Python 中的模型验证

Kasey Jones

Data Scientist

方差

  • 方差:对训练数据贴合过度
    • 无法很好泛化到测试数据
    • 训练误差低,测试误差高
    • 出现在模型过拟合、复杂度高时
Python 中的模型验证

过拟合模型(高方差)

当预测过于贴合训练数据时会发生过拟合。如果散点图中预测与真实值完全对齐,可能就是过拟合。

Python 中的模型验证

偏差

  • 偏差:未能找到特征与响应的关系
    • 训练/测试误差高
    • 出现在模型欠拟合时
Python 中的模型验证

欠拟合模型(高偏差)

当待预测变量与模型中的自变量存在关系,但我们未能找到该关系时,就会欠拟合。

Python 中的模型验证

最优表现

Python 中的模型验证

导致过拟合/欠拟合的参数

rfc = RandomForestClassifier(n_estimators=100, max_depth=4)
rfc.fit(X_train, y_train)

print("Training: {0:.2f}".format(accuracy_score(y_train, train_predictions)))
Training: .84
print("Testing: {0:.2f}".format(accuracy_score(y_test, test_predictions)))
Testing: .77
Python 中的模型验证
rfc = RandomForestClassifier(n_estimators=100, max_depth=14)
rfc.fit(X_train, y_train)

print("Training: {0:.2f}".format(accuracy_score(y_train, train_predictions)))
Training: 1.0
print("Testing: {0:.2f}".format(accuracy_score(y_test, test_predictions)))
Testing: .83
Python 中的模型验证
rfc = RandomForestClassifier(n_estimators=100, max_depth=10)
rfc.fit(X_train, y_train)

print("Training: {0:.2f}".format(accuracy_score(y_train, train_predictions)))
Training: .89
print("Testing: {0:.2f}".format(accuracy_score(y_test, test_predictions)))
Testing: .86
Python 中的模型验证

记住,只有你能防止过拟合!

Python 中的模型验证

Preparing Video For Download...