2

I'm trying to normalize the continuous variables from my dataframe but I'm getting the following error

ValueError: could not convert string to float: 'M'

I know that the error occurs because I have some categorical variables so how can I ignore the categorical variables. This is my code

X=data
mms=MinMaxScaler()
mms.fit(X)
Xnorm=mms.transform(X)
print(Xnorm.min(axis=0))
print(Xnorm.max(axis=0))
print(Xnorm.shape)
4
  • Select the columns you want to normalize instead of using all columns. Something like X = data[['col1', 'col2']]. Commented Jun 2, 2022 at 18:19
  • @matias but that way isn't the dataset reduzed to only that columns? Commented Jun 2, 2022 at 18:23
  • No, only variable X. Commented Jun 2, 2022 at 18:30
  • @matias but if I want to normalize in the dataset I need to normalize in data not in X. Commented Jun 2, 2022 at 18:33

1 Answer 1

4

Select the numerical columns, then apply min-max transformation and store the result back into selected numerical columns

X = data.select_dtypes(np.number)
data[X.columns] = MinMaxScaler().fit_transform(X)
Sign up to request clarification or add additional context in comments.

4 Comments

@Subham Sharma I'm getting only 0's and 1's. [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. nan 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. nan] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. nan 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. nan] It's normal?
@P.Brito This depends on your input data, btw what is the shape of input dataframe data.shape?
this is the shape of the data (60398, 64)
@P.Brito What's the version of scikit-learn you are using? Also check the shape of X?

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.