columns contain the features sepal length, sepal width, petal length, and petal width. The last column contains the target variable species.
dataset = seaborn.load_dataset("iris")
D = dataset.values
X = D[:, :-1]
y = D[:, -1]
X here contains 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. The argument n_splits refers to the number of splits. The argument shuffle=True indicates that the data is shuffled before splitting. And the argument random_state is used to initialize the pseudo-random number generator that is used for shuffling the data.
classifier = LogisticRegression(solver="liblinear") ovr = OneVsRestClassifier(classifier)
We are here using the logistic regression classifier with the OVR classifier.
scores = cross_val_score(ovr, X, y, scoring="accuracy", cv=kfold)
print("Accuracy: ", scores.mean())
Now, we are using the cross_val_score() function to estimate the performance of the model. We are using the 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. So, we are printing the average accuracy score of the model.
The output of the given program will be:
Accuracy: 0.9466666666666667








































0 Comments