Skip to content

Instantly share code, notes, and snippets.

View zkavtaskin's full-sized avatar
🤔
🧠🤖🧪

Zan Kavtaskin zkavtaskin

🤔
🧠🤖🧪
View GitHub Profile
@zkavtaskin
zkavtaskin / choice.js
Last active January 8, 2022 23:10
numpy.random.choice in JavaScript
/***
* This was inspired by Python https://numpy.org/doc/stable/reference/random/generated/numpy.random.choice.html
* Example usage choice([1, 5, 2], 2) <- Gives 1,5 and 2 same chance of occurance i.e. 1/3
* Example usage choice([1, 5, 2], 2, [0.05, 0.8, 0.15]) <-- gives 1 5% chance, 5 80% chance and 2 15% chance to show up
*/
function choice(events, size, probability) {
if(probability != null) {
const pSum = probability.reduce((sum, v) => sum + v);
if(pSum < 1 - Number.EPSILON || pSum > 1 + Number.EPSILON) {
throw Error("Overall probability has to be 1.");
@zkavtaskin
zkavtaskin / histogram.js
Last active January 8, 2022 23:10
Histogram function in JavaScript
/***
* Histogram "bins" numbers in the array X in to group ranges.
* Example usage histogram([1,5,2,4,2,5,2,3,1], 2) would return back [5,2,2] where bin ranges are [1-2, 3-4, 5-6] as bin range is 2
* Example usage histogram([1,5,2,4,2,5,2,3,1], 1) would return back [2,3,1,1,2] where bin ranges are [1,2,3,4,5] as bin range is 1
*/
function histogram(X, binRange) {
//inclusive of the first number
var max = Math.max(...X);
var min = Math.min(...X);
var len = max - min + 1;
@zkavtaskin
zkavtaskin / example.py
Last active January 11, 2022 13:31
Machine learning model partitioning for multi-tenant data in Python. Full write up can be found here: https://zankavtaskin.medium.com/machine-learning-model-partitioning-for-multi-tenant-data-in-python-7297dd7f6ba1
from sklearn.linear_model import LinearRegression
import pickle
import random
import numpy as np
"""
Sample data preparation for the partition.
--------
This example uses linear function y=mx+b where m is the tenant / company
specific bias and b is the error. LinearRegression is then used to find