In [ ]:
#in class exercise (Data Preprocessing)
#Tasks Today
#upload and read customer analysis data into google colab
#preprocessing data so that it has right data format for future machine learning algorithms
#Details
#ID and segmentation are not needed
#before preprocessing: original customer analysis data
#after prprocessing: each customers info will be represented as proper numbers
In [ ]:
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
In [ ]:
df=pd.read_csv('customer_analysis.csv')
df.head()
Out[ ]:
ID Gender Ever_Married Age Graduated Profession Work_Experience Spending_Score Family_Size Segmentation
0 464716 Male Yes 43 Yes Marketing 0.0 High 6.0 D
1 467301 Female Yes 73 No Lawyer 1.0 Low 2.0 B
2 466937 Male Yes 62 No Entertainment 1.0 Average 2.0 B
3 462323 Female Yes 70 Yes Doctor 0.0 Average 2.0 C
4 464706 Female No 32 No Doctor 1.0 Low 3.0 D
In [ ]:
#select features we only want
df_new= df.iloc[:, 1:9]
df_new.head()
Out[ ]:
Gender Ever_Married Age Graduated Profession Work_Experience Spending_Score Family_Size
0 Male Yes 43 Yes Marketing 0.0 High 6.0
1 Female Yes 73 No Lawyer 1.0 Low 2.0
2 Male Yes 62 No Entertainment 1.0 Average 2.0
3 Female Yes 70 Yes Doctor 0.0 Average 2.0
4 Female No 32 No Doctor 1.0 Low 3.0
In [ ]:
df_new.dtypes
Out[ ]:
0
Gender object
Ever_Married object
Age int64
Graduated object
Profession object
Work_Experience float64
Spending_Score object
Family_Size float64

In [ ]:
#handle categorical variables
df_dummy= pd.get_dummies(data=df_new, drop_first=True, dtype=int)
df_dummy.head()
Out[ ]:
Age Work_Experience Family_Size Gender_Male Ever_Married_Yes Graduated_Yes Profession_Doctor Profession_Engineer Profession_Entertainment Profession_Executive Profession_Healthcare Profession_Homemaker Profession_Lawyer Profession_Marketing Spending_Score_High Spending_Score_Low
0 43 0.0 6.0 1 1 1 0 0 0 0 0 0 0 1 1 0
1 73 1.0 2.0 0 1 0 0 0 0 0 0 0 1 0 0 1
2 62 1.0 2.0 1 1 0 0 0 1 0 0 0 0 0 0 0
3 70 0.0 2.0 0 1 1 1 0 0 0 0 0 0 0 0 0
4 32 1.0 3.0 0 0 0 1 0 0 0 0 0 0 0 0 1
In [ ]:
#data normalization
scaler= MinMaxScaler()
df_dummy[['Age', 'Work_Experience', 'Family_Size']]= scaler.fit_transform(df_dummy[['Age', 'Work_Experience', 'Family_Size']])
df_dummy.head()
Out[ ]:
Age Work_Experience Family_Size Gender_Male Ever_Married_Yes Graduated_Yes Profession_Doctor Profession_Engineer Profession_Entertainment Profession_Executive Profession_Healthcare Profession_Homemaker Profession_Lawyer Profession_Marketing Spending_Score_High Spending_Score_Low
0 0.352113 0.000000 0.625 1 1 1 0 0 0 0 0 0 0 1 1 0
1 0.774648 0.071429 0.125 0 1 0 0 0 0 0 0 0 1 0 0 1
2 0.619718 0.071429 0.125 1 1 0 0 0 1 0 0 0 0 0 0 0
3 0.732394 0.000000 0.125 0 1 1 1 0 0 0 0 0 0 0 0 0
4 0.197183 0.071429 0.250 0 0 0 1 0 0 0 0 0 0 0 0 1
In [ ]: