-3

The error shows in my last two codes.

ValueError: Expected 2D array, got 1D array instead: array=[0 1].


Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

import numpy as np

import pandas as pd

from sklearn.model_selection import ShuffleSplit

%matplotlib inline

df = pd.read_csv('.......csv')
df.drop(['Company'], 1, inplace=True)

x = pd.DataFrame(df.drop(['R&D Expense'],1))
y = pd.DataFrame(df['R&D Expense'])

X_test = x.index[[0,1]]
y_test = y.index[[0,1]]

X_train = x.drop(x.index[[0,1]])
y_train = y.drop(y.index[[0,1]])

from sklearn.metrics import r2_score
def performance_metric(y_true, y_predict):
    score = r2_score(y_true, y_predict)
    return score

from sklearn.metrics import make_scorer
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import GridSearchCV

def fit_model_shuffle(x, y):

  cv_sets = ShuffleSplit(n_splits = 10, test_size = 0.20, random_state = 0)

  regressor = KNeighborsRegressor()
    params = {'n_neighbors':range(3,10)}

       scoring_fnc = make_scorer(performance_metric)
     grid = GridSearchCV(regressor, param_grid=params,scoring=scoring_fnc,cv=cv_sets)
    grid = grid.fit(x, y)
    return grid.best_estimator_

reg = fit_model_shuffle(X_train, y_train)

> for i, y_predict in enumerate(reg.predict(X_test),1):
    print(i, y_predict)

1 Answer 1

1

The error message is self-explanatory. Your library expects the input to be a 2D matrix, with one pattern per row. So, if you are doing regression with just one input, before passing it to the regressor, do

my_data = my_data.reshape(-1, 1)

to make a 2X1 shaped matrix

On the other hand (unlikely), if you have a single vector [0, 1]

my_data = my_data.reshape(1, -1) 

to make a 1X2 matrix

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, but where should I reshape the data? Thank you :)
@SandyTsai: do that on your X_train and X_test variables, before passing them into the regressor.fit() function
@blue_note i don't find this answer anywhere, why should i work with 2d array?
@VitorGonçalves: no idea, the original question required a 2D array

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.