Created
July 6, 2016 08:01
-
-
Save wassname/9e9ac8159328123aff1ee48756d440a9 to your computer and use it in GitHub Desktop.
describe statistics of data in javascript (like pandas.DataFrame.describe)
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 {jStat} from 'jstat' | |
/** | |
* describe statistics of data | |
* like: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.describe.html | |
* @param {Object} data - e.g. {x:[1,2,3],y:[6,7,8]} | |
* @return {Object} - e.g. {x:{mean:2,...},y:{...}} | |
*/ | |
export function describe(data){ | |
var labels = { | |
'count':d=>d.length, | |
'mean':d=>jStat.mean(d), | |
'stdev':d=>jStat.stdev(d), | |
'min':d=>jStat.min(d), | |
'max':d=>jStat.max(d), | |
'quart_lower':d=>jStat.quartiles(d)[0], | |
'quart_median':d=>jStat.quartiles(d)[1], | |
'quart_upper':d=>jStat.quartiles(d)[2], | |
} | |
var descriptions = {} | |
for (var col in data){ | |
descriptions[col]={} | |
var series = data[col] | |
for (var label in labels){ | |
var f = labels[label] | |
descriptions[col][label]=f(series) | |
} | |
} | |
return descriptions | |
} | |
// test | |
var data={x:[],y:[]} | |
for (var i=0;i<200;i++){ | |
data.x.push(Math.random()) | |
data.y.push(Math.random()*10-2) | |
} | |
var desc = describe(data) | |
console.log(JSON.stringify(desc,null,4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment