Production-ready ML classification templates with hierarchical and neural network classifiers
Project description
NLP Templates
Production-ready ML classification templates with hierarchical and neural network classifiers.
Features
- SimpleMulticlassClassifier: Flat multi-class classification with multiple backend support (logistic regression, random forest, SVM, neural network)
- HierarchicalNNClassifier: Multi-level hierarchical classification with neural networks
- MLflow Integration: Built-in experiment tracking and model logging
- Configurable: YAML/JSON configuration support for hyperparameters
- Evaluation Tools: Metrics calculation, confusion matrices, and visualizations
Installation
Basic Installation
pip install nlp-templates
With PyTorch Support (for neural network classifiers)
pip install nlp-templates[torch]
With MLflow Support
pip install nlp-templates[mlflow]
Full Installation (all optional dependencies)
pip install nlp-templates[all]
Quick Start
Simple Multi-class Classification
import numpy as np
from nlp_templates import SimpleMulticlassClassifier
# Create sample data
X = np.random.randn(500, 20)
y = np.random.choice([0, 1, 2, 3], size=500)
# Initialize classifier
clf = SimpleMulticlassClassifier(
name="my_classifier",
random_state=42,
test_size=0.3,
)
# Configure for neural network
clf.config = {
"model": {
"type": "neural_network",
"params": {
"hidden_dims": [64, 32],
"epochs": 50,
"batch_size": 32,
}
}
}
# Train and evaluate
clf.load_data(X, y)
clf.build_model()
clf.train()
results = clf.evaluate()
print(f"Test Accuracy: {results['test_metrics']['accuracy']:.4f}")
Hierarchical Classification
import numpy as np
from nlp_templates import HierarchicalNNClassifier
# Create hierarchical data (2-level hierarchy)
X = np.random.randn(500, 20)
y_level0 = np.random.choice([0, 1, 2], size=500)
y_level1 = np.random.choice([0, 1, 2, 3], size=500)
y = np.column_stack([y_level0, y_level1])
# Initialize hierarchical classifier
clf = HierarchicalNNClassifier(
name="hierarchical_clf",
random_state=42,
test_size=0.3,
)
# Train and evaluate
clf.load_data(X, y)
clf.build_model()
clf.train()
results = clf.evaluate()
# Get predictions
predictions = clf.predict(X[:10])
print(f"Predictions shape: {predictions.shape}") # (10, 2) for 2 levels
Using Configuration Files
from nlp_templates import SimpleMulticlassClassifier
# Load from YAML config
clf = SimpleMulticlassClassifier(
name="configured_classifier",
config_path="config/model_config.yaml",
)
# Run full pipeline
results = clf.full_pipeline(
X, y,
save_visualizations=True,
output_dir="outputs",
)
Example config file (config/model_config.yaml):
model:
type: neural_network
params:
hidden_dims: [128, 64, 32]
activation: relu
dropout_rate: 0.3
learning_rate: 0.001
epochs: 100
batch_size: 64
Available Model Types
For SimpleMulticlassClassifier:
| Model Type | Description |
|---|---|
logistic_regression |
Sklearn LogisticRegression |
random_forest |
Sklearn RandomForestClassifier |
naive_bayes |
Sklearn MultinomialNB |
svm |
Sklearn SVC |
neural_network |
PyTorch-based neural network |
API Reference
SimpleMulticlassClassifier
SimpleMulticlassClassifier(
name="classifier_name",
config_path=None, # Path to YAML/JSON config
random_state=42,
test_size=0.3,
mlflow_tracking_uri=None, # MLflow tracking URI
mlflow_experiment_name=None,
)
Methods:
load_data(X, y): Load and split databuild_model(): Build model from configtrain(): Train the modelevaluate(): Evaluate on train/test setspredict(X): Make predictionspredict_proba(X): Get prediction probabilitiesfull_pipeline(X, y, ...): Run complete pipeline
HierarchicalNNClassifier
HierarchicalNNClassifier(
name="hierarchical_classifier",
config_path=None,
random_state=42,
test_size=0.3,
mlflow_tracking_uri=None,
mlflow_experiment_name=None,
)
Methods:
load_data(X, y): Load hierarchical data (y shape: n_samples x n_levels)build_model(): Build classifier for each hierarchy leveltrain(): Train all level classifiersevaluate(): Evaluate each level separatelypredict(X): Predict all hierarchy levelspredict_proba(X): Get probabilities for each levelfull_pipeline(X, y, ...): Run complete pipeline
Requirements
Core dependencies:
- Python >= 3.9
- scikit-learn >= 1.0.0
- pandas >= 1.3.0
- numpy >= 1.21.0
- pyyaml >= 6.0
- matplotlib >= 3.5.0
- seaborn >= 0.11.0
Optional dependencies:
- torch >= 2.0.0 (for neural network classifiers)
- mlflow >= 2.0.0 (for experiment tracking)
- optuna >= 3.0.0 (for hyperparameter tuning)
Development
# Clone the repository
git clone https://github.com/bhavinr9/nlp-templates.git
cd nlp-templates
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=nlp_templates --cov-report=html
License
MIT License - see LICENSE for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nlp_templates-0.1.0.tar.gz.
File metadata
- Download URL: nlp_templates-0.1.0.tar.gz
- Upload date:
- Size: 28.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f68015760d3372207d304ab2e12f90768a427d9e35d068dbbcc4d27a32a5912
|
|
| MD5 |
40a77f2966587ccc85ef8a3a8776ccb3
|
|
| BLAKE2b-256 |
9ea85ad7bdaa8304255b78f063f55acc4c211dd31587c4f2767f9d6869b3b47a
|
File details
Details for the file nlp_templates-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nlp_templates-0.1.0-py3-none-any.whl
- Upload date:
- Size: 33.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b43825fd961062f992818dbf2ca9e836ed35814f8b9e47fd34dddd8f299f5eb2
|
|
| MD5 |
20ad223f09a56de37303b552121976b9
|
|
| BLAKE2b-256 |
5b2ae70f7a90c7441800299fa184b63b3a754649044010c832bdfbba4c34c413
|