def model_mil(train_bags, train_labels, test_bags, test_labels): start_time = datetime.now() classifiers = {} classifiers['MissSVM'] = misvm.MissSVM(kernel='linear', C=1.0, max_iters=20) classifiers['sbMIL'] = misvm.sbMIL(kernel='linear', eta=0.1, C=1e2) classifiers['SIL'] = misvm.SIL(kernel='linear', C=1.0) # Train/Evaluate classifiers perf = {} for algorithm, classifier in classifiers.items(): classifier.fit(train_bags, train_labels) predictions = classifier.predict(test_bags) if algorithm == 'sbMIL' or algorithm == 'MissSVM': predictions = np.sign(predictions) + 1 [[TN, FP], [FN, TP]] = confusion_matrix(test_labels, predictions) FDR = TP / (TP + FN) FAR = FP / (TN + FP) perf[algorithm] = {'FDR': FDR, 'FAR': FAR} print '[%s]algorithm %s done...' % (time.asctime( time.localtime(time.time())), algorithm) print '[%s]%s done for %d seconds...' % (time.asctime( time.localtime(time.time())), sys._getframe().f_code.co_name, (datetime.now() - start_time).seconds) return perf
def main(): # Load list of C4.5 Examples example_set = parse_c45('musk1') table = re.findall(r"<(.*)>", str(example_set)) output.write(str(example_set)) print(len(table)) print(table) # Get stats to normalize data raw_data = np.array(example_set.to_float()) data_mean = np.average(raw_data, axis=0) data_std = np.std(raw_data, axis=0) data_std[np.nonzero(data_std == 0.0)] = 1.0 def normalizer(ex): ex = np.array(ex) normed = ((ex - data_mean) / data_std) # The ...[:, 2:-1] removes first two columns and last column, # which are the bag/instance ids and class label, as part of the # normalization process return normed[2:-1] # Group examples into bags bagset = bag_set(example_set) # Convert bags to NumPy arrays bags = [np.array(b.to_float(normalizer)) for b in bagset] labels = np.array([b.label for b in bagset], dtype=float) # Convert 0/1 labels to -1/1 labels labels = 2 * labels - 1 # Spilt dataset arbitrarily to train/test sets train_bags = bags[10:] train_labels = labels[10:] test_bags = bags[:10] test_labels = labels[:10] # Construct classifiers classifiers = {} classifiers['MissSVM'] = misvm.MissSVM(kernel='linear', C=1.0, max_iters=20) classifiers['sbMIL'] = misvm.sbMIL(kernel='linear', eta=0.1, C=1e2) classifiers['SIL'] = misvm.SIL(kernel='linear', C=1.0) # Train/Evaluate classifiers accuracies = {} for algorithm, classifier in classifiers.items(): classifier.fit(train_bags, train_labels) predictions = classifier.predict(test_bags) accuracies[algorithm] = np.average(test_labels == np.sign(predictions)) for algorithm, accuracy in accuracies.items(): print('\n%s Accuracy: %.1f%%' % (algorithm, 100 * accuracy)) output.write('\n%s Accuracy: %.1f%%' % (algorithm, 100 * accuracy))
def run_mil_classifier(train_bags, train_labels, test_bags, test_labels): classifiers = {} classifiers['MissSVM'] = misvm.MissSVM(kernel='linear', C=1.0, max_iters=10) classifiers['sbMIL'] = misvm.sbMIL(kernel='linear', eta=0.1, C=1.0) #classifiers['SIL'] = misvm.SIL(kernel='linear', C=1.0) # Train/Evaluate classifiers accuracies = {} for algorithm, classifier in classifiers.items(): classifier.fit(train_bags, train_labels) predictions = classifier.predict(test_bags) accuracies[algorithm] = np.average(test_labels == np.sign(predictions)) for algorithm, accuracy in accuracies.items(): print '\n%s Accuracy: %.1f%%' % (algorithm, 100 * accuracy)
def main(): # Load list of C4.5 Examples example_set = parse_c45('musk1') # Group examples into bags bagset = bag_set(example_set) # Convert bags to NumPy arrays # (The ...[:, 2:-1] removes first two columns and last column, # which are the bag/instance ids and class label) bags = [np.array(b.to_float())[:, 2:-1] for b in bagset] labels = np.array([b.label for b in bagset], dtype=float) # Convert 0/1 labels to -1/1 labels labels = 2 * labels - 1 # Spilt dataset arbitrarily to train/test sets train_bags = bags[10:] train_labels = labels[10:] test_bags = bags[:10] test_labels = labels[:10] # Construct classifiers classifiers = {} classifiers['MissSVM'] = misvm.MissSVM(kernel='linear', C=1.0, max_iters=10) classifiers['sbMIL'] = misvm.sbMIL(kernel='linear', eta=0.1, C=1.0) classifiers['SIL'] = misvm.SIL(kernel='linear', C=1.0) # Train/Evaluate classifiers accuracies = {} for algorithm, classifier in classifiers.items(): classifier.fit(train_bags, train_labels) predictions = classifier.predict(test_bags) accuracies[algorithm] = np.average(test_labels == np.sign(predictions)) for algorithm, accuracy in accuracies.items(): print('\n%s Accuracy: %.1f%%' % (algorithm, 100 * accuracy))
def main(): # Khoi tao du lieu train_data, val_data, test_data = get_dataset() train_bags, train_labels = train_data val_bags, val_labels = val_data test_bags, test_labels = test_data train_instances, train_ilabels = convert_bags_to_instances( train_bags, train_labels) val_instances, val_ilabels = convert_bags_to_instances( val_bags, val_labels) # global K # K = get_number_of_k_nearest_neighbors(train_instances, train_bags) # print('Number of K nearest neighbors:', K) # training global classifier classifier = Classifier(train_bags, train_labels) ALPHA = get_alpha_hat(val_instances, val_ilabels) BETA = get_beta(train_ilabels) # testing acc = 0 for bag, blabel in zip(test_bags, test_labels): bpred = is_bag_pos(bag, ALPHA, BETA) acc += (blabel == bpred) print('Accuracy:', acc / len(test_bags)) train_labels = np.array(train_labels) * 2 - 1 test_labels = np.array(test_labels) * 2 - 1 svm = misvm.MissSVM(kernel='linear', C=1.0, max_iters=20) svm.fit(train_bags, train_labels) predictions = svm.predict(test_bags) acc2 = np.average(test_labels == np.sign(predictions)) print(acc2)