Search code examples
python-3.xexport-to-csvgoogle-colaboratory

numpy array:tuple index out of range


#converting the lists into numpy arrays
x_train=np.array(x_train)
x_test=np.array(x_test)
y_train=np.array(y_train)
y_test=np.array(y_test)
x_train.shape,x_test.shape,y_train.shape,y_test.shape

the problem starts from below code

reshaping into 2d to save in csv format

x_train_2d=np.reshape(x_train,(x_train.shape[0],x_train.shape[1]*x_train.shape[2]))
x_test_2d=np.reshape(x_test,(x_test.shape[0],x_test.shape[1]*x_test.shape[2]))
x_train_2d.shape,x_test_2d.shape

Solution

  • Probably what you can do is the following.

    Create a csv file with columns 40 values from second dimension of X, and Y value Rows will be training data first followed by test data

    data_train = np.c_[x_train, y_train]
    data_test = np.c_[x_test, y_test]
    data = np.r_[data_train, data_test]
    print(data.shape)
    np.savetxt('filename.csv', data, delimiter=',')
    

    Output data shape will be

    (8732, 41)