If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Certainly! Here's a simple example of how you can implement K-Fold Cross-Validation using Python and the KFold
class from the sklearn.model_selection
module:
pythonCopy code
from sklearn.datasets import load_iris from sklearn.model_selection import KFold from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score import numpy as np # Load the Iris dataset (you can replace this with your own dataset) iris = load_iris() X = iris.data y = iris.target # Create KFold cross-validation object with K=5 folds kfold = KFold(n_splits=5, shuffle=True, random_state=42) # Initialize lists to store accuracy scores from each fold accuracy_scores = [] # Iterate over each fold for fold_idx, (train_index, test_index) in enumerate(kfold.split(X)): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] # Train a model model = LogisticRegression() model.fit(X_train, y_train) # Make predictions on the test set y_pred = model.predict(X_test) # Calculate accuracy and store it in the list accuracy = accuracy_score(y_test, y_pred) accuracy_scores.append(accuracy) print(f"Fold {fold_idx+1}: Accuracy = {accuracy:.2f}") # Calculate and print the average accuracy across all folds avg_accuracy = np.mean(accuracy_scores) print(f"Average Accuracy: {avg_accuracy:.2f}")
In this example, we load the Iris dataset, create a KFold
object with K=5 folds, and then iterate over each fold using a loop. For each fold, we split the data into training and test sets, train a logistic regression model, calculate the accuracy on the test set, and print the accuracy for each fold. Finally, we calculate and print the average accuracy across all folds.
You can replace the dataset loading and preprocessing steps with your own data if you're working with a different dataset. This example demonstrates the basic concept of K-Fold Cross-Validation, and you can adapt it for other machine learning algorithms and evaluation metrics as needed.
Comments: 0