Skip to content

Instantly share code, notes, and snippets.

@suhaskv
suhaskv / threshold_search.py
Created January 6, 2021 09:45
VSB Power Line Blog - Best threshold selection
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
@suhaskv
suhaskv / model_lstm.py
Created January 6, 2021 09:33
VSB Power Line Blog - DNN implementation
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
@suhaskv
suhaskv / raffel_attention.py
Created January 6, 2021 08:26
VSB Power Line Blog - Raffel's Attention layer implementation
# 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
@suhaskv
suhaskv / bi_gru.py
Created January 5, 2021 16:49
VSB Power Line Blog - Bi-directional GRU implementation
bi_lstm_2 = Bidirectional(GRU(64, return_sequences=True), merge_mode='concat')(bi_lstm_1)
@suhaskv
suhaskv / bi_lstm.py
Created January 5, 2021 13:37
VSB Power Line Blog - Bi-directional LSTM implementation
# 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)
@suhaskv
suhaskv / svd_entropy.py
Created January 2, 2021 07:19
VSB Power Line Blog - SVD Entropy
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
@suhaskv
suhaskv / permutation_entropy.py
Created January 2, 2021 06:47
VSB Power Line Blog - Permutation Entropy
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
@suhaskv
suhaskv / higuchi_fd.py
Created January 1, 2021 18:13
VSB Power Line Blog - Higuchi FD
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]
@suhaskv
suhaskv / dfa_fd.py
Created January 1, 2021 14:15
VSB Power Line Blog - Detrended Fluctuation Analysis FD
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)
@suhaskv
suhaskv / katz_fd.py
Created January 1, 2021 14:11
VSB Power Line Blog - Katz FD
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]