Contents
Some list abbreviations:
head = lambda lst: lst[0]
tail = lambda lst: lst[1:]
last = lambda lst: lst[-1]
init = lambda lst: lst[:-1]
And for tuples:
fst = lambda lst: lst[0]
snd = lambda lst: lst[1]
Return -1 if x is smaller than zero. If x is equal to zero, return zero. Return +1 otherwise.
This method implements the sign function, which is defined as
Note that the abbreviation sgn is also available:
sgn := sign
Return the product of all list elements.
Return the span of a list.
Return the argument for which the value of a function is minimal.
The function f is evaluated at each element of the argument list arg. The value in arg which returns the lowest value of f is returned.
Parameters: |
|
---|
>>> l1, l2, f = [4,5,6], [1,2,3], lambda x,y: x**2-y**2
>>> argmin(lambda (x,y): f(x,y), itertools.product(l1, l2))
(4, 3)
Return the argument for which the value of a function is maximal.
The function f is evaluated at each element of the argument list arg. The value in arg which returns the lowest value of f is returned.
Parameters: |
|
---|
>>> l1, l2, f = [4,5,6], [1,2,3], lambda x,y: x**2-y**2
>>> argmax(lambda (x,y): f(x,y), itertools.product(l1, l2))
(6, 1)
Return the list, such that the elements sum up to s.
Parameters: |
|
---|---|
Returns: | Scaled list with sum(lst) = nrm |
>>> normalize([2.0,4.0, 6.0], 3.0)
[0.5, 1.0, 1.5]
>>> sum(normalize([2.0,4.0, 6.0], 3.0))
3.0
The Kullback-Leibler Divergence is defined as:
- ailib.KLD(p, q, r, save=True)¶
Return the continous Kullback-Leibler divergence.
q*(x) must be non-zero for all x in *r. If this doesn’t hold for q, then inf is returned. If the save flag is set, the support r is reduced to the support of q (thus q non-zero for all x in r’).
If you have lists instead of functions, use listFunc() and range().
Parameters:
- p ((Num b) :: a -> b) – pdf of the first distribution.
- q ((Num b) :: a -> b) – pdf of the second distribution.
- r ([a]) – Support.
- save (bool) – Flag, if support may be reduced.
Returns: KLD of p and q, measured at locations in r.
>>> KLD(lambda x: x, lambda x: x-1, [0,1,2,3,4], save=True) 5.4150374992788439>>> KLD(lambda x: x, lambda x: x-1, [0,1,2,3,4], save=True) inf
Time warping is a distance measurement for two vectors of different size.
- ailib.LTW(x, y, dist=<function euclidean at 0x3319500>)¶
Linear time warping.
Parameters:
- x ([a]) – First input vector.
- y ([b]) – Second input vector.
- dist ((Num c) => a -> b -> c) – Distance measurement for vector elements.
Returns: Linear time warping distance of x, y.
>>> LTW([1,2,3,4,5,6,7], [9,8,7,6]) 28.0Given two input vectors with lenghts .
For all average measurements, the argument lst must be a list of numerics. Note that functions don’t actually need to be implemented as presented here.
- ailib.mean(lst)¶
Return the arithmetic mean of all values of the given list.
- ailib.median(lst)¶
- Return the median value of the given list.
- ailib.gMean(lst)¶
Return the geometric mean of the given values.
- ailib.hMean(lst)¶
Return the harmonic mean of the given values.
- ailib.qMean(lst)¶
Return the quadratic mean of the given values.
- ailib.cMean(lst)¶
Return the cubic mean of the given values.
- ailib.genMean(lst, p)¶
Return the generalized mean of the given values.
The generalized mean with exponent p is defined as follows:
From this, one can clearly see the following equivalences:
mean = genMean(lst, 1.0) qMean = genMean(lst, 2.0) cMean = genMean(lst, 3.0)
- ailib.rms(lst)¶
Return the root mean square of the given values.
>>> rms = qMean
- ailib.midrange(lst)¶
Return the midrange of the given values.
Return the variance of the given list.
Generally, the variance is defined as:
Here, it is implemented using the arithmetic mean:
Return the residual sum of squares.
The list items are expected to be of the form (f(x),y).
Divide a list into multiple bins of equal intervalls.
For the type a there must be a float representation and it must be ordered. Specifically, the type must support the functions min, max and float.
Parameters: |
|
---|---|
Returns: | A list of bins, containing the data elements. |
>>> binning([1,2,3,4], 2, hist=False)
[[1,2],[3,4]]
Return the intervall boundaries used by binning.
The returned intervalls describe the lower and upper limit, whereas the lower limit is inclusive and the upper limit is inclusive only for the last intervall.
Parameters: |
|
---|---|
Returns: | List of intervalls. |
>>> binRange([1,2,3,4], 2, hist=False)
[(1.0,2.5),(2.5,4.0)]
Count the number of occurrences of tokens in a list.
Parameter: | data – List of tokens. |
---|---|
Returns: | Dict with token as key and relative frequency as value |
Return the token with the highest number of occurrences.
The tie-breaking rule is applied, if there are several tokens with identical number of occurrences. The tieBreaking function takes two arguments: The tokens with maximal votes and the token histogram. It has to select a single token as a winner. The default function is a random choice.
Parameters: |
|
---|---|
Returns: | Token of the data stream with the highest number of occurrences. |
>>> majorityVote([1,2,1], tieBreaking=lambda l,h: max(l))
1
>>> majorityVote([1,2,1,2,3], tieBreaking=lambda l,h: max(l))
2
So far, no wrapper is fully implemented. Use the distribution related methods of scipy.stats.