def train_func(data): now = time.time() # clustering and save clusters = Clustering(data).clusters csv.write_file(cf.clusters_path, clusters) # make_rule and save rules = Rule(data, clusters).colection_rules() csv.write_file(cf.rule_path, rules) # Predict and save data x = Predict(data, clusters, rules) corrects = 0 edit = [] editCollection = [] for ix, record in enumerate(data): if int(record[0]) == x.predict(record, rules)["predict"]: corrects += 1 else: pr_rules = x.predict(record, rules)["rule"] cr_rules = x.get_rule_truth(record)["rule"] edit += x.detect_cluster(pr_rules, cr_rules, record[1:len(record)]) print(' Training Time: {:.2f}s'.format(time.time() - now)) print(" Correct: {}, Total: {}, Accuracy: {:.2f}%".format( corrects, len(data), 100 * corrects / len(data))) return 100 * corrects / len(data)
def predict(data): now = time.time() x = Predict(data, clusters, rules) corrects = 0 edit = [] editCollection = [] for ix, record in enumerate(data): if int(record[0]) == x.predict(record, rules)["predict"]: corrects += 1 else: pr_rules = x.predict(record, rules)["rule"] cr_rules = x.get_rule_truth(record)["rule"] edit += x.detect_cluster(pr_rules, cr_rules, record[1: len(record) ]) return 100*corrects / len(data)
def predict(data, slug): now = time.time() x = Predict(data, clusters, rules) corrects = 0 edit = [] editCollection = [] for ix, record in enumerate(data): if int(record[0]) == x.predict(record, rules)["predict"]: corrects += 1 else: pr_rules = x.predict(record, rules)["rule"] cr_rules = x.get_rule_truth(record)["rule"] edit += x.detect_cluster(pr_rules, cr_rules, record[1:len(record)]) print('\n __{} >> Predict time: {:.2f}s'.format(slug, time.time() - now)) print(" __Correct: {}, Total: {}, Accuracy: {:.2f}%\n".format( corrects, len(data), 100 * corrects / len(data)))
data = csv.read_file(cf.train_path, 'float') # clustering and save clusters = Clustering(data).clusters csv.write_file(cf.clusters_path, clusters) _2nd = time.time() print(' __Clustering time: {:.2f}s'.format(_2nd - _1st)) # make_rule and save rules = Rule(data, clusters).colection_rules() csv.write_file(cf.rule_path, rules) _3rd = time.time() print(' __Making rule time: {:.2f}s'.format(_3rd - _2nd)) # Predict and save data x = Predict(data, clusters, rules) corrects = 0 edit = [] editCollection = [] for ix, record in enumerate(data): if int(record[0]) == x.predict(record, rules)["predict"]: corrects += 1 else: pr_rules = x.predict(record, rules)["rule"] cr_rules = x.get_rule_truth(record)["rule"] edit += x.detect_cluster(pr_rules, cr_rules, record[1: len(record) ]) _4th = time.time() print(' __Predict time: {:.2f}s'.format(_4th - _3rd))
def train_func(data): now = time.time() # clustering and save clusters = Clustering(data).clusters csv.write_file(cf.clusters_path, clusters) # make_rule and save rules = Rule(data, clusters).colection_rules() csv.write_file(cf.rule_path, rules) # Predict and save data x = Predict(data, clusters, rules) corrects = 0 edit = [] editCollection = [] for ix, record in enumerate(data): if int(record[0]) == x.predict(record, rules)["predict"]: corrects += 1 else: pr_rules = x.predict(record, rules)["rule"] cr_rules = x.get_rule_truth(record)["rule"] edit += x.detect_cluster(pr_rules, cr_rules, record[1: len(record) ]) for e in edit: if 0.5 < e[2] and e[2] < 0.99 and e[2] + e[4] == 1: editCollection.append(e) def editCollectionSort(x): return x[2] editCollection.sort(key=editCollectionSort) # default fcm_path fmc = [[0.5 for i in range(2*cf.k_mean)] for i in range(len(data[0]) - 1)] csv.write_file(cf.fmc_path, fmc) edit = editCollection x = ha_predict.Predict(data, clusters, rules, fmc) correct = x.num_corrects() train_old = correct*100/len(data) for attr, flase, u_fasle, true, u_true in edit: fmc = csv.read_file(cf.fmc_path, 'float') if true > flase: fmc[attr][2*true] = u_true/u_fasle*fmc[attr][2*true - 1] else: fmc[attr][2*true + 1] = u_true/u_fasle*fmc[attr][2*true + 2] x = ha_predict.Predict(data, clusters, rules, fmc) y = x.num_corrects() if correct <= y: print(" -----> edited", (attr, flase, u_fasle, true, u_true ), "\n") correct = y csv.write_file(cf.fmc_path, fmc) else: print(" -----> rejected", (attr, flase, u_fasle, true, u_true ), "\n") print(" Time: {:.2f}s\n T1FS Train Accuracy: {:.3f}%\n HA_T2FS Accuracy: {:.3f}%".format(time.time() - now, train_old, correct*100/len(data))) return train_old, correct*100/len(data)