Rename classifiers/ package to models/
Aligns module naming with the upcoming classification_neural lab, which will use the same models/ package convention. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
43
models/features.py
Normal file
43
models/features.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import numpy as np
|
||||
from sklearn.feature_extraction import DictVectorizer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.pipeline import Pipeline
|
||||
|
||||
|
||||
class FeatureExtractor:
|
||||
def fit(self, X, y=None):
|
||||
return self
|
||||
|
||||
def transform(self, X):
|
||||
return [self.extract_features(msg) for msg in X]
|
||||
|
||||
def extract_features(self, message):
|
||||
return {
|
||||
"contains_free": int("free" in message.lower()),
|
||||
"num_exclamations": message.count("!"),
|
||||
"length": len(message),
|
||||
}
|
||||
|
||||
|
||||
class FeatureClassifier:
|
||||
def fit(self, X, y):
|
||||
self._pipeline = Pipeline([
|
||||
("features", FeatureExtractor()),
|
||||
("vectorizer", DictVectorizer()),
|
||||
("classifier", LogisticRegression(max_iter=1000)),
|
||||
])
|
||||
y_binary = (np.array(y) == "spam").astype(int)
|
||||
self._pipeline.fit(X, y_binary)
|
||||
return self
|
||||
|
||||
def predict(self, X):
|
||||
y_binary = self._pipeline.predict(X)
|
||||
return np.where(y_binary == 1, "spam", "ham")
|
||||
|
||||
def feature_weights(self, top_n=10):
|
||||
vectorizer = self._pipeline.named_steps["vectorizer"]
|
||||
classifier = self._pipeline.named_steps["classifier"]
|
||||
names = vectorizer.get_feature_names_out()
|
||||
weights = classifier.coef_[0]
|
||||
pairs = sorted(zip(names, weights), key=lambda x: abs(x[1]), reverse=True)
|
||||
return pairs[:top_n]
|
||||
Reference in New Issue
Block a user