Expected 2D array, got 1D array instead error

22,296

Seems, expected dimension is wrong. Could you try:

regressor = SVR(kernel='rbf')
regressor.fit(X.reshape(-1, 1), y)
Share:
22,296
aroN
Author by

aroN

Iam an open source enthusiast having keen interest on devops. I love to learn new technologies and also having an open mind to share my knowledge and ideas.

Updated on February 05, 2022

Comments

  • aroN
    aroN over 2 years

    Iam getting the error as

    "ValueError: Expected 2D array, got 1D array instead: array=[ 45000. 50000. 60000. 80000. 110000. 150000. 200000. 300000. 500000. 1000000.]. 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."

    while executing the following code:

    # SVR
    
    # Importing the libraries
    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    # Importing the dataset
    dataset = pd.read_csv('Position_S.csv')
    X = dataset.iloc[:, 1:2].values
    y = dataset.iloc[:, 2].values
    
     # Feature Scaling
    from sklearn.preprocessing import StandardScaler
    sc_X = StandardScaler()
    sc_y = StandardScaler()
    X = sc_X.fit_transform(X)
    y = sc_y.fit_transform(y)
    
    # Fitting SVR to the dataset
    from sklearn.svm import SVR
    regressor = SVR(kernel = 'rbf')
    regressor.fit(X, y)
    
    # Visualising the SVR results
    plt.scatter(X, y, color = 'red')
    plt.plot(X, regressor.predict(X), color = 'blue')
    plt.title('Truth or Bluff (SVR)')
    plt.xlabel('Position level')
    plt.ylabel('Salary')
    plt.show()
    
    # Visualising the SVR results (for higher resolution and smoother curve)
    X_grid = np.arange(min(X), max(X), 0.01)
    X_grid = X_grid.reshape((len(X_grid), 1))
    plt.scatter(X, y, color = 'red')
    plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
    plt.title('Truth or Bluff (SVR)')
    plt.xlabel('Position level')
    plt.ylabel('Salary')
    plt.show()
    
  • aroN
    aroN over 5 years
    could u please tell what does this x.reshape(-1,1).actually do.It also solves the problem.I actually solved it by changing into y = dataset.iloc[:, 2:3].values eventhough i gave only 3 columns.
  • Danylo Baibak
    Danylo Baibak over 5 years
    According to the error message, you have input data in the format [45000, 50000, 60000, ...]. But the model expects the input in the format like [[45000], [50000], [60000], ...] - a list of the lists. So reshape(-1, 1) just changes a format.
  • Pavindu
    Pavindu almost 3 years
    Note that reshape() is now deprecated. use df.values.reshape() instead.