This function predicts new indexes(groups for our data) :param x_items: a list containing all items(gaussian distribution of all classes) :param means: a list containing real mean values of each class :param variance: calculated value of variance by calculate_variance function :param
(
x_items: list, means: list, variance: float, probabilities: list
)
| 154 | |
| 155 | # Making predictions |
| 156 | def predict_y_values( |
| 157 | x_items: list, means: list, variance: float, probabilities: list |
| 158 | ) -> list: |
| 159 | """This function predicts new indexes(groups for our data) |
| 160 | :param x_items: a list containing all items(gaussian distribution of all classes) |
| 161 | :param means: a list containing real mean values of each class |
| 162 | :param variance: calculated value of variance by calculate_variance function |
| 163 | :param probabilities: a list containing all probabilities of classes |
| 164 | :return: a list containing predicted Y values |
| 165 | |
| 166 | >>> x_items = [[6.288184753155463, 6.4494456086997705, 5.066335808938262, |
| 167 | ... 4.235456349028368, 3.9078267848958586, 5.031334516831717, |
| 168 | ... 3.977896829989127, 3.56317055489747, 5.199311976483754, |
| 169 | ... 5.133374604658605, 5.546468300338232, 4.086029056264687, |
| 170 | ... 5.005005283626573, 4.935258239627312, 3.494170998739258, |
| 171 | ... 5.537997178661033, 5.320711100998849, 7.3891120432406865, |
| 172 | ... 5.202969177309964, 4.855297691835079], [11.288184753155463, |
| 173 | ... 11.44944560869977, 10.066335808938263, 9.235456349028368, |
| 174 | ... 8.907826784895859, 10.031334516831716, 8.977896829989128, |
| 175 | ... 8.56317055489747, 10.199311976483754, 10.133374604658606, |
| 176 | ... 10.546468300338232, 9.086029056264687, 10.005005283626572, |
| 177 | ... 9.935258239627313, 8.494170998739259, 10.537997178661033, |
| 178 | ... 10.320711100998848, 12.389112043240686, 10.202969177309964, |
| 179 | ... 9.85529769183508], [16.288184753155463, 16.449445608699772, |
| 180 | ... 15.066335808938263, 14.235456349028368, 13.907826784895859, |
| 181 | ... 15.031334516831716, 13.977896829989128, 13.56317055489747, |
| 182 | ... 15.199311976483754, 15.133374604658606, 15.546468300338232, |
| 183 | ... 14.086029056264687, 15.005005283626572, 14.935258239627313, |
| 184 | ... 13.494170998739259, 15.537997178661033, 15.320711100998848, |
| 185 | ... 17.389112043240686, 15.202969177309964, 14.85529769183508]] |
| 186 | |
| 187 | >>> means = [5.011267842911003, 10.011267842911003, 15.011267842911002] |
| 188 | >>> variance = 0.9618530973487494 |
| 189 | >>> probabilities = [0.3333333333333333, 0.3333333333333333, 0.3333333333333333] |
| 190 | >>> predict_y_values(x_items, means, variance, |
| 191 | ... probabilities) # doctest: +NORMALIZE_WHITESPACE |
| 192 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, |
| 193 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 194 | 2, 2, 2, 2, 2, 2, 2, 2, 2] |
| 195 | |
| 196 | """ |
| 197 | # An empty list to store generated discriminant values of all items in dataset for |
| 198 | # each class |
| 199 | results = [] |
| 200 | # for loop iterates over number of elements in list |
| 201 | for i in range(len(x_items)): |
| 202 | # for loop iterates over number of inner items of each element |
| 203 | for j in range(len(x_items[i])): |
| 204 | temp = [] # to store all discriminant values of each item as a list |
| 205 | # for loop iterates over number of classes we have in our dataset |
| 206 | for k in range(len(x_items)): |
| 207 | # appending values of discriminants for each class to 'temp' list |
| 208 | temp.append( |
| 209 | x_items[i][j] * (means[k] / variance) |
| 210 | - (means[k] ** 2 / (2 * variance)) |
| 211 | + log(probabilities[k]) |
| 212 | ) |
| 213 | # appending discriminant values of each item to 'results' list |