To read the given data and perform Feature Encoding and Transformation process and save the data to a file.
STEP 1:Read the given Data. STEP 2:Clean the Data Set using Data Cleaning Process. STEP 3:Apply Feature Encoding for the feature in the data set. STEP 4:Apply Feature Transformation for the feature in the data set. STEP 5:Save the data to the file.
- Ordinal Encoding An ordinal encoding involves mapping each unique label to an integer value. This type of encoding is really only appropriate if there is a known relationship between the categories. This relationship does exist for some of the variables in our dataset, and ideally, this should be harnessed when preparing the data.
- Label Encoding Label encoding is a simple and straight forward approach. This converts each value in a categorical column into a numerical value. Each value in a categorical column is called Label.
- Binary Encoding Binary encoding converts a category into binary digits. Each binary digit creates one feature column. If there are n unique categories, then binary encoding results in the only log(base 2)ⁿ features.
- One Hot Encoding We use this categorical data encoding technique when the features are nominal(do not have any order). In one hot encoding, for each level of a categorical feature, we create a new variable. Each category is mapped with a binary variable containing either 0 or 1. Here, 0 represents the absence, and 1 represents the presence of that category.
• Log Transformation • Reciprocal Transformation • Square Root Transformation • Square Transformation
• Boxcox method • Yeojohnson method
import pandas as pd
df=pd.read_csv("/content/Encoding Data.csv")
df
from sklearn.preprocessing import LabelEncoder,OrdinalEncoder
pm=['Hot','Warm','Cold']
e1=OrdinalEncoder(categories=[pm])
e1.fit_transform(df[["ord_2"]])
df['bo2']=e1.fit_transform(df[["ord_2"]])
df
le=LabelEncoder()
dfc=df.copy()
dfc['ord_2']=le.fit_transform(dfc['ord_2'])
dfc
from sklearn.preprocessing import OneHotEncoder
ohe=OneHotEncoder(sparse=False)
df2=df.copy()
enc=pd.DataFrame(ohe.fit_transform(df2[["nom_0"]]))
df2=pd.concat([df2,enc],axis=1)
df2
pd.get_dummies(df2,columns=["nom_0"])
pip install --upgrade category_encoders
from category_encoders import BinaryEncoder
df=pd.read_csv("/content/data.csv")
df
be=BinaryEncoder()
nd=be.fit_transform(df['Ord_2'])
df
dfb=pd.concat([df,nd],axis=1)
dfb
from category_encoders import TargetEncoder
te=TargetEncoder()
CC=df.copy()
new=te.fit_transform(X=CC["City"],y=CC["Target"])
CC=pd.concat([CC,new],axis=1)
CC
import pandas as pd
from scipy import stats
import numpy as np
df=pd.read_csv("/content/Data_to_Transform.csv")
df
df.skew()
np.log(df["Highly Positive Skew"])
np.reciprocal(df["Moderate Positive Skew"])
np.sqrt(df["Highly Positive Skew"])
np.square(df["Highly Positive Skew"])
df["Highly Positive Skew_boxcox"], parameters=stats.boxcox(df["Highly Positive Skew"])
df
df.skew()
df["Moderate Negative Skew_yeojohnson"],parameters=stats.yeojohnson(df["Moderate Negative Skew"])
df.skew()
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt
sm.qqplot(df["Moderate Negative Skew"],line='45')
plt.show()
![](https://private-user-images.githubusercontent.com/119104009/322249502-db9383e3-08e3-4b0d-8cf9-7d730479ba15.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUyMDY1MzEsIm5iZiI6MTcyNTIwNjIzMSwicGF0aCI6Ii8xMTkxMDQwMDkvMzIyMjQ5NTAyLWRiOTM4M2UzLTA4ZTMtNGIwZC04Y2Y5LTdkNzMwNDc5YmExNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAxJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMVQxNTU3MTFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01OTU2MGZmZjZiNThmYzQ3ZDI4OTg2NWE5MTRjMWVhNGE4NTZiOTUzYjY0ZTE1NzkxMzIxY2QwMWMzOGFkNTQwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.N2r1t30Jvhbung3F0K721JzG6uyu0chJl81yj9M8AzE)
![](https://private-user-images.githubusercontent.com/119104009/322249556-35c3c643-d659-45a2-b41c-825f708bb77a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUyMDY1MzEsIm5iZiI6MTcyNTIwNjIzMSwicGF0aCI6Ii8xMTkxMDQwMDkvMzIyMjQ5NTU2LTM1YzNjNjQzLWQ2NTktNDVhMi1iNDFjLTgyNWY3MDhiYjc3YS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAxJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMVQxNTU3MTFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1kZjA4YjFkMDZlZTJmMTEzZWNjMzBlODU5OGMyMGNkZWI0ZjhjYTRkZTU1ZTBhZDE5NzgwNjYzMjJkMjdhNzJjJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.LC19NsWcJc5VSEXK3vfHrni9b_xybSyHenkaB-Wlo9s)
df["Moderate Negative Skew"]=qt.fit_transform(df[["Moderate Negative Skew"]])
sm.qqplot(df["Moderate Negative Skew"],line='45') plt.show()
<img src="https://github.com/Vanisha0609/EXNO-3-DS/assets/119104009/f7660654-7cda-403c-80d2-076de26be3ec" width="400" height="300">
df["Highly Negative Skew_1"]=qt.fit_transform(df[["Highly Negative Skew"]]) sm.qqplot(df["Highly Negative Skew"],line='45') plt.show()
<img src="https://github.com/Vanisha0609/EXNO-3-DS/assets/119104009/2a80dd6d-d895-4ff2-bcf2-366cb4a048be" width="400" heigth="300">
sm.qqplot(df["Highly Negative Skew_1"],line='45') plt.show()
<img src="https://github.com/Vanisha0609/EXNO-3-DS/assets/119104009/fa3211cb-c8b8-4b8d-8719-b944a75e3c8d" width="400" height="300">
# RESULT:
Thus the given data, Feature Encoding, Transformation process and save the data to a file was performed successfully.