Update README.md
Browse files
README.md
CHANGED
|
@@ -64,6 +64,64 @@ The Intelligent Stacking system operates in multiple stages:
|
|
| 64 |
|
| 65 |
4. **Dynamic Thresholds**: Per-category optimized decision boundaries for multilabel output
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
## Categories
|
| 69 |
|
|
|
|
| 64 |
|
| 65 |
4. **Dynamic Thresholds**: Per-category optimized decision boundaries for multilabel output
|
| 66 |
|
| 67 |
+
## Usage
|
| 68 |
+
|
| 69 |
+
### Quick Start with Python
|
| 70 |
+
|
| 71 |
+
```python
|
| 72 |
+
import joblib
|
| 73 |
+
import numpy as np
|
| 74 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 75 |
+
from scipy.sparse import hstack, csr_matrix
|
| 76 |
+
|
| 77 |
+
# Load the model components
|
| 78 |
+
tfidf_vectorizer = joblib.load("int_stacking_tfidf_vectorizer.joblib")
|
| 79 |
+
meta_learner = joblib.load("int_stacking_meta_learner.joblib")
|
| 80 |
+
mlb_encoder = joblib.load("int_stacking_mlb_encoder.joblib")
|
| 81 |
+
base_models = joblib.load("int_stacking_base_models.joblib")
|
| 82 |
+
optimal_thresholds = np.load("int_stacking_optimal_thresholds.npy")
|
| 83 |
+
|
| 84 |
+
# Prepare text
|
| 85 |
+
text = """CONTRATO DE PRESTAÇÃO DE SERVIÇOS
|
| 86 |
+
Entre a Administração Pública Municipal e a empresa contratada,
|
| 87 |
+
fica estabelecido o presente contrato para prestação de serviços
|
| 88 |
+
de manutenção e conservação de vias públicas."""
|
| 89 |
+
|
| 90 |
+
# Extract features
|
| 91 |
+
tfidf_features = tfidf_vectorizer.transform([text])
|
| 92 |
+
|
| 93 |
+
# Generate base model predictions
|
| 94 |
+
base_predictions = np.zeros((1, len(mlb_encoder.classes_), 12))
|
| 95 |
+
model_idx = 0
|
| 96 |
+
|
| 97 |
+
for feat_name in ["TF-IDF", "BERT", "TF-IDF+BERT"]:
|
| 98 |
+
for algo_name in ["LogReg_C1", "LogReg_C05", "GradBoost", "RandomForest"]:
|
| 99 |
+
model_key = f"{feat_name}_{algo_name}"
|
| 100 |
+
if model_key in base_models:
|
| 101 |
+
model = base_models[model_key]
|
| 102 |
+
pred = model.predict_proba(tfidf_features)
|
| 103 |
+
base_predictions[0, :, model_idx] = pred[0]
|
| 104 |
+
model_idx += 1
|
| 105 |
+
|
| 106 |
+
# Meta-learner prediction
|
| 107 |
+
meta_features = base_predictions.reshape(1, -1)
|
| 108 |
+
meta_pred = meta_learner.predict_proba(meta_features)[0]
|
| 109 |
+
|
| 110 |
+
# Apply dynamic thresholds
|
| 111 |
+
predicted_labels = []
|
| 112 |
+
for i, (prob, threshold) in enumerate(zip(meta_pred, optimal_thresholds)):
|
| 113 |
+
if prob > threshold:
|
| 114 |
+
predicted_labels.append({
|
| 115 |
+
"label": mlb_encoder.classes_[i],
|
| 116 |
+
"probability": float(prob),
|
| 117 |
+
"confidence": "high" if prob > 0.7 else "medium" if prob > 0.4 else "low"
|
| 118 |
+
})
|
| 119 |
+
|
| 120 |
+
# Sort by probability
|
| 121 |
+
predicted_labels.sort(key=lambda x: x["probability"], reverse=True)
|
| 122 |
+
print("Predicted categories:", predicted_labels)
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
|
| 126 |
## Categories
|
| 127 |
|