Home > Mobile >  Standard Scaler Python Pandas
Standard Scaler Python Pandas

Time:06-03

can you help me to solve the error I get when I run the code below?

enter image description here

enter image description here

enter image description here

CodePudding user response:

The error is returned as you are trying to scale string variables, which you cannot do. You can only scale numerical variables.

Depending on what your string data looks like, if it's simple categorical data you could try one-hot-encoding. If it's more complicated, you'll probably have to go down the natural language processing route.

CodePudding user response:

You need to encode your string columns (categorical features) first. Use OrdinalEncoder(), LabelEncoder() or OneHotEncoder() to convert categorical columns to numeric. You can only scale numerical variables.

for example:

# Preform label encoding for gender variable (two values possible for male and female)
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lableencoder_X_2 = LabelEncoder()
X[:, 2] = lableencoder_X_2.fit_transform(X[:, 2])

# preform one hot encoding for geography varaible (3 values for states)
from sklearn.compose import ColumnTransformer
ct = ColumnTransformer([('ohe', OneHotEncoder(), [1])], remainder='passthrough')
X = np.array(ct.fit_transform(X), dtype = str)
X = X[:, 1:]
  • Related