0% found this document useful (0 votes)
5 views4 pages

Data Preprocessing Techniques in Python

The document outlines a data pre-processing workflow using Python, specifically focusing on loading a dataset, handling missing values, encoding categorical variables, and normalizing data. It employs libraries such as pandas, numpy, and scikit-learn for various tasks including imputation, label encoding, one-hot encoding, and standardization. Finally, it splits the dataset into training and testing sets for model creation.

Uploaded by

guptaaman954862
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Data Preprocessing Techniques in Python

The document outlines a data pre-processing workflow using Python, specifically focusing on loading a dataset, handling missing values, encoding categorical variables, and normalizing data. It employs libraries such as pandas, numpy, and scikit-learn for various tasks including imputation, label encoding, one-hot encoding, and standardization. Finally, it splits the dataset into training and testing sets for model creation.

Uploaded by

guptaaman954862
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Pre-processing

Load the excel


C:\Noble\Training\Top Mentor\Training\Data Set\Data Preprocessing Data [Link]
Formula Y = mx+c

import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
import os

Load Data Set


[Link] ("C:\\Noble\\Training\\Top Mentor\\Training\\Data Set\\")
df1= pd.read_excel("Data Preprocessing Data [Link]")
print (df1)

Load independent variables and dependent variables to two separate arrays


Columns Independent variable – Country, Age , Salary
Dependent variable - Purchased

Create X

x = [Link][:,:-1].values
print (x)

Create Y
y = [Link][:,3].values
print (y)
Missing value treatment – Impute Values
Country – Most Frequent

from [Link] import SimpleImputer


imputer = SimpleImputer(missing_values=[Link], strategy='most_frequent')
imputer = [Link](x[:,0:1])
x [:,0:1]= [Link](x[:,0:1])
print (x)

Missing value treatment – Impute Values


Age – Constant

from [Link] import SimpleImputer


imputer = SimpleImputer(missing_values=[Link], strategy='constant', fill_value=40)
imputer = [Link](x[:,1:2])
x [:,1:2]= [Link](x[:,1:2])
print (x)

Missing value treatment – Impute Values


Salary – Mean

from [Link] import SimpleImputer


imputer = SimpleImputer(missing_values=[Link], strategy='mean')
imputer = [Link](x[:,2:3])
x [:,2:3]= [Link](x[:,2:3])
print (x)
Label Encoding
from [Link] import LabelEncoder
label_x = LabelEncoder()
x[:,0]= label_x.fit_transform(x[:,0])
print (x)

One hot encoding /Column Transformation


from [Link] import ColumnTransformer
from [Link] import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])],
remainder='passthrough')
x = [Link](ct.fit_transform(x))
print (x)

Label Encoding Y
label_y = LabelEncoder()
y = label_y.fit_transform(y)
print (y)

Standardization
from [Link] import StandardScaler
std_sca= StandardScaler()
x_STD = std_sca.fit_transform(x)
print ([Link](x_STD))

Normalization - MinMaxScaler
from [Link] import MinMaxScaler
Nm_x= MinMaxScaler()
x_NOR = Nm_x.fit_transform(x)
print ([Link](x_NOR))
Normalization
from [Link] import Normalizer
Nm_x= Normalizer()
x_NOR = Nm_x.fit_transform(x)
print ([Link](x_NOR))

Model Creation
from sklearn.model_selection import train_test_split
x_train,x_test, y_train,y_test = train_test_split (x_NOR,y,test_size = 0.2)
print ([Link](x_train), y_train)

You might also like