-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreProcess.py
118 lines (118 loc) · 3.93 KB
/
preProcess.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def z_norm(result):\n",
" result_mean = result.mean()\n",
" result_std = result.std()\n",
" result -= result_mean\n",
" result /= result_std\n",
" return result, result_mean\n",
"\n",
"def get_split_prep_data(train_start, train_end,\n",
" test_start, test_end):\n",
" data = gen_wave()\n",
" print(\"Length of Data\", len(data))\n",
"\n",
" # train data\n",
" print (\"Creating train data...\")\n",
"\n",
" result = []\n",
" for index in range(train_start, train_end - sequence_length):\n",
" result.append(data[index: index + sequence_length])\n",
" result = np.array(result) # shape (samples, sequence_length)\n",
" result, result_mean = z_norm(result)\n",
"\n",
" print (\"Mean of train data : \", result_mean)\n",
" print (\"Train data shape : \", result.shape)\n",
"\n",
" train = result[train_start:train_end, :]\n",
" np.random.shuffle(train) # shuffles in-place\n",
" X_train = train[:, :-1]\n",
" y_train = train[:, -1]\n",
"\n",
" # test data\n",
" print (\"Creating test data...\")\n",
"\n",
" result = []\n",
" for index in range(test_start, test_end - sequence_length):\n",
" result.append(data[index: index + sequence_length])\n",
" result = np.array(result) # shape (samples, sequence_length)\n",
" result, result_mean = z_norm(result)\n",
"\n",
" print (\"Mean of test data : \", result_mean)\n",
" print (\"Test data shape : \", result.shape)\n",
"\n",
" X_test = result[:, :-1]\n",
" y_test = result[:, -1]\n",
"\n",
" print(\"Shape X_train\", np.shape(X_train))\n",
" print(\"Shape X_test\", np.shape(X_test))\n",
"\n",
" X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))\n",
" X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))\n",
"\n",
" return X_train, y_train, X_test, y_test\n",
"\n",
"def flatten(X):\n",
" '''\n",
" Flatten a 3D array.\n",
" \n",
" Input\n",
" X A 3D array for lstm, where the array is sample x sequence length x features.\n",
" \n",
" Output\n",
" flattened_X A 2D array, sample x features.\n",
" '''\n",
" flattened_X = np.empty((X.shape[0], X.shape[2])) # sample x features array.\n",
" for i in range(X.shape[0]):\n",
" flattened_X[i] = X[i, (X.shape[1]-1), :]\n",
" return(flattened_X)\n",
"\n",
"\n",
"\n",
"def kde_sklearn(x, bandwidth=0.2, **kwargs):\n",
"\tx_grid = np.linspace(x.min() - 0.9*x.min(), x.max() + x.max(), 500)\n",
"\t\"\"\"Kernel Density Estimation with Scikit-learn\"\"\"\n",
"\tkde_skl = KernelDensity(bandwidth=bandwidth, **kwargs)\n",
"\tkde_skl.fit(x[:, np.newaxis])\n",
"\t# score_samples() returns the log-likelihood of the samples\n",
"\tlog_pdf = kde_skl.score_samples(x_grid[:, np.newaxis])\n",
"\treturn np.exp(log_pdf), x_grid\n",
" \n",
"def FindThreshold(x,h,p):\n",
" tau=0\n",
" x.sort() \n",
" for i in range(len(x)):\n",
" int_K = integrate.quad(lambda s: (1/(h*np.sqrt(2*np.pi)))*np.exp(-0.5*(s-p)/h), (i-1)/len(x), i/len(x))\n",
" tau=tau+int_K[0]*x[i]\n",
" return tau"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}