Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

label encoder python

from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()

# apply on df
df['status'] = encoder.fit_transform(df['status'])
# can allso use the pandas.map
df['status'] = df['status'].map(lambda x: 1 if x=='Placed' else 0)

#apply on np.array
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [1])], remainder='passthrough')
X = np.array(ct.fit_transform(X))
Comment

Label Encoding in python

##We apply Label Encoding on black Friday dataset on the target column which is Species. It contains three species Iris-setosa, Iris-versicolor, Iris-virginica. 

# Import libraries
import numpy as np
import pandas as pd
 
# Importing dataset
df = pd.read_csv('../../data/blackFriday.csv')

#Cheking out the unique values in your dataset
df['Age'].unique()

# Import label encoder
from sklearn import preprocessing
 
# label_encoder object knows how to understand word labels.
label_encoder = preprocessing.LabelEncoder()
 
# Encode labels in column 'Age'.
df['Age']= label_encoder.fit_transform(df['Age'])
 
df['Age'].unique()
Comment

how to use label encoding in python


obj_df["body_style"] = obj_df["body_style"].astype('category')
obj_df.dtypes

obj_df["body_style_cat"] = obj_df["body_style"].cat.codes
obj_df.head()
Comment

PREVIOUS NEXT
Code Example
Python :: flask port 
Python :: color python 
Python :: 1d array to one hot 
Python :: requests.packages.urllib3.util.retry could not be resolved from source 
Python :: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, warn=True) 
Python :: how to remove items from list in python 
Python :: how to open pickle file 
Python :: print typeof in python 
Python :: how to import from parent directory 
Python :: python get parent directory 
Python :: python web parsing 
Python :: python collections Counter sort by key 
Python :: pyspark dropna in one column 
Python :: python find object with attribute in list 
Python :: find all color in image python 
Python :: tuple length in python 
Python :: send telegram bot message python 
Python :: loop throughthe key and the values of a dict in python 
Python :: python grid 
Python :: what is wsgi 
Python :: import get object 
Python :: pygame tick time 
Python :: feature importance naive bayes python 
Python :: SystemError: tile cannot extend outside image 
Python :: heatmap of pandas dataframe with seaborn 
Python :: number system conversion python 
Python :: python string vs byte string 
Python :: python keep value recursive function 
Python :: gematria python 
Python :: python print datetime 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =