-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathprocess.py
76 lines (58 loc) · 1.79 KB
/
process.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
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import pandas as pd
import os
# so scripts from other folders can import this file
dir_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
# normalize numerical columns
# one-hot categorical columns
def get_data():
df = pd.read_csv(dir_path + '/ecommerce_data.csv')
# just in case you're curious what's in it
# df.head()
# easier to work with numpy array
data = df.to_numpy()
# shuffle it
np.random.shuffle(data)
# split features and labels
X = data[:,:-1]
Y = data[:,-1].astype(np.int32)
# one-hot encode the categorical data
# create a new matrix X2 with the correct number of columns
N, D = X.shape
X2 = np.zeros((N, D+3))
X2[:,0:(D-1)] = X[:,0:(D-1)] # non-categorical
# one-hot
for n in range(N):
t = int(X[n,D-1])
X2[n,t+D-1] = 1
# method 2
# Z = np.zeros((N, 4))
# Z[np.arange(N), X[:,D-1].astype(np.int32)] = 1
# # assign: X2[:,-4:] = Z
# assert(np.abs(X2[:,-4:] - Z).sum() < 1e-10)
# assign X2 back to X, since we don't need original anymore
X = X2
# split train and test
Xtrain = X[:-100]
Ytrain = Y[:-100]
Xtest = X[-100:]
Ytest = Y[-100:]
# normalize columns 1 and 2
for i in (1, 2):
m = Xtrain[:,i].mean()
s = Xtrain[:,i].std()
Xtrain[:,i] = (Xtrain[:,i] - m) / s
Xtest[:,i] = (Xtest[:,i] - m) / s
return Xtrain, Ytrain, Xtest, Ytest
def get_binary_data():
# return only the data from the first 2 classes
Xtrain, Ytrain, Xtest, Ytest = get_data()
X2train = Xtrain[Ytrain <= 1]
Y2train = Ytrain[Ytrain <= 1]
X2test = Xtest[Ytest <= 1]
Y2test = Ytest[Ytest <= 1]
return X2train, Y2train, X2test, Y2test