-
Notifications
You must be signed in to change notification settings - Fork 0
/
machinelearning_customer_prediction_data_preprocessing.py
78 lines (49 loc) · 1.69 KB
/
machinelearning_customer_prediction_data_preprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""MachineLearning_customer_prediction_data_preprocessing.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1aHCBPlJC9ObpEYYBi3ZRXlXV3YkewW7-
# Data Preprocessing Tools
## Importing the libraries
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
"""## Importing the dataset"""
DataSet = pd.read_csv('Data.csv')
x = DataSet.iloc[:, :-1].values
y = DataSet.iloc[:, -1].values
print(x)
print(y)
"""## Taking care of missing data"""
from sklearn.impute import SimpleImputer
sc = SimpleImputer(missing_values = np.nan, strategy = 'mean')
x[:, 1:] = sc.fit_transform(x[:,1:])
print(x)
"""## Encoding categorical data
### Encoding the Independent Variable (country column)
"""
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers = [('encoder', OneHotEncoder(), [0])], remainder = 'passthrough')
x = np.array(ct.fit_transform(x))
print(x)
"""### Encoding the Dependent Variable (purchase column)"""
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)
print(y)
"""## Splitting the dataset into the Training set and Test set"""
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 1)
print(x_train)
print(y_train)
print(x_test)
print(y_test)
"""## Feature Scaling (leave the dummy variables)"""
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
x_train[:, 3:] = ss.fit_transform(x_train[:,3:])
x_test[:, 3:] = ss.transform(x_test[:,3:])
print(x_train)
print(x_test)