contains the target variable. So, X here contains all the features. And y contains the target variable.
sk_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)
Now, we are using the StratifiedKFold class to initialize the stratified k-fold cross-validation. n_splits here indicates that the number of splits is 10. shuffle=True indicates that we are shuffling the data before splitting. And the random_state argument initializes the pseudo-random number generator that is used for shuffling.
classifier = LogisticRegression(solver="liblinear")
Now, we are initializing the classifier using the LogisticRegression class. Please note that LogisticRegression() by default uses libfgs or Limited-memory Broyden–Fletcher–Goldfarb–Shanno. This solver may be good for smaller datasets. On larger datasets, libfgs may fail to converge. So, we are here using liblinear solver.
results = cross_val_score(classifier, X, y, cv=sk_fold, scoring="accuracy") mean_score = results.mean() print("Accuracy: ", mean_score)
Now, we are using the cross_val_score() function to evaluate the performance of the machine learning model. We are here using the accuracy score to evaluate the performance of the machine learning model (What is the accuracy score in machine learning?). As as know, we will get one accuracy score for each iteration of the k-fold cross-validation. So, we are taking the average of the accuracy scores and printing the average accuracy score.
The program will print the following output:
Accuracy: 0.766848940533151






0 Comments