Skip to content

Commit

Permalink
percentile & quantile
Browse files Browse the repository at this point in the history
  • Loading branch information
janpipek committed Feb 26, 2016
1 parent 5779895 commit 1e24757
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions boadata/data/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@ def __setitem__(self, key, value, unwrap=True):


@DataObject.proxy_methods([
"sum", "std", "max", "mean"
"sum", "std", "max", "mean", "min"
])
class StatisticsMixin(object):
"""
"""
def quantile(self, n):
import boadata
import numpy as np
if hasattr(self.inner_data, "quantile"):
return self.inner_data.quantile()
if isinstance(n, list):
return [ self.quantile(x) for x in n ]
return boadata.wrap(self.inner_data.quantile(n), force=False)
if isinstance(self.inner_data, np.ndarray):
return boadata.wrap(np.percentile(self.inner_data, np.array(n) * 100.0), force=False)
else:
raise RuntimeError("Object does not support quantiles")

def percentile(self, n):
import numpy as np
return self.quantile(np.array(n) / 100.0)

def median(self):
return self.quantile(0.5)


@DataObject.proxy_methods([
Expand All @@ -47,8 +58,9 @@ class AsArrayMixin(object):
Including this mixin, you can use the object in matplotlib and seaborn
"""
def __array__(self):
return self.convert("numpy_array").inner_data
def __array__(self, *args):
import numpy as np
return np.array(self.convert("numpy_array").inner_data, *args)

def astype(self, *args):
return self.__array__().astype(*args)

0 comments on commit 1e24757

Please sign in to comment.