我是使用 Python 进行机器学习的新手,正在通过教程尝试我的第一次尝试。在那篇教程中,有些代码行我很难理解它们是如何相互作用的。
第一段代码是数据拆分如下:
train_x, val_x, train_y, val_y = train_test_split(X, y,test_size=0.3)
我的第一个问题:为什么我们使用验证数据而不是测试数据?为什么不是全部、训练、验证和测试?使用哪种组合的用例是什么?
下一节将详细说明 ML 模型和预测。
model = DecisionTreeRegressor()
model.fit(train_x, train_y)
val_predictions = model.predict(val_x)
我的第二个问题:对于 model.predict() 语句,我们为什么要把 val_x 放在那里?我们不想预测 val_y 吗?
奖励问题:另外,在许多教程中我看到了如何应用 StandardScaler。但是,在本教程中它并没有这样出现,或者其他一些函数是否已经在无需明确声明的情况下对其进行了缩放?请帮忙。
最佳答案
Q 1.1:为什么我们使用验证数据而不是测试数据? (在上面的场景中)
train_x, val_x, train_y, val_y = train_test_split(X, y,test_size=0.3)
First of all, the terms validation set and test set are very loosely used in many tutorials and sometimes interchangeably. It is quite possible to call the above
val_x, val_y
astest_x, test_y
Q 1.2:为什么不是全部、训练、验证和测试? (为什么 split ?)
我们所有的机器学习算法都将用于一些真实世界的数据(引用实际测试数据)。然而,在设计出一种算法之后,我们想要“测试”它的性能、准确性等。
Actually we currently don't have the real world data! Right?
但是我们有什么?火车数据!所以我们巧妙地留出一部分(拆分)用于以后测试算法。一旦模型准备就绪,测试数据将用于评估性能。
model = DecisionTreeRegressor()
model.fit(train_x, train_y)
val_predictions = model.predict(val_x) # contains y values predicted by the model
score = model.score(val_x, val_y) # evaluates predicted y against actual y of test data
print(score)
Q 2. : 对于model.predict() 语句,我们为什么要把val_x 放在那里?我们不想预测 val_y 吗?
绝对正确,我们想要预测 val_y
,但是模型需要 val_x
来预测 y。这正是我们作为参数传递给预测函数的内容。
I understand it might be confusing to read
model
predict
val_x
.So the better way is to interpret it, as
model
could you u pleasepredict
fromval_x
, and returnpredicted_y
.
我说 predicted_y
而不是 val_y
因为两者不会完全相似。他们有多少不同?这就是分数给出的。
一些术语
- 数据集:手头的数据。就是这些数据后来被分割了
- 训练集:它是我们的模型从中学习的数据集的一部分。通常较大,约为 70-80%。通常用 train_x 和 train_y 表示。
- 测试集:我们预留的一部分数据集,用于评估模型的性能。这“测试”了模型,因此得名。用 test_x 和 test_y 表示。
- 验证集:如果我们希望在学习过程中对准确度进行无偏估计,我们会使用另一组数据集。通常用于查找超参数等。通常用于
- 选择性能最好的算法(NB vs DT vs..)
- 微调参数(树深度,kNN 中的 k,SVM 中的 c)
问题 1.3:使用哪种组合的用例是什么?
You will always have train & test, or all three. However in your case the test is just named as val.
奖金问题:在许多教程中,我看到了如何应用 StandardScaler。但是,在本教程中它并没有这样出现,或者其他一些函数是否已经在无需明确声明的情况下对其进行了缩放?
It all depends on your data. If the data is pre-processed and all scaled properly then StandardScalers need not be applied. This particular tutorial just implies that data is already normalised accordingly.
关于python - model.fit vs model.predict - sklearn 中的差异和用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56518763/