You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* feat: add Logistic Regression classifier
Add Classifier::LogisticRegression with SGD training and L2 regularization.
Provides better calibrated probabilities than Naive Bayes and feature
weights for interpretability.
Closes#102
* style: fix rubocop offenses in logistic regression tests
* docs: add 'Why This Library?' section highlighting unique features
* docs: improve README with comparison table and modernized examples
* feat: add storage API to TF-IDF and complete LogisticRegression storage
- Add .storage, .save, .load, .reload, .reload!, .dirty? to TF-IDF
- Add .reload and .reload! to LogisticRegression
- Add StorageAPIConsistencyTest to enforce all classifiers implement storage API
- Add comprehensive storage tests for both TF-IDF and LogisticRegression
A Ruby library for text classification using Bayesian, LSI (Latent Semantic Indexing), k-Nearest Neighbors (kNN), and TF-IDF algorithms.
7
+
A Ruby library for text classification using Bayesian, Logistic Regression, LSI (Latent Semantic Indexing), k-Nearest Neighbors (kNN), and TF-IDF algorithms.
> **Note:** This is the original `classifier` gem, maintained for 20 years since 2005. After a quieter period, active development resumed in 2025 with major new features. If you're choosing between this and a fork, this is the canonical, most actively-developed version.
12
+
13
+
## Why This Library?
14
+
15
+
This gem has features no fork provides:
16
+
17
+
|| This Gem | Forks |
18
+
|:--|:--|:--|
19
+
|**Algorithms**| 5 classifiers | 2 only |
20
+
|**LSI Performance**| Native C (5-50x faster) | Pure Ruby, or requires GSL/Numo + system libs |
21
+
|**Persistence**| Pluggable (file, Redis, S3, SQL) | Marshal only |
The gem includes a native C extension for fast LSI operations. It compiles automatically during gem installation. No external dependencies are required.
77
+
The gem includes a zero-dependency native C extension for fast LSI operations (5-50x faster than pure Ruby). It compiles automatically during installation.
47
78
48
79
To verify the native extension is active:
49
80
@@ -58,22 +89,6 @@ To force pure Ruby mode (for debugging):
58
89
NATIVE_VECTOR=true ruby your_script.rb
59
90
```
60
91
61
-
To suppress the warning when native extension isn't available:
62
-
63
-
```bash
64
-
SUPPRESS_LSI_WARNING=true ruby your_script.rb
65
-
```
66
-
67
-
### Compatibility
68
-
69
-
| Ruby Version | Status |
70
-
|--------------|--------|
71
-
| 4.0 | Supported |
72
-
| 3.4 | Supported |
73
-
| 3.3 | Supported |
74
-
| 3.2 | Supported |
75
-
| 3.1 | EOL (unsupported) |
76
-
77
92
## Bayesian Classifier
78
93
79
94
Fast, accurate classification with modest memory requirements. Ideal for spam filtering, sentiment analysis, and content categorization.
classifier.train(ham:"Meeting scheduled for tomorrow at 10am")
91
106
92
107
# Train multiple items at once
@@ -106,6 +121,66 @@ classifier.classify "Congratulations! You've won a prize!"
106
121
-[Build a Spam Filter Tutorial](https://rubyclassifier.com/docs/tutorials/spam-filter) - Step-by-step guide
107
122
-[Paul Graham: A Plan for Spam](http://www.paulgraham.com/spam.html)
108
123
124
+
## Logistic Regression
125
+
126
+
Linear classifier using Stochastic Gradient Descent (SGD). Often more accurate than Naive Bayes while remaining fast and interpretable. Provides well-calibrated probability estimates and feature weights for understanding which words drive predictions.
127
+
128
+
### Key Features
129
+
130
+
-**More Accurate**: No independence assumption means better accuracy on many text problems
|**Probabilities**| Well-calibrated | Tend to be extreme |
178
+
|**Training**| Needs to fit model | Instant (just counting) |
179
+
|**Interpretability**| Feature weights | Word frequencies |
180
+
|**Small datasets**| Use higher regularization | Works well |
181
+
182
+
Use Logistic Regression when you need accurate probabilities or want to understand feature importance. Use Bayes when you need instant updates or have very small training sets.
183
+
109
184
## LSI (Latent Semantic Indexing)
110
185
111
186
Semantic analysis using Singular Value Decomposition (SVD). More flexible than Bayesian classifiers, providing search, clustering, and classification based on meaning rather than just keywords.
@@ -117,18 +192,18 @@ require 'classifier'
117
192
118
193
lsi =Classifier::LSI.new
119
194
120
-
# Add documents with hash-style syntax (category => item(s))
121
-
lsi.add("Pets" =>"Dogs are loyal pets that love to play fetch")
122
-
lsi.add("Pets" =>"Cats are independent and love to nap")
123
-
lsi.add("Programming" =>"Ruby is a dynamic programming language")
195
+
# Add documents (category: item(s))
196
+
lsi.add(pets:"Dogs are loyal pets that love to play fetch")
197
+
lsi.add(pets:"Cats are independent and love to nap")
198
+
lsi.add(programming:"Ruby is a dynamic programming language")
124
199
125
200
# Add multiple items with the same category
126
-
lsi.add("Programming" => ["Python is great for data science", "JavaScript runs in browsers"])
201
+
lsi.add(programming: ["Python is great for data science", "JavaScript runs in browsers"])
127
202
128
203
# Batch operations with multiple categories
129
204
lsi.add(
130
-
"Pets" => ["Hamsters are small furry pets", "Birds can be great companions"],
131
-
"Programming" =>"Go is fast and concurrent"
205
+
pets: ["Hamsters are small furry pets", "Birds can be great companions"],
206
+
programming:"Go is fast and concurrent"
132
207
)
133
208
134
209
# Classify new text
@@ -330,7 +405,7 @@ loaded = Marshal.load(data)
330
405
331
406
## Persistence
332
407
333
-
Save and load classifiers with pluggable storage backends. Works with Bayes, LSI, and kNN classifiers.
408
+
Save and load classifiers with pluggable storage backends. Works with Bayes, LogisticRegression, LSI, and kNN classifiers.
0 commit comments