Environment:Fastai Fastbook Sklearn Environment
| Knowledge Sources | |
|---|---|
| Domains | Tabular, Machine_Learning |
| Last Updated | 2026-02-09 17:00 GMT |
Overview
Scikit-learn environment required for Random Forest models, decision tree visualization, and feature importance analysis in Ch9 Tabular Modeling.
Description
The Tabular Modeling chapter (Ch9) uses scikit-learn's `RandomForestRegressor` and `DecisionTreeRegressor` alongside fastai's tabular deep learning tools. The `utils.py` file also imports `sklearn.tree.export_graphviz` for decision tree visualization. Scikit-learn is listed in `requirements.txt` as `scikit_learn` (no version constraint).
Usage
Use this environment for the Tabular Modeling workflow, specifically for:
- Random Forest training: `RandomForestRegressor` with custom hyperparameters
- Decision tree visualization: `export_graphviz` rendered via graphviz
- Feature importance analysis: Permutation importance and partial dependence
- Model ensembling: Comparing RF predictions with neural network predictions
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Any (Linux, macOS, Windows) | No platform restrictions |
| Hardware | CPU only | Random forests do not use GPU |
| RAM | 4GB+ recommended | For large tabular datasets with many trees |
Dependencies
Python Packages
- `scikit_learn` (any recent version)
- `pandas` (for data manipulation)
- `graphviz` (for decision tree visualization)
- `scipy` (for `scipy.cluster.hierarchy` and `scipy.stats.spearmanr`)
Credentials
No credentials required.
Quick Install
pip install scikit-learn graphviz scipy
Code Evidence
Random Forest wrapper from `09_tabular.md:617-621`:
def rf(xs, y, n_estimators=40, max_samples=200_000,
max_features=0.5, min_samples_leaf=5, **kwargs):
return RandomForestRegressor(n_jobs=-1, n_estimators=n_estimators,
max_samples=max_samples, max_features=max_features,
min_samples_leaf=min_samples_leaf, oob_score=True).fit(xs, y)
Decision tree visualization from `utils.py:81-86`:
from sklearn.tree import export_graphviz
def draw_tree(t, df, size=10, ratio=0.6, precision=0, **kwargs):
s=export_graphviz(t, out_file=None, feature_names=df.columns, filled=True, rounded=True,
special_characters=True, rotate=False, precision=precision, **kwargs)
return graphviz.Source(re.sub('Tree {', f'Tree {{ size={size}; ratio={ratio}', s))
Scipy clustering from `utils.py:90-98`:
from scipy.cluster import hierarchy as hc
def cluster_columns(df, figsize=(10,6), font_size=12):
corr = np.round(scipy.stats.spearmanr(df).correlation, 4)
corr_condensed = hc.distance.squareform(1-corr)
z = hc.linkage(corr_condensed, method='average')
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `ModuleNotFoundError: No module named 'sklearn'` | scikit-learn not installed | `pip install scikit-learn` |
| `ExecutableNotFoundError: graphviz not found` | System graphviz not installed | Install via package manager: `apt install graphviz` or `brew install graphviz` |
| `MemoryError` during RF training | Dataset too large for available RAM | Reduce `max_samples` parameter or use fewer trees |
Compatibility Notes
- n_jobs=-1: The Random Forest code uses `n_jobs=-1` to parallelize across all CPU cores. This works on all platforms.
- Graphviz: Requires both the Python package (`pip install graphviz`) and the system binary (`apt install graphviz` on Linux).
- No GPU: Scikit-learn models run exclusively on CPU. GPU is not used for random forests.