0

This is my code:

X = df['bmi']
y = df['charges']
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.70, test_size = 0.3)
print("trainX:", X_train.shape,"testX:",  X_test.shape, "trainY:", y_train.shape, "testY:", y_test.shape)
print()

myModel = LinearRegression() 

myModel.fit(X_train,y_train)

When run I get a big error saying: "Expected 2D array, got 1D array instead: array=[17.86 28.69 30.69 23.75 19.95 26.885 33.66 34.105 36.005 37.1 etc..."

Jupyter says it has something to do with the

myModel.fit(X_train,y_train)

line of code

acidic231
  • 45
  • 6
  • y_train = y_train.values.reshape(1,-1) ? https://stackoverflow.com/questions/45554008/error-in-python-script-expected-2d-array-got-1d-array-instead – Sachin Kohli Oct 10 '22 at 05:11
  • @SachinKohli got it spot on, but I think it's better to do this with `y` itself so that after your `train_test_split` the changes will also be transferred to `y_test` using `y = y.values.reshape(-1, 1)` – Gautam Chettiar Oct 10 '22 at 05:34

0 Answers0