This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
best_threshold = 0 | |
best_score = 0 | |
thresh_arr = np.linspace(0, 0.99, 100) | |
y_val = y_train | |
for thresh in thresh_arr: | |
preds_train_target = (preds_train_target_proba > thresh).astype(np.int).flatten() | |
score = matthews_corrcoef(y_val, preds_train_target) | |
if score > best_score: | |
best_threshold = thresh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def model_lstm(input_shape, feat_shape): | |
""" | |
Builds the Neural Network Architecture. | |
Following is the architecture that is built: | |
* Layer 1 | |
* LSTM | |
* Bidirectional LSTM - 128 neurons | |
* Bidirectional LSTM - 64 neurons | |
* Attention layer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://keras.io/layers/writing-your-own-keras-layers/ | |
class Attention(Layer): | |
""" | |
Performs basic attention layer operation. | |
References | |
---------- | |
.. [1] https://arxiv.org/pdf/1512.08756.pdf | |
.. [2] https://www.kaggle.com/qqgeogor/keras-lstm-attention-glove840b-lb-0-043 | |
.. [3] https://www.kaggle.com/tarunpaparaju/vsb-competition-attention-bilstm-with-features/notebook?scriptVersionId=10690570 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bi_lstm_2 = Bidirectional(GRU(64, return_sequences=True), merge_mode='concat')(bi_lstm_1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# training data input_shape = (2904, 160, 57) | |
# test data input_shape = (6779, 160, 57) | |
inp = Input(shape=(input_shape[1], input_shape[2],)) | |
# training data feature shape = (2904, 98) | |
# test data feature shape = (6779, 98) | |
feat = Input(shape=(feat_shape[1],)) | |
bi_lstm_1 = Bidirectional(LSTM(128, return_sequences=True), merge_mode='concat')(inp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _embed(x, order=3, delay=1): | |
N = len(x) | |
# In our case, N-(order-1)*delay = 800000 - (3-1)*1 = 799998 | |
# Y.shape = 3x799998 | |
Y = np.zeros((order, N - (order - 1) * delay)) | |
for i in range(order): | |
# Y[0] = x[0:799998], Y[1] = x[1:799999], Y[2] = x[3:800000] | |
Y[i] = x[(i * delay) : (i * delay) + Y.shape[1]].T | |
# Y.T[0] = [x[0],x[1],x[2]], Y.T[1] = [x[1],x[2],x[3]], ... , Y.T[799998] = [x[799998],x[799999],x[800000]] | |
return Y.T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _embed(x, order=3, delay=1): | |
N = len(x) | |
# In our case, N-(order-1)*delay = 800000 - (3-1)*1 = 799998 | |
# Y.shape = 3x799998 | |
Y = np.zeros((order, N - (order - 1) * delay)) | |
for i in range(order): | |
# Y[0] = x[0:799998], Y[1] = x[1:799999], Y[2] = x[3:800000] | |
Y[i] = x[(i * delay) : (i * delay) + Y.shape[1]].T | |
# Y.T[0] = [x[0],x[1],x[2]], Y.T[1] = [x[1],x[2],x[3]], ... , Y.T[799998] = [x[799998],x[799999],x[800000]] | |
return Y.T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _linear_regression(x, y): | |
n_times = x.size | |
sx2 = 0 | |
sx = 0 | |
sy = 0 | |
sxy = 0 | |
for j in range(n_times): | |
sx2 += x[j] ** 2 | |
sx += x[j] | |
sxy += x[j] * y[j] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def logarithmic_n(min_n, max_n, factor): | |
# stop condition: min * f^x = max | |
# => f^x = max/min | |
# => x = log(max/min) / log(f) | |
max_i = int(np.floor(np.log(1.0 * max_n / min_n) / np.log(factor))) | |
ns = [min_n] | |
for i in range(max_i + 1): | |
n = int(np.floor(min_n * (factor ** i))) | |
if n > ns[-1]: | |
ns.append(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def katz_fd(x): | |
x = np.array(x) | |
# Get the difference between consecutive elements of x | |
dists = np.abs(np.ediff1d(x)) | |
# Get the sum of all the differences | |
l1 = dists.sum() | |
# Divide each value of dists (diff. b/w consec. elements) with its mean value and take the log10 | |
ln = np.log10(np.divide(l1, dists.mean())) | |
# Get the difference between the first value of the sequence with all the elements of the sequence | |
aux_d = x - x[0] |
NewerOlder