Error while running Python code

python

#1

HI Experts ,
learning and working with data science with phython ans stuck with the following

when I execute the following code in anaconda/Jupyter its giving me an eeror.

from sklearn.preprocessing import LabelEncoder
var_mod = ['Gender','Married','Dependents','Education','Self_Employed','Property_Area','Loan_Status']
le = LabelEncoder()
for i in var_mod:
    df[i] = le.fit_transform(df[i])
df.dtypes

Error

File “”, line 2
from sklearn.preprocessing import LabelEncoder
^
IndentationError: unexpected indent

please help me in troubleshooting

Regards,
Tony


#3

@tillutony

Check if there is any space before from

Python is very sensitive to indentation, so make sure you are careful about spaces.

Kunal


#4

Hi kunal,

from sklearn.preprocessing import LabelEncoder
var_mod = ['Gender','Married','Dependents','Education','Self_Employed','Property_Area','Loan_Status']
le = LabelEncoder()
for i in var_mod:
    df[i] = le.fit_transform(df[i])
df.dtypes 

The above code this time gives me the following error

TypeError: unorderable types: str() > float()

Please let me know how do I fix this.

Regards,
tony


#5

Hi @tillutony,

You are trying to do Label encoding for float variables too. Just use the columns which have the attribute “object” for label encoding.


#6

Hi

firstly thanks for your adhoc responce

I am just running through the guide data science with phython and got the above so please can u elaborate me on the code to proceed further or send me the code to move further.

Thanks,
tony


#7

The first thing to do is check the datatypes of each column by

train.dtypes

then if the datatype is object, then label encode them to numerical.

Check out the “How to Label Encode” part in this article


#8

Hi @tillutony

Did you find a solution to your problem above ? I am facing the same problem : TypeError: unorderable types: str() > float().

Regards
Umesh


#9

Hi @kunal, I am getting the same error as @tillutony. Not able to troubleshoot. kindly help.
TypeError: ‘>’ not supported between instances of ‘str’ and ‘float’


#10

Hello All,

you can try to explicitly convert the column to object (string), before label encoding it.

from sklearn.preprocessing import LabelEncoder
var_mod = ['Gender','Married','Dependents','Education','Self_Employed','Property_Area','Loan_Status']
le = LabelEncoder()
for i in var_mod:
     df[i] = le.fit_transform(df[i].astype('str'))

This should get rid of the type conversion error.

regards,
jagdish joshi


#11

Thanks Jagdish