Skip to content

Instantly share code, notes, and snippets.

@Joelfranklin96
Last active May 27, 2020 00:28
Show Gist options
  • Save Joelfranklin96/d531e9cd48f0b49d3d9f91e194a1dbed to your computer and use it in GitHub Desktop.
Save Joelfranklin96/d531e9cd48f0b49d3d9f91e194a1dbed to your computer and use it in GitHub Desktop.
class2
# Defining the Class guassian
class guassian():
def __init__(self,mean=0,stdev=0):
self.mean = mean
self.stdev = stdev
# If dataset is passed as parameter rather than the 'mean' and 'stdev',
# we first read the data and then find the 'mean' and 'stdev'.
def read_text(self,file):
data = []
file1 = open(file,'r')
for x in file1.readlines():
data.append(int(x))
self.data = data
# The 'mean' is calculated.
def calculate_mean(self):
self.mean = sum(self.data)/len(self.data)
return self.mean
# The standard deviation is calculated.
def calculate_stdev(self,sample=True):
# note :-
# If 'sample' = True, the dataset belongs to a sample
# If 'sample' = False, the dataset belongs to a population.
if sample:
n = len(self.data)
else:
n = len(self.data) - 1
total = 0
for x in self.data:
total = total + ((x-self.mean)**2)
self.stdev = math.sqrt(total/n)
return self.stdev
# The Histogram is plotted.
def plot_histogram(self):
plt.hist(self.data)
plt.title('Histogram')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment