2
from sklearn.model_selection import train_test_split

predictors=data.drop(['target'],axis=1)
targets=data['target']
train_x,test_x,train_y,test_y=train_test_split(predictors,targets,test_size=0.2,random_state=0) 

shape of train_x is (242,13)
shape of train_y is (242,)
shape of test_x is (61,13)
shape of test_y is (61,)

dataset has 303 examples and i/p has 13 features

if I try

np.reshape(train_y,(-1,1))

It says data must be 1 dimensional

I want train_y shape to be (242,1)

Imanpal Singh
  • 1,105
  • 1
  • 12
  • 22

2 Answers2

1

You should use the reshape method like so

train_y = train_y.reshape(train_y.shape[0], 1)

But I'd suggest reshaping targets so that both train_y and test_y are of shape (x, 1)

targets = data['target']
targets = targets.reshape(targets.shape[0], 1)

After this you can use train test split

Eeshaan
  • 1,557
  • 1
  • 10
  • 22
1

You can add a new axis with np.newaxis:

train_y = train_y[:, np.newaxis]

It does the same as train_y.reshape(train_y.shape[0], 1), so I guess it is just a matter of personal preference which way you choose.

Niklas Mertsch
  • 1,399
  • 12
  • 24