MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / calculate_variance

Function calculate_variance

machine_learning/linear_discriminant_analysis.py:127–152  ·  view source on GitHub ↗

Calculate the variance :param items: a list containing all items(gaussian distribution of all classes) :param means: a list containing real mean values of each class :param total_count: the number of all instances :return: calculated variance for considered dataset >>> item

(items: list, means: list, total_count: int)

Source from the content-addressed store, hash-verified

125
126# Calculate the variance
127def calculate_variance(items: list, means: list, total_count: int) -> float:
128 """
129 Calculate the variance
130 :param items: a list containing all items(gaussian distribution of all classes)
131 :param means: a list containing real mean values of each class
132 :param total_count: the number of all instances
133 :return: calculated variance for considered dataset
134
135 >>> items = gaussian_distribution(5.0, 1.0, 20)
136 >>> means = [5.011267842911003]
137 >>> total_count = 20
138 >>> calculate_variance([items], means, total_count)
139 0.9618530973487491
140 """
141 squared_diff = [] # An empty list to store all squared differences
142 # iterate over number of elements in items
143 for i in range(len(items)):
144 # for loop iterates over number of elements in inner layer of items
145 for j in range(len(items[i])):
146 # appending squared differences to 'squared_diff' list
147 squared_diff.append((items[i][j] - means[i]) ** 2)
148
149 # one divided by (the number of all instances - number of classes) multiplied by
150 # sum of all squared differences
151 n_classes = len(means) # Number of classes in dataset
152 return 1 / (total_count - n_classes) * sum(squared_diff)
153
154
155# Making predictions

Callers 1

mainFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected