Skip to content

Instantly share code, notes, and snippets.

@yassu
Created February 8, 2015 00:44
Show Gist options
  • Save yassu/721656d9b25be6d3babd to your computer and use it in GitHub Desktop.
Save yassu/721656d9b25be6d3babd to your computer and use it in GitHub Desktop.
class for very simple statics
from copy import deepcopy
from math import sqrt
import numpy as np
from scipy import polyfit
from pylab import *
class SimpleAnalyser:
def __init__(self, datas):
self._datas = datas
@property
def datas(self):
return deepcopy(self._datas)
@property
def mean(self):
return np.mean(self.datas)
@property
def standard_deviation(self):
return sqrt(np.var(self._datas))
@property
def max(self):
return float(max(self.datas))
@property
def min(self):
return float(min(self.datas))
@property
def median(self):
return np.median(self.datas)
@property
def linregress(self):
xs = list(range(len(self.datas)))
return list(map(int, polyfit(xs, self.datas, 1).tolist()))
def _plot_data(self, plt):
plt.plot(range(len(self.datas)), self.datas, label='data')
def _plot_line_regression(self, plt):
loop, sedgement = self.linregress
xs = range(len(self.datas))
plt.plot(
xs, [loop * x + sedgement for x in xs],
'r-',
label='regression line'
)
def add_plot(self, plt):
self._plot_data(plt)
self._plot_line_regression(plt)
def __str__(self):
statics = (
('data' , self.datas),
('min' , self.min),
('median' , self.median),
('mean' , self.mean),
('max' , self.max),
('standard deviation' , self.standard_deviation))
text = ''
for s, val in statics:
text += "{}: {}\n".format(s, val)
return text
def __list__(self):
return list(self._datas)
if __name__ == '__main__':
import matplotlib.pyplot as plt
datas = SimpleAnalyser([randint(0, 100) for i in range(10)])
print(str(datas))
datas.add_plot(plt)
plt.show()
@yassu
Copy link
Author

yassu commented Feb 8, 2015

Note that this gist depends on matplotlib and scipy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment