In this segment, you will implement the One vs One classifier on the loan data set. Let’s hear more about it from Ajay.
As explained in the video, the code for creating the model and initialising it to the One vs One classifier is similar to that of One vs Rest. So, an LR model is initialised/defined and given to the One vs One classifier as a function parameter as shown below:LR1 = LogisticRegression() OneVsOne = OneVsOneClassifier(LR1)
The code for training the model with the train data set stored in the X_train_scaled and y_train was executed. X_train_scaled are the scaled/normalised values of the training dataset.# Fitting the model with training data OneVsOne.fit(X_train_scaled, y_train)
Finally, step 3 was executed, which is the model prediction done on the test sample stored in the variable X_test_scaled. The predicted results are stored in the variable prediction_oneVsone as shown below:# Making a prediction on the test set prediction_oneVsone = OneVsOne.predict(X_test_scaled)
Later, the classification results were analysed using the metrics discussed during the One vs Rest classifier method. The metrics discussed are given below:
- Accuracy
- Precision
- Recall
- F1-score
You can check out the code below that was executed to display the classification results. We print the accuracy values using the accuracy_score function and the remaining parameters using the classification_report function.# Evaluating the model print(f”Test Set Accuracy: {accuracy_score(y_test, prediction_oneVsone) * 100} %\n\n“) print(f”Classification Report: \n\n{classification_report(y_test, prediction_oneVsone)}”)
The image given below shows the results under each parameter (accuracy, precision, recall and F1-score) for each class category separately.
.png)
You can see that the accuracy is about 82%, but as discussed in earlier sessions, we cannot depend on accuracy alone for model effectiveness. In the classification report table as shown in the image above, each row represents a class category and its respective parameter values. The second table was not discussed, as it is beyond the scope of this module. Later, we analysed the prediction values by executing the code given below:X_test[‘prediction_oneVsone’] = prediction_oneVsone X_test.head()
Under the column ‘prediction_oneVsone’, we will have the predicted class category for each test sample, and the values in the column ‘Actual’ will remain the same as we have used the same train and test samples for both One vs Rest and One vs One classifier, i.e., we have not split the data set again.
Note: In the results displayed in the image given below, the columns prob_oneVsrest, prob_oneVsrest_highRisk, prob_oneVsrest_lowRisk, prob_oneVsrest_mediumRisk and Sacled_features do not belong to the One vs One classifier, i.eThey were calculated for the One vs Rest classifier.
Unlike One vs Rest, One vs One does not have support functions to check the results of individual models and calculate the final classification count for each test sample. But we can check the number of LR models built by executing the code given below:OneVsOne.estimators_
In the image given below, you can see that three LR models with their own parameters have been created. We get a total of three models, as the formula is given as:
C(n,2) => C(3,2) = 3
.png)
Similar to One vs Rest method, there will be seven weight coefficients (β1, …. , β7), each corresponding to one column of the train data set. There will be a total of three intercepts (β0), one for each LR model.
In the next segment, we will summarise all the topics discussed in this session.
Report an error