> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cirron.com/llms.txt
> Use this file to discover all available pages before exploring further.

# scikit-learn

> scikit-learn project template.

# scikit-learn Template

The scikit-learn template scaffolds a production-ready ML project with scikit-learn, pandas, and traditional ML algorithms. Two variants are available:

* **`sklearn`**: basic model template
* **`sklearn-pipeline`**: full preprocessing + model pipeline

## Quick Start

```bash theme={null}
cirron init my-sklearn-model --template sklearn
```

The model type (classification, regression, etc.) is selected interactively during `cirron init`.

## Project Structure

```
my-sklearn-model/
├── src/
│   ├── model.py           # create_model() + preprocess_data()
│   ├── data_loader.py     # CSV loading + train/test split
│   ├── train.py           # Training script
│   └── inference.py       # Inference / prediction
├── data/
├── models/                # joblib-serialized model + preprocessor
├── requirements.txt
├── Dockerfile
└── cirron.yaml
```

For shared files (`Dockerfile`, `.gitignore`, `README.md`, `.env.example`, tests), see [Common files](/cli/templates/common).

## Cirron-specific bits

### cirron.yaml

```yaml theme={null}
framework: sklearn
model_type: classification
train_entrypoint: src/train.py
inference_entrypoint: src/inference.py
```

### Profiling hooks

The generated `train.py` and `inference.py` integrate with the Cirron SDK. Call `ci.profile()` once at module top, then wrap the estimator with `ci.wrap()` to record `fit`, `predict`, and pipeline-step spans:

```python theme={null}
import cirron as ci
from sklearn.ensemble import RandomForestClassifier

ci.profile()

self.model = ci.wrap(RandomForestClassifier(n_estimators=100))
self.model.fit(self.X_train_processed, self.y_train)
```

### requirements.txt

```txt theme={null}
scikit-learn>=1.3.0
numpy>=1.21.0
pandas>=1.5.0
matplotlib>=3.5.0
joblib>=1.2.0
```

## Model Types

* **Classification**: defaults to `RandomForestClassifier(n_estimators=100, max_depth=10)`. Metrics: accuracy, precision, recall, F1.
* **Regression**: defaults to `RandomForestRegressor(n_estimators=100, max_depth=10)`. Metrics: MSE, R².

Swap the default model in `src/model.py`:

```python theme={null}
def create_model():
    return SVC(probability=True, random_state=42)
```

## Model Persistence

The generated `train.py` saves both the model and the fitted preprocessor together via `joblib`:

```python theme={null}
joblib.dump({
    'model': self.model,
    'preprocessor': self.preprocessor,
    'config': self.config,
}, 'models/model.joblib')
```

`inference.py` reloads both so prediction-time preprocessing matches training.

## Usage

```bash theme={null}
python src/train.py     # Train
cirron build            # Container
cirron deploy           # Deploy
```

## Hyperparameter Tuning

Wrap `create_model()` with `GridSearchCV` for sweep-based tuning:

```python theme={null}
from sklearn.model_selection import GridSearchCV

def create_model():
    base = RandomForestClassifier(random_state=42)
    return GridSearchCV(base, {
        'n_estimators': [50, 100, 200],
        'max_depth': [5, 10, None],
    }, cv=5)
```

## Next Steps

<CardGroup cols={2}>
  <Card title="PyTorch Template" href="/cli/templates/pytorch">
    Deep learning framework
  </Card>

  <Card title="TensorFlow Template" href="/cli/templates/tensorflow">
    Alternative deep learning framework
  </Card>

  <Card title="Common files" href="/cli/templates/common">
    Dockerfile, tests, and shared scaffolding
  </Card>

  <Card title="Deployment" href="/cli/commands/deploy">
    Deploy your trained model
  </Card>
</CardGroup>
