Skip to content

Commit 86f00b2

Browse files
committed
Adding var_calc function
1 parent 007e59e commit 86f00b2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

machine_learning/linear discriminant analysis.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,27 @@ def prob_calc(instance_count: int, total_count: int) -> float:
103103
# number of instances in specific class divided by number of all instances
104104
probability = instance_count / total_count
105105
return probability
106+
107+
108+
# Calculating the variance
109+
def var_calc(items: list, means: list, total_count: int) -> float:
110+
""" This function calculates the variance
111+
:param items: a list containing all items(gaussian distribution of all classes)
112+
:param means: a list containing real mean values of each class
113+
:param total_count: the number of all instances
114+
:return: calculated variance for considered dataset
115+
"""
116+
117+
squared_diff = [] # An empty list to store all squared differences
118+
n_classes = len(means) # Number of classes in dataSet
119+
120+
# for loo iterates over number of elements in items
121+
for i in range(len(items)):
122+
# for loop iterates over number of elements in inner layer of items
123+
for j in range(len(items[i])):
124+
# appending squared differences to 'squared_diff' list
125+
squared_diff.append((items[i][j] - means[i]) ** 2)
126+
127+
# one divided by (the number of all instances - number of classes) multiplied by sum of all squared differences
128+
variance = 1 / (total_count - n_classes) * sum(squared_diff)
129+
return variance

0 commit comments

Comments
 (0)