How does Label Encoder assigns the same number

0 votes

I have the column in my data frame

city 

London
Paris
New York 
.


I am label encoding the column and it assigns the 0 to London , 1 to Paris and 2 to New York . But when I pass single value for predictions from model I gives city name New York and it assigns the 0 to it . How it shall remains same , I want that if New York values assigns 2 by label encoder in training phase, it should assign 2 again at the predictions .

Code
from sklearn.preprocessing import LabelEncoder
labelencoder=LabelEncoder()
df['city']=labelencoder.fit_transform(df['city'])
Feb 22, 2022 in Machine Learning by Dev
• 6,000 points
1,550 views

1 answer to this question.

0 votes

I am creating a dummy data set by using list and using the zip function.

city = ['London','Paris','New York ']
continent = ['Europe', 'Europe' ,'North America']

data = list(zip(city, continent))
data

Output

[('London', 'Europe'), ('Paris', 'Europe'), ('New York ', 'North America')]

Converting the data set into data frame

import pandas as pd
from sklearn.preprocessing import LabelEncoder
labelencoder=LabelEncoder()
df= pd.DataFrame(data, columns=['city', 'continent'])
df
df['label'] = labelencoder.fit_transform(df['city'])
df
City        Continent
London      Europe
Paris       Europe
New York   North America

You need to use fit_transform to fit the encoder and then transform the data. This will encode the labels as you want and will not re-fit the encoder.
Output

City                   Continent             label
London                Europe                  0
Paris                 Europe                  2
New York          North America               1

answered Feb 22, 2022 by Nandini
• 5,480 points

Related Questions In Machine Learning

+1 vote
1 answer

how to analysis the heatmap to find the correlation

Hi @Vikas, there are 5 simple steps ...READ MORE

answered Sep 30, 2019 in Machine Learning by Vishal
10,547 views
+1 vote
1 answer

​In ANN how the weight gets selected by the model INITIALLY?

Hi@Nandini, When you add your NN layer, it ...READ MORE

answered May 9, 2020 in Machine Learning by MD
• 95,460 points
717 views
0 votes
1 answer

How to import the BatchNormalization function in Keras?

Hi@akhtar, The general use case is to use ...READ MORE

answered Jul 29, 2020 in Machine Learning by MD
• 95,460 points
3,642 views
0 votes
1 answer

AttributeError: module 'numpy' has no attribute '__version__'

Hi@akhtar, To avoid this error you can use ...READ MORE

answered Apr 20, 2020 in Python by MD
• 95,460 points
21,138 views
+1 vote
1 answer

module 'numpy' has no attribute 'unit8'

Hi@akhtar, You have used unit8 in your code. ...READ MORE

answered Jun 23, 2020 in Python by MD
• 95,460 points
15,690 views
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by MD 2,060 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika 2,861 views
0 votes
1 answer

Leela Chess Zero: how large is the probability vector in the output layer?

The next move's probability vector (called the ...READ MORE

answered Mar 9, 2022 in Machine Learning by Nandini
• 5,480 points
669 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP