Label Encoding with user defined Function | Machine Learning | Preprocessing - P1
Topic to be Covered - Label Encoding using Functions
In machine learning, we usually deal with datasets which contains multiple labels in one or more than one columns. These labels can be in the form of words or numbers. To make the data understandable or in human readable form, the training data is often labeled in words.
Label Encoding refers to converting the labels into numeric form so as to convert it into the machine-readable form. Machine learning algorithms can then decide in a better way on how those labels must be operated. It is an important pre-processing step for the structured dataset in supervised learning.
Code Starts Here
===============
import pandas as pd
import numpy as np
train = pd.read_csv('train.csv')
train['Age'].fillna(train['Age'].mean,inplace=True)
train.drop(['Cabin'],axis=1,inplace=True)
train['Embarked'].fillna(train['Embarked'].mode()[0],inplace=True)
print(train.info())
print(train.head())
features = train.iloc[:].values
df = pd.DataFrame(features)
'''------------------------------- L A B E L E N C O D I N G------------------'''
Column Sex, Embarked,
from sklearn.preprocessing import LabelEncoder
encode = LabelEncoder()
features[:,4] = encode.fit_transform(features[:,4])
features[:,10] = encode.fit_transform(features[:,10])
df = pd.DataFrame(features)
from sklearn.preprocessing import LabelEncoder
encode = LabelEncoder()
features1 = train.iloc[:].values
label_index=[3,4,8,9,10]
df2= pd.DataFrame(features1)
def label_encode(list1,features1):
for i in range(len(label_index)):
for j in range(features1.shape[1] + 1):
if j == label_index[i]:
features1[:,j] = encode.fit_transform(features1[:,j])
label_encode(label_index,features1)
df3= pd.DataFrame(features1)
All Playlist of this youtube channel
====================================
1. Data Preprocessing in Machine Learning
• Data Preprocessing in Machine Learning| Li...
2. Confusion Matrix in Machine Learning, ML, AI
• Confusion Matrix in Machine Learning, ML, AI
3. Anaconda, Python Installation, Spyder, Jupyter Notebook, PyCharm, Graphviz
• Anaconda | Python Installation | Spyder | ...
4. Cross Validation, Sampling, train test split in Machine Learning
• Cross Validation | Sampling | train test s...
5. Drop and Delete Operations in Python Pandas
• Drop and Delete Operations in Python Pandas
6. Matrices and Vectors with python
• Matrices and Vectors with python
7. Detect Outliers in Machine Learning
• Detect Outliers in Machine Learning
8. TimeSeries preprocessing in Machine Learning
• TimeSeries preprocessing in Machine Learning
9. Handling Missing Values in Machine Learning
• Handling Missing Values in Machine Learning
10. Dummy Encoding Encoding in Machine Learning
• Label Encoding, One hot Encoding, Dummy En...
11. Data Visualisation with Python, Seaborn, Matplotlib
• Data Visualisation with Python, Matplotlib...
12. Feature Scaling in Machine Learning
• Feature Scaling in Machine Learning
13. Python 3 basics for Beginner
• Python | Python 3 Basics | Python for Begi...
14. Statistics with Python
• Statistics with Python
15. Sklearn Scikit Learn Machine Learning
• Sklearn Scikit Learn Machine Learning
16. Python Pandas Dataframe Operations
• Python Pandas Dataframe Operations
17. Linear Regression, Supervised Machine Learning
• Linear Regression | Supervised Machine Lea...
18 Interiew Questions on Machine Learning and Data Science
• Interview Question for Machine Learning, D...
19. Jupyter Notebook Operations
• Jupyter and Spyder Notebook Operations in ...