Skip to content

Instantly share code, notes, and snippets.

View richwandell's full-sized avatar
:shipit:
Coding

Rich Wandell richwandell

:shipit:
Coding
View GitHub Profile
function kMeans(c){
var center, i, j, new_clusters = [], new_error = centroids.map(function(){return 0;}),
x, y, clu, dp, closest, closest_distance, distance, same;
centroids = [];
//Determine the center point of each cluster
for(i = 0; i < c.length; i++){
clu = c[i];
x = 0;
y = 0;
for(j = 0; j < clu.length; j++){
@richwandell
richwandell / knn.js
Last active May 6, 2017 17:15
k nearest neighbor
function knn(s){
//Normalize sample
var s0 = (s[0] - min_X) / (max_X - min_X);
var s1 = (s[1] - min_X) / (max_X - min_X);
//Find nearest neighbors
var c1 = false;
var c2 = false;
var distance;
var linreg = (function(numbers){
var xsum = 0, ysum = 0, xysum = 0, x2sum = 0, m, b;
for(var i = 0; i < numbers.length; i++){
xsum += i;
ysum += numbers[i];
xysum += i * numbers[i];
x2sum += Math.pow(i, 2);
}
m = (numbers.length * xysum - xsum * ysum) / (numbers.length * x2sum - Math.pow(xsum, 2));
b = (ysum - m * xsum) / numbers.length;
@richwandell
richwandell / kalman.js
Created August 22, 2016 00:37
Simple 1d kalman filter
var cest, pest = 0, mea, kg, eest = 1, emea = 1;
function kalman(m){
mea = m;
kg = eest / (eest + emea);
cest = pest + kg * (mea - pest);
pest = cest;
eest = (emea * eest) / (emea + eest);
return cest;
}