Dataset heritate from list

This commit is contained in:
Benjamin Bertrand 2016-01-08 17:01:39 +03:00
parent 79f2fa3c9e
commit 33f49edc6e

View File

@ -7,9 +7,9 @@
# #
# #
from math import sqrt, cos, ceil from math import sqrt, ceil
class Dataset(): class Dataset(list):
""" Classe réprésentant un série statistique avec rendu latex """ """ Classe réprésentant un série statistique avec rendu latex """
def __init__(self, data = [], data_name = "Valeurs"): def __init__(self, data = [], data_name = "Valeurs"):
@ -19,9 +19,9 @@ class Dataset():
:param data: values of the data set :param data: values of the data set
:param data_name: name of the data set :param data_name: name of the data set
""" """
self.data = list(data) list.__init__(self, data)
self.data_name = data_name self_name = data_name
def add_data(self, data): def add_data(self, data):
"""Add datas to the data set """Add datas to the data set
@ -29,19 +29,16 @@ class Dataset():
:param data: datas :param data: datas
""" """
try: try:
self.data += data self += data
except TypeError: except TypeError:
self.data += [data] self += [data]
def sort(self, *args, **kwrds): def sort(self, *args, **kwrds):
""" Apply sort to data """ """ Apply sort to data """
self.data.sort(*args, **kwrds) self.sort(*args, **kwrds)
def __len__(self):
return len(self.data)
def sum(self): def sum(self):
return sum(self.data) return sum(self)
def mean(self): def mean(self):
return self.sum()/len(self) return self.sum()/len(self)
@ -49,10 +46,10 @@ class Dataset():
def deviation(self): def deviation(self):
""" Compute the deviation (not normalized) """ """ Compute the deviation (not normalized) """
mean = self.mean() mean = self.mean()
return sum([(x - mean)**2 for x in self.data]) return sum([(x - mean)**2 for x in self])
def variance(self): def variance(self):
return self.deviation()/len(self.data) return self.deviation()/len(self)
def sd(self): def sd(self):
""" Compute the standard deviation """ """ Compute the standard deviation """
@ -64,7 +61,7 @@ class Dataset():
:return: (min, Q1, Me, Q3, Max) :return: (min, Q1, Me, Q3, Max)
""" """
return (min(self.data) , self.quartile(1) , self.quartile(2) , self.quartile(3), max(self.data)) return (min(self) , self.quartile(1) , self.quartile(2) , self.quartile(3), max(self))
def quartile(self, quartile = 1): def quartile(self, quartile = 1):
""" """
@ -82,7 +79,7 @@ class Dataset():
""" """
position = self.posi_quartile(quartile)[0] position = self.posi_quartile(quartile)[0]
# À vérifier... # À vérifier...
return self.data[position] return self[position]
def posi_quartile(self, quartile = 1): def posi_quartile(self, quartile = 1):
@ -215,7 +212,7 @@ On a ainsi $Q_{q} = {val_q}$
""" """
d_per_line = len(self) // nbr_lines d_per_line = len(self) // nbr_lines
d_last_line = len(self) % d_per_line d_last_line = len(self) % d_per_line
splited_data = [self.data[x:x+d_per_line] for x in range(0, len(self.data), d_per_line)] splited_data = [self[x:x+d_per_line] for x in range(0, len(self), d_per_line)]
# On ajoute les éléments manquant pour la dernière line # On ajoute les éléments manquant pour la dernière line
if d_last_line: if d_last_line:
splited_data[-1] += [' ']*(d_per_line - d_last_line) splited_data[-1] += [' ']*(d_per_line - d_last_line)