IndexError: tuple index out of range to split features and label

IndexError: tuple index out of range to split features and label

我在尝试从 DATASET 中拆分特征和标签时遇到错误。这是代码:

print("Persiapan data")
#Split features and label from DATASET 
X = [] #features
Y = [] #labels
x_train,y_train=[],[]
for features,label in vektor_input:
    X.append(features)
    Y.append(label)
    
#Split X,Y to train and test
x_train,x_test,y_train,y_test = train_test_split(X, Y, train_size=0.7)

x_train = np.array(x_train)
x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))


print("Ukuran x_train :",len(x_train))
print("Ukuran x_test  :",len(x_test))

错误显示。我是 python 的新人。请帮忙,我已经尝试修复它,但出现了一些错误并卡在了这个问题上。

IndexError Traceback (most recent call
 last) <ipython-input-12-cd5441347f88> in <module>
      12 
      13 x_train = np.array(x_train)
 ---> 14 x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
      15 x_test = np.array(x_test)
      16 x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))
  IndexError: tuple index out of range

假设 np_array 是一维数组。

当使用 a 的 np_array.shape 时,您将得到输出 -> ( 行数 , )
所以 np_array.shape[1] 给你错误“IndexError:元组索引超出范围”

要解决此问题,请根据需要使用整数值而不是 np_array.shape[1]。 (注意:输入和输出数组中的元素总数应该相同)

例如:

num = 2 # change this according to required output 
x_train = np.reshape(x_train, (int(x_train.shape[0]/num), 1, num))