TypeError: fit_transform() missing 1 required positional argument: ‘X’

Tags: ,



I am trying to do Feature Scaling in a dataset, but I get an error and have no idea how to proceed:

    > Traceback (most recent call last):
    > 
    >   File "<ipython-input-10-71bea414b4d0>", line 22, in <module>
    >     x_train = sc_X.fit_transform(x_train)
    > 
    > TypeError: fit_transform() missing 1 required positional argument: 'X'

and here is my code:

import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 3].values
# Taking care of missing data
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values="NaN", strategy="mean", axis=0)
imputer = Imputer.fit(imputer,X[:,1:3])
X[:, 1:3] = Imputer.transform(imputer,X[:, 1:3])

#Spliting the dataset into 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 Scalling

from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler
x_train = sc_X.fit_transform(x_train)
x_test = sc_X.transform(x_test)

Answer

You are assigning sc_X a reference to the StandardScaler class. but fit_transform() is is not a class method, but an instance method. This means that you have to create an instance of the class.

So,

sc_X = StandardScaler

should be:

sc_X = StandardScaler()


Source: stackoverflow