MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / get_data

Function get_data

ann_logistic_extra/process.py:17–66  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15# one-hot categorical columns
16
17def get_data():
18 df = pd.read_csv(dir_path + '/ecommerce_data.csv')
19
20 # just in case you're curious what's in it
21 # df.head()
22
23 # easier to work with numpy array
24 data = df.to_numpy()
25
26 # shuffle it
27 np.random.shuffle(data)
28
29 # split features and labels
30 X = data[:,:-1]
31 Y = data[:,-1].astype(np.int32)
32
33 # one-hot encode the categorical data
34 # create a new matrix X2 with the correct number of columns
35 N, D = X.shape
36 X2 = np.zeros((N, D+3))
37 X2[:,0:(D-1)] = X[:,0:(D-1)] # non-categorical
38
39 # one-hot
40 for n in range(N):
41 t = int(X[n,D-1])
42 X2[n,t+D-1] = 1
43
44 # method 2
45 # Z = np.zeros((N, 4))
46 # Z[np.arange(N), X[:,D-1].astype(np.int32)] = 1
47 # # assign: X2[:,-4:] = Z
48 # assert(np.abs(X2[:,-4:] - Z).sum() < 1e-10)
49
50 # assign X2 back to X, since we don't need original anymore
51 X = X2
52
53 # split train and test
54 Xtrain = X[:-100]
55 Ytrain = Y[:-100]
56 Xtest = X[-100:]
57 Ytest = Y[-100:]
58
59 # normalize columns 1 and 2
60 for i in (1, 2):
61 m = Xtrain[:,i].mean()
62 s = Xtrain[:,i].std()
63 Xtrain[:,i] = (Xtrain[:,i] - m) / s
64 Xtest[:,i] = (Xtest[:,i] - m) / s
65
66 return Xtrain, Ytrain, Xtest, Ytest
67
68
69def get_binary_data():

Callers 5

sklearn_ann.pyFile · 0.90
ann_train.pyFile · 0.90
ann_predict.pyFile · 0.90
get_binary_dataFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected