Hey, I cooked up an initial implementation for one-dimensional histogram_discrete().
Example: >> import numpy >> numpy.histogram_discrete([-1, 9, 9, 0, 3, 5, 3]) array([1, 1, 0, 0, 2, 0, 1, 0, 0, 0, 2]) >> numpy.histogram_discrete([-99999, 99999]) array([1, 0, 0, ..., 0, 0, 1]) Suggestions, criticism? :) Priit :)
From f322bfb9bb2d18a93eac37ba0947743b93f94a5b Mon Sep 17 00:00:00 2001 From: Priit Laes <pl...@plaes.org> Date: Fri, 13 Nov 2009 00:45:42 +0200 Subject: [PATCH] Initial implementation of histogram_discrete(). Signed-off-by: Priit Laes <pl...@plaes.org> --- numpy/lib/function_base.py | 4 ++- numpy/lib/src/_compiled_base.c | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletions(-) diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 76ea08d..0516d42 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4,7 +4,8 @@ __all__ = ['select', 'piecewise', 'trim_zeros', 'diff', 'gradient', 'angle', 'unwrap', 'sort_complex', 'disp', 'extract', 'place', 'nansum', 'nanmax', 'nanargmax', 'nanargmin', 'nanmin', 'vectorize', 'asarray_chkfinite', 'average', - 'histogram', 'histogramdd', 'bincount', 'digitize', 'cov', + 'histogram', 'histogram_discrete', 'histogramdd', 'bincount', + 'digitize', 'cov', 'corrcoef', 'msort', 'median', 'sinc', 'hamming', 'hanning', 'bartlett', 'blackman', 'kaiser', 'trapz', 'i0', 'add_newdoc', 'add_docstring', 'meshgrid', 'delete', 'insert', 'append', @@ -27,6 +28,7 @@ from numpy.core import atleast_1d, atleast_2d from numpy.lib.twodim_base import diag from _compiled_base import _insert, add_docstring from _compiled_base import digitize, bincount, interp as compiled_interp +from _compiled_base import histogram_discrete from arraysetops import setdiff1d from utils import deprecate import numpy as np diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c index 3a8193f..7076d1f 100644 --- a/numpy/lib/src/_compiled_base.c +++ b/numpy/lib/src/_compiled_base.c @@ -85,6 +85,50 @@ mnx (intp *i , intp len) return mn; } +/* + * arr_histogram_discrete is registered as histogram_discrete. + * + * histogram_discrete accepts one arguments containing a 1-d array of + * integers called as list. This function returns an array with a length + * of max(list) - min(list) containing counts of each integer in list. + * Self is not used. + */ +static PyObject * +arr_histogram_discrete(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) +{ + PyArray_Descr *type; + PyObject *list = NULL; + PyObject *lst=NULL, *ans=NULL; + intp *numbers, *ians, len , mxi, mni, ans_size; + int i; + static char *kwlist[] = {"list", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", + kwlist, &list)) { + goto fail; + } + if (!(lst = PyArray_ContiguousFromAny(list, PyArray_INTP, 1, 1))) { + goto fail; + } + len = PyArray_SIZE(lst); + numbers = (intp *) PyArray_DATA(lst); + mxi = mxx(numbers, len); + mni = mnx(numbers, len); + ans_size = (numbers[mxi] + 1) - numbers[mni]; + type = PyArray_DescrFromType(PyArray_INTP); + if (!(ans = PyArray_Zeros(1, &ans_size, type, 0))) { + goto fail; + } + ians = (intp *)(PyArray_DATA(ans)); + for (i = 0; i < len; i++) + ians[numbers[i] - numbers[mni]] += 1; + Py_DECREF(lst); + return ans; + +fail: + Py_XDECREF(lst); + return NULL; +} /* * arr_bincount is registered as bincount. @@ -889,6 +933,8 @@ static struct PyMethodDef methods[] = { METH_VARARGS | METH_KEYWORDS, NULL}, {"unpackbits", (PyCFunction)io_unpack, METH_VARARGS | METH_KEYWORDS, NULL}, + {"histogram_discrete", (PyCFunction)arr_histogram_discrete, + METH_VARARGS | METH_KEYWORDS, NULL}, {NULL, NULL, 0, NULL} /* sentinel */ }; -- 1.6.5.2
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion