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
import pickle | |
import numpy as np | |
import theano | |
import pymc3 as pm | |
def relu(x): | |
return pm.math.maximum(0, x) | |
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define STRLEN 20 | |
struct linked_queue { | |
char data[STRLEN]; | |
float priority; | |
struct linked_queue * next; | |
}; |
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include "stack.h" | |
void create_stack(stack * st) { | |
st -> indx = -1; | |
st -> size = 0; | |
} | |
// check empty |
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
void range_binary_search(int *array, int len, int val, int *res) { | |
int right = 0; | |
int left = 0; | |
int middle; | |
// not found | |
if(val < array[0] || val > array[len - 1]){ | |
right = 0; | |
left = -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
CREATE OR REPLACE FUNCTION bits_count(value bigint) RETURNS integer AS $$ | |
DECLARE i integer; | |
c integer; | |
bits BIT(25); | |
BEGIN | |
c := 0; | |
bits := value::BIT(25); | |
FOR i IN 1..LENGTH(bits) LOOP | |
IF substring(bits, i, 1) = B'1' THEN | |
c := c + 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
CREATE OR REPLACE FUNCTION hamming_distance( | |
A0 bigint, A1 bigint, A2 bigint, A3 bigint, | |
B0 bigint, B1 bigint, B2 bigint, B3 bigint | |
) | |
RETURNS integer AS $$ | |
BEGIN | |
RETURN | |
bits_count(A0 # B0) + | |
bits_count(A1 # B1) + | |
bits_count(A2 # B2) + |
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
// performing film recommendation system inside Neo4j database | |
// using Jaccard Index as similarity measurement | |
MATCH (c1:Customer)-[:RENTED]->(f:Film)<-[:RENTED]-(c2:Customer) | |
WHERE c1 <> c2 AND c1.customerID = "13" // an example of a user index | |
WITH c1, c2, COUNT(DISTINCT f) as intersection | |
MATCH (c:Customer)-[:RENTED]->(f:Film) | |
WHERE c in [c1, c2] | |
WITH c1, c2, intersection, COUNT(DISTINCT f) as union |
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
# one hot encoding string columns and normalizing numeric ones | |
# the function prepares new coming and/or test dataframe using existing/training dataframe | |
function preprocess(new::DataFrame, old::DataFrame) | |
dataType = describe(old) | |
x = DataFrame() | |
d = DataFrame() | |
str = dataType[dataType[:eltype] .== String, :variable] | |
num = dataType[(dataType[:eltype] .== Float64) .| (dataType[:eltype] .== Int64), :variable] | |
str = setdiff(str, [names(old)[end]]) | |
for i in str |
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
## How to create a single variable model in R | |
single_variable_model <- function(x, y, pos) { | |
if (class(x) %in% c("numeric", "integer")) { | |
# if numeric descretize it | |
probs <- unique(quantile(x, probs = seq(0.1, 1, 0.1), na.rm = T)) | |
x <- cut(x, breaks = probs, include.lowest = T) | |
} | |
prob_table <- table(as.factor(y), x) | |
vals <- unique(y) | |
neg <- vals[which(vals != pos)] |
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
### one-hot encoding | |
vars <- colnames(data) | |
## to one hot encode factor values and normalize numeric ones if needed | |
cat_vars <- vars[sapply(data[, vars], class) %in% | |
c("factor", "character", "logical")] | |
data2 <- data[, cat_vars] | |
for (i in cat_vars) { | |
dict <- unique(data2[, i]) | |
for (key in dict) { | |
data2[[paste0(i, "_", key)]] <- 1.0 * (data2[, i] == key) |
NewerOlder