0
$\begingroup$

I'm trying to split my x and y into train and test data for my ML model but it's giving me this error: ValueError: Found input variables with inconsistent numbers of samples: [6, 366]. My numpy array x looks like this:

array([[2, 3, 4, ..., 31, 1, 2],
       [1, 1, 1, ..., 12, 1, 1],
       [2021, 2021, 2021, ..., 2021, 2022, 2022],
       [53, 53, 1, ..., 52, 52, 52],
       [1, 1, 1, ..., 4, 1, 1],
       [1, 1, 1, ..., 1, 0, 1]], dtype=object)

x.shape: (6, 366)

My y numpy array:

array([ 774.534973,  975.50769 , ... ,  3824.19873 ])

With shape: (366,)

$\endgroup$

1 Answer 1

0
$\begingroup$

You should probably transpose your x array since the first dimension should correspond to the number of samples in your dataset, currently the first dimension represents the number of features instead of the number of samples. The following should work:

import numpy as np
from sklearn.model_selection import train_test_split
# generate random data with same shape as your data
X, y = np.random.randn(6, 366), np.random.randn(366)
print(X.shape, y.shape)
# (6, 366) (366,)

# transpose features array to make sure first dimension corresponds to number of samples
X = X.T
print(X.shape, y.shape)
# (366, 6) (366,)
X_train, X_test, y_train, y_test = train_test_split(X, y)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
# (274, 6) (92, 6) (274,) (92,)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.