0
$\begingroup$

Below, I have the following datatset:

sample_df.head(2)

ID     S_LENGTH     S_WIDTH     P_LENGTH     P_WIDTH     SPECIES
-------------------------------------------------------------------
1      3.5          2.5          5.6         1.7        VIRGINICA
2      4.5          5.6          3.4         8.7         SETOSA

Therefore, how to I apply normalisation to this dataset using the following code below to all my columns, excluding the ID and SPECIES columns?

I basically want to use the preprocessing.MinMaxScaler() to apply normalisation, so that all the features are in a range of 0 and 1.

This is the code I am using...

min_max = preprocessing.MinMaxScaler()
min_max.fit_transform(sample_df)

...but when I execute it, I get this error:

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

Alternatively, if I do this...

min_max = preprocessing.MinMaxScaler()
min_max.fit_transform(sample_df[['S_LENGTH', 'S_WIDTH']])

sample_df.head(2)

...I get this error:

AttributeError: 'numpy.ndarray' object has no attribute 'sample'

Any help on how to accomplish what I want to do is much appreciated!

$\endgroup$
1
$\begingroup$

If you want to apply the result of fit_transform, you must assign to your columns.

columns = ['S_LENGTH', 'S_WIDTH', 'P_LENGTH', 'P_WIDTH']

min_max = preprocessing.MinMaxScaler()
df[columns] = min_max.fit_transform(df[columns])
df.head()

Output

   ID  S_LENGTH  S_WIDTH  P_LENGTH  P_WIDTH    SPECIES
0   1       0.0      0.0       1.0      0.0  VIRGINICA
1   2       1.0      1.0       0.0      1.0     SETOSA
$\endgroup$
  • $\begingroup$ Thanks so much! This has worked! $\endgroup$ – user110100 Jan 10 at 13:16
0
$\begingroup$

You can use df.select_dtypes(exclude='object') to exclude categorical columns. Also while importing dataset, set the index_col='ID' to use ID as index instead of column.

$\endgroup$
  • $\begingroup$ Thanks, but how do I solve this error? AttributeError: 'numpy.ndarray' object has no attribute 'sample' $\endgroup$ – user110100 Jan 10 at 12:28
  • $\begingroup$ Please can you rerun your code from start? Because I can on myside.. I'm not getting this error $\endgroup$ – Pramod yadav Jan 10 at 12:52
  • $\begingroup$ import pandas as pd from sklearn.preprocessing import MinMaxScaler from sklearn import datasets data=datasets.load_iris() Data=pd.DataFrame(data.data,columns=data.feature_names) Data['Target']=data.target Data.columns=['S_LENGTH','S_WIDTH','P_LENGTH','P_WIDTH','SPECIES'] sample_df=Data min_max=MinMaxScaler() min_max.fit_transform(sample_df[['S_LENGTH', 'S_WIDTH']]) sample_df.head(2) $\endgroup$ – Pramod yadav Jan 10 at 12:54
  • $\begingroup$ Thanks, but it still has the error. $\endgroup$ – user110100 Jan 10 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy