def train_test_score(classifier, data, target, labels, clf2, ml_seed): normalizer = make_normalizer(data) split_point = round(ml_seed.split_ratio * len(data)) train_set = data[:split_point] train_key = target[:split_point] test_set = data[split_point:] test_key = target[split_point:] if ml_seed.normalize_my_data: my_train_set = normalizer(train_set) my_test_set = normalizer(test_set) else: my_train_set = train_set my_test_set = test_set classifier.train(my_train_set, train_key, ml_seed.relation) prediction = classifier.predict(my_test_set) if ml_seed.normalize_their_data: their_train_set = normalizer(train_set) their_test_set = normalizer(test_set) else: their_train_set = train_set their_test_set = test_set clf2.fit(their_train_set, train_key) prediction_real = clf2.predict(their_test_set) accuracy = calc_accuracy(test_key, prediction) accuracy_real = calc_accuracy(test_key, prediction_real) confusion_matrix = make_confusion_matrix(labels, test_key, prediction) confusion_matrix_real = make_confusion_matrix(labels, test_key, prediction_real) return accuracy, accuracy_real, confusion_matrix, confusion_matrix_real
def train_test_score(classifier, data, target, labels, clf2, ml_seed): normalizer = make_normalizer(data) split_point = round(ml_seed.split_ratio * len(data)) train_set = data[:split_point] train_key = target[:split_point] test_set = data[split_point:] test_key = target[split_point:] if ml_seed.normalize_my_data: my_train_set = normalizer(train_set) my_test_set = normalizer(test_set) else: my_train_set = train_set my_test_set = test_set classifier.train(my_train_set, train_key, ml_seed.relation) prediction = classifier.predict(my_test_set) if ml_seed.normalize_their_data: their_train_set = normalizer(train_set) their_test_set = normalizer(test_set) else: their_train_set = train_set their_test_set = test_set clf2.fit(their_train_set, train_key) prediction_real = clf2.predict(their_test_set) accuracy = calc_accuracy(test_key, prediction) accuracy_real = calc_accuracy(test_key, prediction_real) confusion_matrix = make_confusion_matrix(labels, test_key, prediction) confusion_matrix_real = make_confusion_matrix( labels, test_key, prediction_real) return accuracy, accuracy_real, confusion_matrix, confusion_matrix_real
def cross_val_score(classifier, data, target, labels, clf2, ml_seed): k = max(0, min(ml_seed.cross_folds, len(data))) step = len(data) // k results = np.zeros(k) results_real = np.zeros(k) confusion_matrices = [] confusion_matrices_real = [] normalizer = make_normalizer(data) for i, pos in zip(range(0, len(data), step), range(k)): train_set = np.vstack((data[:i], data[i + step:])) train_key = np.hstack((target[:i], target[i + step:])) test_set = data[i:i + step] test_key = target[i:i + step] if ml_seed.normalize_my_data: my_train_set = normalizer(train_set) my_test_set = normalizer(test_set) else: my_train_set = train_set my_test_set = test_set classifier.train(my_train_set, train_key, ml_seed.relation) prediction = classifier.predict(my_test_set) results[pos] = calc_accuracy(test_key, prediction) if ml_seed.normalize_their_data: their_train_set = normalizer(train_set) their_test_set = normalizer(test_set) else: their_train_set = train_set their_test_set = test_set clf2.fit(their_train_set, train_key) prediction_real = clf2.predict(their_test_set) results_real[pos] = calc_accuracy(test_key, prediction_real) confusion_matrices.append( make_confusion_matrix(labels, test_key, prediction)) confusion_matrices_real.append( make_confusion_matrix(labels, test_key, prediction_real)) confusion_matrix = flatten_confusion_matrices(confusion_matrices) confusion_matrix_real = flatten_confusion_matrices(confusion_matrices_real) if ml_seed.confusion_matrix_format != CMF_ACTUAL: confusion_matrix.values = convert_to_zero_one(confusion_matrix.values) confusion_matrix_real.values = convert_to_zero_one( confusion_matrix_real.values) return results, results_real, confusion_matrix, confusion_matrix_real
def cross_val_score(classifier, data, target, labels, clf2, ml_seed): k = max(0, min(ml_seed.cross_folds, len(data))) step = len(data) // k results = np.zeros(k) results_real = np.zeros(k) confusion_matrices = [] confusion_matrices_real = [] normalizer = make_normalizer(data) for i, pos in zip(range(0, len(data), step), range(k)): train_set = np.vstack((data[:i], data[i+step:])) train_key = np.hstack((target[:i], target[i+step:])) test_set = data[i:i+step] test_key = target[i:i+step] if ml_seed.normalize_my_data: my_train_set = normalizer(train_set) my_test_set = normalizer(test_set) else: my_train_set = train_set my_test_set = test_set classifier.train(my_train_set, train_key, ml_seed.relation) prediction = classifier.predict(my_test_set) results[pos] = calc_accuracy(test_key, prediction) if ml_seed.normalize_their_data: their_train_set = normalizer(train_set) their_test_set = normalizer(test_set) else: their_train_set = train_set their_test_set = test_set clf2.fit(their_train_set, train_key) prediction_real = clf2.predict(their_test_set) results_real[pos] = calc_accuracy(test_key, prediction_real) confusion_matrices.append( make_confusion_matrix(labels, test_key, prediction)) confusion_matrices_real.append( make_confusion_matrix(labels, test_key, prediction_real)) confusion_matrix = flatten_confusion_matrices(confusion_matrices) confusion_matrix_real = flatten_confusion_matrices(confusion_matrices_real) if ml_seed.confusion_matrix_format != CMF_ACTUAL: confusion_matrix.values = convert_to_zero_one(confusion_matrix.values) confusion_matrix_real.values = convert_to_zero_one( confusion_matrix_real.values) return results, results_real, confusion_matrix, confusion_matrix_real