0

i am trying to use my machine learning model on dataset where i have only two columns while standard scaling them,i got the error expected 2D array but got 1 .

Below is the code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

# Splitting the dataset into the Training set and Test set
"""from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)"""

# 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)

# Predicting a new result
y_pred = regressor.predict(6.5)
y_pred = sc_y.inverse_transform(y_pred)

# 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()

when i try to put

y = sc_y.fit_transform([y])

like this i received no error but when i execute next 3 lines i receive another error.

which is bad input shape (1, 10)

can anyone help me on this?

2
  • Are both the two columns, input? Commented Jan 26, 2018 at 12:25
  • No , X is the input value and y is the actual output... Commented Jan 26, 2018 at 12:31

1 Answer 1

1

The StandardScaler() function in sklearn expects the input(X) to be in the following format:

X : numpy array of shape [n_samples, n_features]

So, reshape X to (-1,1) if you have only one feature column.

sc_X.fit_transform(X.reshape[-1,1])

This should work!

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

2 Comments

i just changed sc_X.fit_transform(X.reshape(-1,1)) and it worked fine. Thank you so much for your help. You are amazing.
Happy to help :) Thanks

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.