gpt4 book ai didi

python - 在尝试使用 train_test_split 时,我的测试集形状很奇怪

转载 作者:行者123 更新时间:2023-11-30 09:30:32 25 4
gpt4 key购买 nike

我正在尝试将 KNN 应用于糖尿病初级数据,为了将我的数据集拆分为训练和测试数据集,我使用了代码中描述的 iloc 函数。但是当我使用这段代码时,我得到了非常奇怪的测试数据形状。谁能解释一下我在这里做错了什么

这是代码:

# first 8 columns from index 0 to 7 to be used for parameters
X = dataset.iloc[:,0:8]
y = dataset.iloc[:,8]
# lets split X and Y into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size =0.2,random_state =0)
# let us check the shape of all of these
print("X_train shape is : ", X_train.shape)
print("X_test shape is : ", X_test.shape)
print("y_train shape is : ", y_train.shape)
print("y_test shape is : ", y_test.shape)

This is the output I am getting :
X_train shape is : (614, 8)
X_test shape is : (154, 8)
y_train shape is : (614,)
y_test shape is : (154,)

最佳答案

当你使用train_test_split时,你不会得到pandas对象,而是numpy数组。您得到的输出是 numpy 数组如何显示其形状。以下是几个示例:

import numpy as np
np.array([0, 1, 2]).shape

## (3,)

np.array([[0, 1, 2], [3, 4, 5]]).shape

## (2, 3)

关于python - 在尝试使用 train_test_split 时,我的测试集形状很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59792443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com