0
$\begingroup$

I have the following matrices for training a model:

INPUT FEATURES MATRIX

$$ X = \begin{bmatrix} | & | & & |\\ X_1 & X_2 & ... & X_m\\ | & | & & | \end{bmatrix}; ~ X.shape = (n_x, m) $$

OUTPUT MATRIX

$$ Y = \begin{bmatrix} y_1 & y_2 & y_3 & ... & y_m \end{bmatrix} ~ ; ~ Y.shape=(1,m) $$

And I want to split into train and test data sets using scikitlearn.

This is what I have tried:

>>> print(X.shape)
>>> print(X.dtype)
>>> print(type(X))
>>> print(Y.shape)
>>> print(Y.dtype)
>>> print(type(Y))

(16, 504)
float64
<class 'numpy.ndarray'>
(1, 504)
float64
<class 'numpy.ndarray'>

>>> from sklearn.model_selection import train_test_split
>>> X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)

But I get the following error:

ValueError                                Traceback (most recent call last)
<timed exec> in <module>

/usr/local/lib/python3.8/site-packages/sklearn/model_selection/_split.py in train_test_split(*arrays, **options)
   2114         raise TypeError("Invalid parameters passed: %s" % str(options))
   2115 
-> 2116     arrays = indexable(*arrays)
   2117 
   2118     n_samples = _num_samples(arrays[0])

/usr/local/lib/python3.8/site-packages/sklearn/utils/validation.py in indexable(*iterables)
    235         else:
    236             result.append(np.array(X))
--> 237     check_consistent_length(*result)
    238     return result
    239 

/usr/local/lib/python3.8/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
    209     uniques = np.unique(lengths)
    210     if len(uniques) > 1:
--> 211         raise ValueError("Found input variables with inconsistent numbers of"
    212                          " samples: %r" % [int(l) for l in lengths])
    213 

ValueError: Found input variables with inconsistent numbers of samples: [16, 1]
```
$\endgroup$

1 Answer 1

1
$\begingroup$

The sample size should be on the first axis. i.e.

$X.shape = (m, n_x) $
$Y.shape = (m, 1) $

You can simply transpose the data.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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