Description
We are ready to train our models. In this stage, you need to find the best algorithm that can identify handwritten digits. Refer to the following algorithms: K-nearest Neighbors, Decision Tree, Logistic Regression, and Random Forest. In this stage, you need to train these four classifiers with default parameters. In the next stages, we will try to improve their performances. We will use an accuracy metric to evaluate the models.
Objectives
- Import
sklearnimplementations of the classifiers from the description and the accuracy scorer; - Since you are going to train a lot of models, implementing the following function will make the process fast and convenient:
# the function def fit_predict_eval(model, features_train, features_test, target_train, target_test): # here you fit the model # make a prediction # calculate accuracy and save it to score print(f'Model: {model}\nAccuracy: {score}\n') # example # code fit_predict_eval( model=KNeighborsClassifier(), features_train=x_train, features_test=x_test, target_train=y_train, target_test=y_test ) # output # >>> Model: KNeighborsClassifier() # >>> Accuracy: 0.1234 - Initialize the models with default parameters. Some of the algorithms have randomness, so you need to set
random_state=40to receive reproducible results; - Fit the models;
- Make predictions and print the accuracies. Make sure that the program prints the models and their results in the following order: K-nearest Neighbors, Decision Tree, Logistic Regression, and Random Forest;
- Which model performs better? Print the answer, including the model's name without parameters, and its accuracy. Round the result to the third decimal place.
The models' input includes the train and test sets, which you got in the previous stage. The output consists of the trained models.
Make sure your output format matches the example below.
Example
Example 1: an example of the output
Model: FriendsClassifier()
Accuracy: 0.4321
Model: DecisionFlowerClassifier(random_state=40)
Accuracy: 0.6543
Model: HeuristicRegression()
Accuracy: 0.9876
Model: RandomFlowerbedClassifier(random_state=40)
Accuracy: 0.7654
The answer to the question: HeuristicRegression - 0.988