- By specifying the mentioned strategy using the multi_class argument of the LogisticRegression() constructor
- By using OneVsOneClassifier along with logistic regression
- By using the OneVsRestClassifier along with logistic regression
We have already discussed the second and third methods in our previous articles. Interested readers may follow the links above. In this article, we will discuss the first method.
We can use the following Python code to specify the multiclass strategy through the multi_class argument of the LogisticRegression() constructor.
import seaborn from sklearn.model_selection import KFold from sklearn.model_selection import cross_val_score from sklearn.linear_model import LogisticRegression dataset = seaborn.load_dataset("iris") D = dataset.values X = D[:, :-1] y = D[:, -1] kfold = KFold(n_splits=10, shuffle=True, random_state=1) model = LogisticRegression(multi_class="ovr") scores = cross_val_score(model, X, y, cv=kfold, scoring="accuracy") print("Accuracy: ", scores.mean()) Here, we are first reading the iris dataset and splitting the columns of the dataset into features and the target variable. dataset = seaborn.load_dataset("iris") D = dataset.values X = D[:, :-1] y = D[:, -1]
Here, X contains all the features, and y contains the target variable.
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
Now, we are initializing the k-fold cross-validation with 10 splits. We are shuffling the data before splitting. The random_state argument is used to initialize the pseudo-random number generator that is used for randomization.
model = LogisticRegression(multi_class="ovo")
Now, we are initializing the model using the LogisticRegression class. We are specifying the One-Vs-Rest strategy using the value “ovr” for the multi_class argument. We can use the value “ovo” for specifying the One-Vs-One (OVO) strategy.
model = LogisticRegression(multi_class="ovo") scores = cross_val_score(model, X, y, cv=kfold, scoring="accuracy") print("Accuracy: ", scores.mean())
Now, we are using the cross_val_score() function to estimate the performance of the model. We are using an accuracy score here (What is the accuracy score in machine learning?) Please note that we will get an accuracy score for each iteration of the k-fold cross-validation. We are printing the average accuracy score here.
The output of the given program will be like the following:
Accuracy: 0.96






0 Comments