def test_hardcoded(num_tests=200, verbose=False): print("----------------------------") print(" TESTING HARDCODED ") print("----------------------------") classifier = HardCodedClassifier() tester = ClassifierTesterIris(classifier) tester.test(num_tests, verbose) print('\n\n')
shell = Shell(iris.data, iris.target, classifier) shell.fit_model_to_shell() shell.predict_from_classifier() num_correct = 0 for predicted, target in zip(shell.predictions, shell.targets_test): if predicted == target: num_correct += 1 accuracy = num_correct / len(shell.targets_test) print(f"Accuracy of GaussianNB Model: {round(accuracy * 100)}%") """ Test HardCodedClassifier """ hardcoded_classifier = HardCodedClassifier() new_shell = Shell(iris.data, iris.target, hardcoded_classifier) new_shell.fit_model_to_shell() new_shell.predict_from_classifier() """ Calculate accuracy """ num_correct = 0 for predicted, target in zip(new_shell.predictions, new_shell.targets_test): if predicted == target: num_correct += 1 accuracy = num_correct / len(new_shell.targets_test) print(f"Accuracy of Hard-Coded Model: {round(accuracy * 100)}%")
random_state=45) classifier = GaussianNB() classifier.fit(X_train, Y_train) print("Predicted -------- Real") countCorrect = 0 for xt, yt in zip(classifier.predict(X_test), Y_test): print(xt, "---------", yt) if xt == yt: countCorrect += 1 print("Naive Bayes Gaussian Accuracy: ", 100 * round(countCorrect / len(Y_test), 2), "%") mClassifier = HardCodedClassifier() model = mClassifier.fit(X_train, Y_train) predicted = model.predict(X_test) countCorrect = 0 print("Predicted ------ Real") for xt, yt in zip(predicted, Y_test): if xt == 0: xt = "setosa" print(xt, "---------", yt) if xt == yt: countCorrect += 1
def get_multiple_classifiers(is_regressor_data): # The classifiers dictionary will have a key with its name as a string # and the value will be the classifier class classifiers = dict() # Gets the classifiers the user wants to compare print("Which algorithms would you like to test?") print( "(Enter an algorithm one at a time, pressing enter after each addition)" ) response = "" # If the data set is regressor data, only displays algorithms that can handle it. if is_regressor_data: while response != "done" and response != "Done": print("1 - Hard Coded Nearest Neighbor Classifier") print("2 - scikit-learn Nearest Neighbor Regressor") print("3 - Hard Coded Classifier") print("Type \"done\" when completed.") response = input("> ") if response == '1': k = get_k('K') classifiers[ "Hard Coded Nearest Neighbor Classifier with a K of " + str(k)] = kNNClassifier(k) elif response == '2': k = get_k('K') classifiers["sci-learn Nearest Neighbor Regressor with a K of " + str(k)] = KNeighborsRegressor(n_neighbors=k) elif response == '3': classifiers["Hard Coded Classifier"] = HardCodedClassifier() elif response != "Done" and response != "done": print("Not a valid response.") else: while response != "done" and response != "Done": print("1 - scikit-learn Gaussian") print("2 - Hard Coded Nearest Neighbor Classifier") print("3 - scikit-learn Nearest Neighbor Classifier") print("4 - Decision Tree Classifier") print("5 - Hard Coded Nerual Network Classifier") print("6 - scikit-learn Neural Network Classifier") print("7 - scikit-learn Bagging Classifier") print("8 - scikit-learn Random Forest Classifier") print("9 - scikit-learn Ada Boost Classifier") print("10 - Hard Coded Classifier") print("Type \"done\" when completed.") response = input("> ") if response == '1': classifiers["scikit-learn Gaussian"] = GaussianNB() elif response == '2': k = get_k('K') classifiers[ "Hard Coded Nearest Neighbor Classifier with a K of " + str(k)] = kNNClassifier(k) elif response == '3': k = get_k('K') classifiers["sci-learn Nearest Neighbor Classifier with a K of " + str(k)] = \ KNeighborsClassifier(n_neighbors=k) elif response == '4': classifiers[ "Decision Tree Classifier"] = DecisionTreeClassifier() elif response == '5': classifiers[ "Hard Coded Neural Network Classifier"] = NeuralNetworkCalssifier( ) elif response == '6': classifiers[ "scikit-learn Neural Network Classifier"] = MLPClassifier( ) elif response == '7': sample = set_sample() features = set_features() k = get_k('k') classifiers["scikit-learn Bagging Classifier sample=" + str(sample) + " feature=" + str(features) + " k=" + str(k)] = BaggingClassifier( KNeighborsClassifier(n_neighbors=k), max_samples=sample, max_features=features) elif response == '8': k = get_k('n') classifiers["scikit-learn Random Forest Classifier n=" + str(k)] = RandomForestClassifier(n_estimators=k) elif response == '9': k = get_k('n') classifiers["scikit-learn Ada Boost Classifier n=" + str(k)] = AdaBoostClassifier(n_estimators=k) elif response == '10': classifiers["Hard Coded Classifier"] = HardCodedClassifier() elif response != "Done" and response != "done": print("Not a valid response.") # Returns a map of classifiers with their name as they key and the classifier as the value return classifiers
def get_classifier(is_regressor_data): # Prompts the user for their desired algorithm print("Which algorithm would you like to use?") # Only returns classifier data that can handle regressors if were using regressor data if is_regressor_data: while True: print("1 - Hard Coded Nearest Neighbor Classifier") print("2 - scikit-learn Nearest Neighbor Regressor") print("3 - Hard Coded Classifier") algorithm_response = input("> ") if algorithm_response == '1': k = get_k('k') return kNNClassifier( k ), "Hard Coded Nearest Neighbor Classifier with a K of " + str( k) elif algorithm_response == '2': k = get_k('k') return KNeighborsRegressor( n_neighbors=k ), "sci-learn Nearest Neighbor Regressor with a K of " + str(k) elif algorithm_response == '3': return HardCodedClassifier(), "Hard Coded Classifier" else: print("Not a valid response.") else: while True: print("1 - scikit-learn Gaussian") print("2 - Hard Coded Nearest Neighbor Classifier") print("3 - scikit-learn Nearest Neighbor Classifier") print("4 - Decision Tree Classifier") print("5 - Hard Coded Neural Network Classifier") print("6 - scikit-learn Neural Network Classifier") print("7 - scikit-learn Bagging Classifier") print("8 - scikit-learn Random Forest Classifier") print("9 - scikit-learn Ada Boost Classifier") print("10 - Hard Coded Classifier") algorithm_response = input("> ") if algorithm_response == '1': return GaussianNB(), "Gaussian" elif algorithm_response == '2': k = get_k('K') return kNNClassifier( k ), "Hard Coded Nearest Neighbor Classifier with a K of " + str( k) elif algorithm_response == '3': k = get_k('K') return KNeighborsClassifier( n_neighbors=k ), "sci-learn Nearest Neighbor Classifier with a K of " + str( k) elif algorithm_response == '4': return DecisionTreeClassifier(), "Decision Tree Classifier" elif algorithm_response == '5': return NeuralNetworkCalssifier( ), "Hard Coded Neural Network Classifier" elif algorithm_response == '6': return MLPClassifier( ), "scikit-learn Neural Network Classifier" elif algorithm_response == '7': sample = set_sample() feature = set_features() k = get_k('k') return BaggingClassifier(KNeighborsClassifier(n_neighbors=k), max_samples=sample, max_features=feature),\ "scikit-learn Bagging Classifier sample=" + str(sample) + " feature=" + str(feature) + " k=" + str(k) elif algorithm_response == '8': k = get_k('n') return RandomForestClassifier( n_estimators=k ), "scikit-learn Random Forest Classifier n=" + str(k) elif algorithm_response == '9': k = get_k('n') return AdaBoostClassifier( n_estimators=k ), "scikit-learn Ada Boost Classifier n=" + str(k) elif algorithm_response == '10': return HardCodedClassifier(), "Hard Coded Classifier" else: print("Not a valid response.")
target_predicted = model.predict(data_test) missed = 0 j = 0 for i in target_predicted: if i != target_test[j]: missed += 1 j += 1 accuracy = int(((target_test.size - missed) / target_test.size) * 100) print("Gaussian Accuracy = ", accuracy, "%\n") classifier2 = HardCodedClassifier() model2 = classifier2.fit(data_train, target_train) target_predicted2 = model2.predict(data_test) missed = 0 for i in range(len(target_predicted2)): if target_predicted2[i] != target_test[i]: missed += 1 accuracy2 = int(((target_test.size - missed) / target_test.size) * 100) print("My Accuracy = ", accuracy2, "%\n")
gausswrong = 0 for x in xrange(len(gausstargets_predicted)): if gausstargets_predicted[x] != test_target[x]: gausswrong = gausswrong + 1 #print(gausstargets_predicted) #print(test_target) #print("gausswrong") #print(gausswrong) print("gauss accuracy") print((len(test_target) - gausswrong) / float(len(test_target))) classifier = HardCodedClassifier() model = classifier.fit(train_data, train_target) targets_predicted = model.predict(test_data) wrong = 0 for x in xrange(len(targets_predicted)): if targets_predicted[x] != test_target[x]: wrong = wrong + 1 #print(targets_predicted) #print(test_target) #print("wrong") #print(wrong) print("accuracy") print((len(test_target) - wrong) / float(len(test_target)))