学习预测函数的参数,并在相同数据集上进行测试是一种错误的做法: 因为仅给出测试用例标签的模型将会获得极高的分数,但对于尚未出现过的数据它则无法预测出任何有用的信息。这种情况称为过拟合。为了避免这种情况,在进行(监督)机器学习实验时,一般会将数据集划分为训练集和测试集,训练集训练数据,测试集查看训练结果。
按照划分方式不同,分为纯随机采样和分层采样。
纯随机采样:train_test_split()
案例
from sklearn.model_selection import train_test_split
from sklearn import datasets
# 获取鸢尾花数据集
iris = datasets.load_iris()
print(iris.DESCR)
.. _iris_dataset:
Iris plants dataset
--------------------
**Data Set Characteristics:**
:Number of Instances: 150 (50 in each of three classes)
:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
- class:
- Iris-Setosa
- Iris-Versicolour
- Iris-Virginica
:Summary Statistics:
============== ==== ====