https://github.com/freakwill/ezstat
OO For easy statistics
https://github.com/freakwill/ezstat
Last synced: about 1 month ago
JSON representation
OO For easy statistics
- Host: GitHub
- URL: https://github.com/freakwill/ezstat
- Owner: Freakwill
- License: mit
- Created: 2021-08-30T03:39:15.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-08-30T03:45:30.000Z (almost 5 years ago)
- Last Synced: 2025-02-27T20:20:17.372Z (over 1 year ago)
- Language: Python
- Size: 21.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ezstat: Easy statistics
OO For easy statistics, inspired by `Statistics` class of [deap](https://deap.readthedocs.io/en/master/index.html)
It is really easy and awesome! Believe me!

## Introduction
`ezstat` is built for easy statistics, esp. for the history of iterations.
The main class `Statistics` just extends `dict{str:function}` (called statistics dict), function here will act on the object of statistics. The values of dict have not to be a function, if it is a string, then the object of method with the same name is applied.
Frankly, It just borrows the idea from the `Statistics` class of [deap](https://deap.readthedocs.io/en/master/index.html). But unlike the author of deap, I just create it a subclass of dict, need not define strange methods.
See the following example and function `_call`, the underlying implementation.
Pseudo code of `_call`:
```python
if s: str
f = getattr(obj, s)
if f: function
r = f()
else
r = f
elif s: function
r = s(obj)
elif s is number:
r = s
```
## Installation
`pip install ezstat`
## Examples
Example:
```python
>>> import numpy as np
>>> T = np.random.random((100,100))
>>> stat = Statistics({'mean': lambda x: np.mean(np.mean(x, axis=1)), 'max': lambda x: np.max(np.mean(x, axis=1)), 'shape':'shape'})
>>> print(stat(T))
>>> {'mean': 0.5009150557686407, 'max': 0.5748552862392957, 'shape': (100, 100)}
>>> print(stat(T, split=True)) # split the tuple if it needs
>>> {mean': 0.5009150557686407, 'max': 0.5748552862392957, 'shape-0': 100, 'shape-1': 100}
```
`MappingStatistics` is a subclass of `Statistics`. It just copes with iterable object, and maps the obect to an array by funcional attribute `key`.
Example:
```python
>>> stat = MappingStatistics('mean', {'mean':np.mean, 'max':np.max})
>>> print(stat(T))
>>> {'mean': 0.5009150557686407, 'max': 0.5748552862392957}
```
In the exmaple, 'mean', an attribute of T, maps T to a 1D array.
## Advanced Usage
`Statistics` acts on a list/tuple of objects iteratively, gets a series of results,
forming an object of `pandas.DataFrame`. In fact, it is insprited by `Statistics` class of third part lib [deap](https://deap.readthedocs.io/en/master/index.html). In some case, it collects a list of dicts of the statistics result for a series of objects. It is suggested to transform to DataFrame object.
```python
history = pd.DataFrame(columns=stat.keys())
for obj in objs:
history = history.append(stat(obj), ignore_index=True)
```
## To Do
- [ ] To define tuple of functions for the value of statistics dict.