Cross Validation example with Simple Python
Let’s simulate this using a basic neural network and manual k-Fold CV.
import numpy as np # --- Generate dummy dataset --- np.random.seed(42) X = np.random.rand(100, 1) # 100 samples, 1 feature y = 3 * X.squeeze() + np.random.randn(100) * 0.1 # y = 3x + noise # --- Simple Neural Network Model --- def train_and_evaluate(X_train, y_train, X_val, y_val, epochs=100, lr=0.01): w = np.random.randn() b = np.random.randn() for epoch in range(epochs): y_pred = X_train * w + b error = y_pred - y_train loss = (error ** 2).mean() # Gradient descent grad_w = 2 * (error * X_train).mean() grad_b = 2 * error.mean() w -= lr * grad_w b -= lr * grad_b # Final evaluation val_pred = X_val * w + b val_loss = ((val_pred - y_val) ** 2).mean() return val_loss # --- K-Fold Cross Validation --- def cross_validate(X, y, k=5): fold_size = len(X) // k losses = [] for i in range(k): start, end = i * fold_size, (i + 1) * fold_size X_val, y_val = X[start:end], y[start:end] X_train = np.concatenate([X[:start], X[end:]]) y_train = np.concatenate([y[:start], y[end:]]) loss = train_and_evaluate(X_train, y_train, X_val, y_val) print(f"Fold {i+1} Validation Loss: {loss:.4f}") losses.append(loss) print(f"\nAverage Validation Loss: {np.mean(losses):.4f}") return losses # --- Run Cross Validation --- cross_validate(X, y)
How to Know Cross-Validation Is Needed?
Here are signals:
Situation | Why CV Helps |
---|---|
Small dataset | Prevents overfitting, uses all data effectively |
Highly variable results | Stabilizes performance estimates |
You’re tuning hyperparameters | Ensures tuning isn’t biased by one split |
Accuracy drops on unseen data | Indicates overfitting; CV gives better generalization |
Class imbalance | CV ensures fair class distribution over folds |
Cross Validation example with Simple Python – Summary