sklearn中的x_test、x_train、y_test、y_train有什么区别?

What is the difference between x_test, x_train, y_test, y_train in sklearn?

我正在学习 sklearn,但我不太了解其中的区别以及为什么使用函数 train_test_split.

的 4 个输出

在文档中,我找到了一些示例,但这还不足以消除我的疑虑。

代码是使用 x_train 预测 x_test 还是使用 x_train 预测 y_test?

训练和测试有什么区别?我是否使用训练来预测测试或类似的东西?

我对此很困惑。我将在下面给出文档中提供的示例。

>>> import numpy as np  
>>> from sklearn.model_selection import train_test_split  
>>> X, y = np.arange(10).reshape((5, 2)), range(5)  
>>> X
array([[0, 1], 
       [2, 3],  
       [4, 5],  
       [6, 7],  
       [8, 9]])  
>>> list(y)  
[0, 1, 2, 3, 4] 
>>> X_train, X_test, y_train, y_test = train_test_split(  
...     X, y, test_size=0.33, random_state=42)  
...  
>>> X_train  
array([[4, 5], 
       [0, 1],  
       [6, 7]])  
>>> y_train  
[2, 0, 3]  
>>> X_test  
array([[2, 3], 
       [8, 9]])  
>>> y_test  
[1, 4]  
>>> train_test_split(y, shuffle=False)  
[[0, 1, 2], [3, 4]]

您应该使用您的训练集训练您的分类器/回归器,并使用您的测试集测试/评估它。

您的分类器/回归器使用 x_train 来预测 y_pred 并使用 y_predy_train 之间的差异(通过损失函数)来学习。然后通过计算 x_test(也可以命名为 y_pred)和 y_test.

的预测之间的损失来评估它

下面是一个虚拟的 pandas.DataFrame 例如:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

df = pd.DataFrame({'X1':[100,120,140,200,230,400,500,540,600,625],
                       'X2':[14,15,22,24,23,31,33,35,40,40],
                       'Y':[0,0,0,0,1,1,1,1,1,1]})

这里有 3 列,X1,X2,Y 假设 X1 & X2 是您的自变量,而 'Y' 列是您的因变量。

X = df[['X1','X2']]
y = df['Y']

使用 sklearn.model_selection.train_test_split,您将创建 4 个数据部分,这些数据将用于拟合和预测值。

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4,random_state=42) 

X_train, X_test, y_train, y_test

现在

1). X_train - 这包括您的所有自变量,这些将用于训练模型,也正如我们指定的 test_size = 0.4,这意味着您的观察值 60%完整数据将用于 train/fit 模型,其余 40% 将用于测试模型。

2). X_test - 这是数据中自变量的剩余 40% 部分,不会在训练阶段使用,将用于进行预测以测试模型的准确性.

3). y_train - 这是您的因变量,需要由该模型预测,这包括针对您的自变量的类别标签,我们需要在 training/fitting 模型时指定我们的因变量。

4). y_test - 此数据具有测试数据的类别标签,这些标签将用于测试实际类别和预测类别之间的准确性。

现在您可以根据这些数据拟合模型,让我们拟合 sklearn.linear_model.LogisticRegression

logreg = LogisticRegression()
logreg.fit(X_train, y_train) #This is where the training is taking place
y_pred_logreg = logreg.predict(X_test) #Making predictions to test the model on test data
print('Logistic Regression Train accuracy %s' % logreg.score(X_train, y_train)) #Train accuracy
#Logistic Regression Train accuracy 0.8333333333333334
print('Logistic Regression Test accuracy %s' % accuracy_score(y_pred_logreg, y_test)) #Test accuracy
#Logistic Regression Test accuracy 0.5
print(confusion_matrix(y_test, y_pred_logreg)) #Confusion matrix
print(classification_report(y_test, y_pred_logreg)) #Classification Report

您可以阅读有关指标的更多信息here

阅读有关数据拆分的更多信息here

希望这对您有所帮助:)

将 X 视为 1000 个数据点,将 Y 视为整数 class 标签(每个数据点属于 class)

例如:
X = [1.24 2.36 3.24 ...(1000 项)
Y = [1,0,0,1.....(1000 项)]

我们按 600:400 比例

拆分

X_train => 将有 600 个数据点

Y_train => 将有 400 个数据点

X_test=> 将有 class 个标签对应 600 个数据点

Y_test=> 将有 class 个标签对应于 400 个数据点

假设我们有这个数据

Age    Sex       Disease
----  ------ |  ---------
  
  X_train    |   y_train   )
                           )
 5       F   |  A Disease  )
 15      M   |  B Disease  ) 
 23      M   |  B Disease  ) training
 39      M   |  B Disease  ) data
 61      F   |  C Disease  )
 55      M   |  F Disease  )
 76      F   |  D Disease  )
 88      F   |  G Disease  )
-------------|------------
   
  X_test     |    y_test

 63      M   |  C Disease  )
 46      F   |  C Disease  ) test
 28      M   |  B Disease  ) data
 33      F   |  B Disease  )

X_train 包含特征值(年龄和性别 => 训练数据)

y_train包含对应X_train值的目标输出(疾病=>训练数据)(训练过程后我们应该找到什么值)

训练过程(预测)后还会生成一些值,如果模型成功,这些值应该与 y_train 值非常接近或相同。

X_test包含训练后要测试的特征值(年龄和性别=>测试数据)

y_test 包含对应于 X_test(年龄和性别 => 训练数据)的目标输出(疾病 => 测试数据),并将与给定 [=15= 的预测值进行比较] 训练后模型的值以确定模型的成功程度。