Jan*_*ima 1 python machine-learning scikit-learn supervised-learning sklearn-pandas
我正在学习 sklearn,但我不太明白其中的区别以及为什么将 4 个输出与函数 train_test_split 一起使用。
在文档中,我找到了一些示例,但这还不足以结束我的疑虑。
代码是使用 x_train 来预测 x_test 还是使用 x_train 来预测 y_test?
训练和测试有什么区别?我是否使用 train 来预测测试或类似的东西?
我很困惑。我将在文档中提供以下示例。
>>> 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]]
Run Code Online (Sandbox Code Playgroud)
小智 13
假设我们有这些数据
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 )
Run Code Online (Sandbox Code Playgroud)
X_train包含特征值(年龄和性别 => 训练数据)
y_train包含与值对应的目标输出X_train(疾病=>训练数据)(训练过程后我们应该找到什么值)
y_train如果模型成功,训练过程(预测)后也会生成一些值,这些值应该与值非常接近或相同 。
X_test包含训练后要测试的特征值(年龄和性别=>测试数据)
y_test包含与(年龄和性别 => 训练数据)相对应的目标输出(疾病 => 测试X_test数据),并将X_test在训练后与模型给定值的预测值进行比较,以确定模型的成功程度。
Man*_*ojK 10
下面是一个假人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]})
Run Code Online (Sandbox Code Playgroud)
这里我们有 3 列,X1,X2,Y
假设X1 & X2是您的自变量,'Y'列是您的因变量。
X = df[['X1','X2']]
y = df['Y']
Run Code Online (Sandbox Code Playgroud)
与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
Run Code Online (Sandbox Code Playgroud)
现在
1)。X_train - 这包括您的所有自变量,这些将用于训练模型,正如我们所指定的test_size = 0.4,这种60%来自完整数据的观察手段将用于训练/拟合模型,其余的40%将用于测试模型。
2)。X_test - 这是40%数据中自变量的剩余部分,不会在训练阶段使用,将用于进行预测以测试模型的准确性。
3)。y_train - 这是您需要由该模型预测的因变量,这包括针对您的自变量的类别标签,我们需要在训练/拟合模型时指定我们的因变量。
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
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读有关指标的更多信息
在此处阅读有关数据拆分的更多信息
希望这可以帮助:)
| 归档时间: |
|
| 查看次数: |
13342 次 |
| 最近记录: |