def lvq1(training, prototypes, range_vector = [], limit = 1000, alpha = 0.02): times = 0 new_prototypes = [p for p in prototypes] while times < limit: sample = training[times%len(training)] knn_indexs = get_knn_indexs(1, sample, new_prototypes, euclidian_distance, range_vector) alpha_t = alpha * math.exp(-times/(limit/5)) for i in knn_indexs: prototype = new_prototypes[i] new_prototypes[i] = lvq1_ajust(sample, prototype, alpha_t) times = times + 1 return new_prototypes
def lvq2(training, prototypes, range_vector = [], limit = 1000, alpha = 0.02, window = 0.70): lvq1_prototypes = lvq1(training, prototypes, range_vector, limit) new_prototypes = lvq1_prototypes times = 0 while times < limit: sample = training[times%len(training)] knn_indexs = get_knn_indexs(2, sample, new_prototypes, euclidian_distance, range_vector) mi = new_prototypes[knn_indexs[0]] mj = new_prototypes[knn_indexs[1]] alpha_t = alpha * math.exp(-times/(limit/5)) if in_window(sample, mi, mj, window): if mi[-1] != mj[-1] and (mi[-1] == sample[-1] or mj[-1] == sample[-1]): new_prototypes[knn_indexs[0]] = lvq1_ajust(sample, mi, alpha_t) new_prototypes[knn_indexs[1]] = lvq1_ajust(sample, mj, alpha_t) times = times + 1 return new_prototypes