Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: is there a frequency function?

Tags:

python

excel

vba

in excel there is a frequency function:

The Excel FREQUENCY function This useful function can analyse a series of values and summarise them into a number of specified ranges. For example the heights of some children can be grouped in to four categories of [Less than 150cm]; [151 - 160cm]; [161 - 170cm]; [More than 170cm].

Would you like to learn more?

Excel 2003 Formulas by John Walkenbach (with CD)

FREQUENCY() is an unusual array function and it works differently to most other normal functions. It can not simply be typed into a cell or even entered properly using the Excel Function Wizard.

Note that this function does not analyse values into categories e.g. household expenditure into groups such as gas, electricity, water, rates etc. To perform this kind of analysis an Advanced Filter may be appropriate.

The frequency function has two arguments - the first is the range of cells containing values to be analysed; the second is the range of cells containing the upper values of each group banding. e.g. =FREQUENCY(A3:A120, B6:B10)

The second argument (the group upper limits) will exclude any values which exceed the highest category or banding. The function allows you to take account of this and extend the range of analysis to an additional category which contains all values that exceed the specified upper limit.

http://www.meadinkent.co.uk/xlfreq.htm

is there such a thing in python?

like image 887
Alex Gordon Avatar asked Sep 25 '26 16:09

Alex Gordon


1 Answers

import numpy
numpy.histogram( [ <data> ], [ <bins> ] )

Docs:

numpy.histogram(a, bins=10, range=None, normed=False, weights=None)

Compute the histogram of a set of data. Parameters:

a : array_like Input data. The histogram is computed over the flattened array.

bins : int or sequence of scalars, optional If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths.

range : (float, float), optional The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored.

normed : bool, optional If False, the result will contain the number of samples in each bin. If True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability mass function.

weights : array_like, optional An array of weights, of the same shape as a. Each value in a only contributes its associated weight towards the bin count (instead of 1). If normed is True, the weights are normalized, so that the integral of the density over the range remains 1

Returns:

hist : array The values of the histogram. See normed and weights for a description of the possible semantics.

bin_edges : array of dtype float Return the bin edges (length(hist)+1).

You may have to install numpy first.

like image 60
Katriel Avatar answered Sep 27 '26 05:09

Katriel