This file contains hidden or 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
# desired matrix: | |
# 1 1 | |
# 1 2 | |
# 1 3 | |
# 2 1 | |
# 2 2 | |
# 2 3 | |
# 2 4 | |
# 3 1 | |
# 3 2 |
This file contains hidden or 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 sample(x): | |
""" | |
1 | |
2 3 | |
4 5 6 7 | |
= | |
4 2 6 1 3 5 7 | |
>>> list(sample(['a', 'b', 'c', 'd', 'e', 'f', 'g'])) | |
[('d', 1), ('b', 2), ('f', 2), ('a', 3), ('c', 3), ('e', 3), ('g', 3)] | |
""" |
This file contains hidden or 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
# 1 line | |
function(x) sum(head(tail(pmin(cummax(x),rev(cummax(rev(x))))-x,-1),-1)) | |
# or with fancy ascii visualisation! | |
# | |
# > water(x) | |
# $water | |
# [1] 10 | |
# | |
# $vis |
This file contains hidden or 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
wapply <- function(x, window=1:10, fun=mean) { | |
res <- rep(NA, length(x)) | |
names(res) <- names(x) | |
i <- max(window) + 1 | |
while(i <= length(x)) { | |
res[i] <- fun(x[i - window]) | |
i <- i + 1 |
This file contains hidden or 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
# convert | |
# 7, 7, 6, 5, 5, 3, 3, 2, 0, 0 -> | |
# 1 1 2 3 3 4 4 5 NA NA | |
x <- c(7,7,6,5,5,3,3,2,0,0) | |
ifelse(x,cumsum(c(1,abs(sign(diff(x))))),NA) | |
# [1] 1 1 2 3 3 4 4 5 NA NA |
This file contains hidden or 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
library(homomorpheR) | |
key.pair <- PaillierKeyPair$new(modulusBits=1024) | |
encrypt <- function(x) key.pair$pubkey$encrypt(x) | |
decrypt <- decrypt <- function(x) key.pair$getPrivateKey()$decrypt(x) | |
"%+%" <- function(a, b) key.pair$pubkey$add(a, b) | |
20 == decrypt(encrypt(1) %+% encrypt(19)) # TRUE |
This file contains hidden or 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 set_weights(intercept, coef, classes, model=linear_model.SGDClassifier()): | |
model.intercept_ = intercept | |
model.coef_ = coef | |
model.classes_ = classes | |
return model |
This file contains hidden or 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 batches(X, y, batch_size, randomise): | |
rows = X.shape[0] | |
i = np.arange(rows) | |
if randomise: | |
np.random.shuffle(i) | |
splits = rows / batch_size | |
for x_batch, y_batch in zip(np.array_split(X[i, ], splits), | |
np.array_split(y[i], splits)): | |
yield x_batch, y_batch |
This file contains hidden or 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 batch_update(model, X, y, classes, batch_size, randomise): | |
if batch_size is None: | |
batch_size = X.shape[0] | |
for x_batch, y_batch in batches(X, y, batch_size, randomise): | |
model.partial_fit(x_batch, y_batch, classes) | |
return model.intercept_, model.coef_ |
This file contains hidden or 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 batch_train(model, X, y, classes, epochs, batch_size, randomise): | |
for _ in range(0, epochs): | |
batch_update(model, X, y, classes, batch_size, randomise) |