Skip to content

Instantly share code, notes, and snippets.

View MNoorFawi's full-sized avatar

Muhammad Noor Fawi MNoorFawi

View GitHub Profile
@MNoorFawi
MNoorFawi / pymc3_bayesian_nn.py
Last active July 1, 2023 08:19
bayesian neural network using pymc3 library with residual block and dropout
import pickle
import numpy as np
import theano
import pymc3 as pm
def relu(x):
return pm.math.maximum(0, x)
@MNoorFawi
MNoorFawi / priority_queue.c
Created August 21, 2020 18:08
Priority queue data structure implementation in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRLEN 20
struct linked_queue {
char data[STRLEN];
float priority;
struct linked_queue * next;
};
@MNoorFawi
MNoorFawi / stack.c
Created August 21, 2020 18:06
Stack data structure implementation in C
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void create_stack(stack * st) {
st -> indx = -1;
st -> size = 0;
}
// check empty
@MNoorFawi
MNoorFawi / range_binary_search.c
Created August 3, 2020 01:14
Range Binary Search in C
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;
}
@MNoorFawi
MNoorFawi / bit_count.sql
Created May 29, 2020 22:34
Implementing bit count function in PostgreSQL database
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;
@MNoorFawi
MNoorFawi / hamming_dist.sql
Created May 29, 2020 22:32
Hamming Distance in PostgreSQL Database
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) +
@MNoorFawi
MNoorFawi / recommender_system.cypher
Last active May 29, 2020 22:27
recommender system in Neo4j using Jaccard Index
// 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
@MNoorFawi
MNoorFawi / trn_tst_preprocess.jl
Created May 29, 2020 22:12
preprocessing train and test data frames in Julia (v0.6.4)
# 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
@MNoorFawi
MNoorFawi / single_var_model.R
Created May 29, 2020 21:41
Single Variable Model in R
## 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)]
@MNoorFawi
MNoorFawi / one_hot_encode.R
Created May 29, 2020 21:33
One-Hot Encoding in R
### 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)