首页 > 解决方案 > IndexError:索引 2048 超出轴 1 的范围,大小为 2

问题描述


我下面的代码给了我以下错误

"IndexError: index 2048 is out of bounds for axis 1 with size 2". 

我很新,所以我不知道如何解决这个问题。

任何形式的帮助将不胜感激。

X=[]
X1 = np.load('class_1data_model1.npy')
X2 = np.load('class_2data_model1.npy')
X_data = np.append(X1,X2,axis=0)
Y_data = X_data[0:,2048]
X_data = X_data[:,0:2048]
x_tr,x_ts,y_tr,y_ts = train_test_split(X_data, Y_data, test_size=0.2,random_state=10

标签: pythonnumpyscikit-learn

解决方案


尝试这个。

X1 = np.load('class_1data_model1.npy')
X2 = np.load('class_2data_model1.npy')
X_data = np.append(X1,X2,axis=0)
nrows, nclos = X_data.shape
Y_data = X_data[:,2048].reshape((nrows,1))
X_data = X_data[:,:2048]
print('X_data.shape: \t{} (OLD)'.format((nrows,ncols))
print('X_data.shape: \t{} (NEW)'.format(X_data.shape)
print('Y_data.shape: \t{}'.format(Y_data.shape) 

### Now perform train_test_split.

推荐阅读