Sub topic 3
When Neural Networks Are Worth Trying
Assume you know supervised learning, pandas, and scikit-learn’s fit and predict interface. This chapter focuses on choosing and comparing models rather than implementing neural networks from scratch.
When neural networks are worth trying
Neural networks can learn complex interactions and nonlinear patterns. They are worth testing when you have many rows, many useful features, or a problem where representation learning matters. They may also help when embeddings, text, images, or other learned features join your tabular columns.
For ordinary tabular data, gradient-boosted trees are often a stronger first choice. Neural networks usually need careful scaling, architecture choices, regularization, and more tuning. Small datasets often do not provide enough evidence for them to generalize well.
Tuning without fooling yourself
Hyperparameter tuning searches choices such as learning rate, tree depth, hidden-layer size, or regularization strength. If you tune and evaluate on the same validation results, those results become optimistic.
Use nested cross-validation when the dataset is limited. The inner folds select settings. The untouched outer fold estimates the complete tuning procedure.
Comparing models fairly
Compare a baseline, linear model, random forest, boosted trees, and neural network on identical folds. Use the same preprocessing rules and metric. Fit imputers, encoders, and scalers inside a pipeline so each training fold learns them without seeing its validation fold.
Report the mean and spread of scores. A tiny average improvement may not justify extra complexity if the fold-to-fold variation is large.
| Question | Evidence |
|---|---|
| Does the model improve? | Mean score and variation on identical folds |
Interpretability, latency, and maintenance
Accuracy is only one selection criterion. Also consider:
- Interpretability: Can you explain important predictions?
- Latency: How quickly must each prediction return?
- Maintenance: How difficult are retraining, monitoring, and debugging?
- Operations: Does the model need special hardware or dependencies?
A simpler model may be preferable when its score is close to the best score.
A repeatable selection workflow
- Define the prediction target and decision metric.
- Create a leakage-safe preprocessing pipeline.
- Establish a simple baseline.
- Compare several model families on fixed folds.
- Tune promising candidates with nested validation.
- Check practical constraints and error patterns.
- Retrain the selected pipeline once on all available training data.
Exercise
Why should preprocessing be inside the cross-validation pipeline?
Show answer
To prevent information from the validation fold influencing imputation, encoding, or scaling.