Skip to main content
Version: Next 🚧

Logistic Regression

Classes for training and running logistic regression classifiers.

LRTrainer​

Train a logistic regression model with SGD optimization.

Usage​

from underthesea_core import LRTrainer

X_train = [
["word=tα»‘t", "len=3"],
["word=xαΊ₯u", "len=3"],
["word=Δ‘αΊΉp", "len=3"],
]
y_train = ["positive", "negative", "positive"]

trainer = LRTrainer(
l2_penalty=0.01,
learning_rate=0.1,
max_epochs=100,
verbose=1,
)
model = trainer.train(X_train, y_train)
model.save("lr_model.bin")

Constructor​

LRTrainer(
l1_penalty=0.0,
l2_penalty=0.01,
learning_rate=0.1,
max_epochs=100,
batch_size=1,
tol=1e-4,
verbose=1,
)

Parameters​

ParameterTypeDefaultDescription
l1_penaltyfloat0.0L1 regularization (lasso)
l2_penaltyfloat0.01L2 regularization (ridge)
learning_ratefloat0.1Learning rate for SGD
max_epochsint100Maximum training epochs
batch_sizeint1Mini-batch size (1 = pure SGD)
tolfloat1e-4Convergence tolerance for early stopping
verboseint1Verbosity: 0=quiet, 1=progress, 2=detailed

Methods​

MethodReturnsDescription
train(X, y)LRModelTrain on feature lists X and labels y
set_l1_penalty(penalty)NoneSet L1 regularization
set_l2_penalty(penalty)NoneSet L2 regularization
set_learning_rate(lr)NoneSet learning rate
set_max_epochs(epochs)NoneSet maximum epochs
set_batch_size(size)NoneSet batch size
get_model()LRModelGet the current model

LRModel​

Stores trained logistic regression model weights and class labels.

Usage​

from underthesea_core import LRModel

# Load a saved model
model = LRModel.load("lr_model.bin")
print(model.num_classes) # number of classes
print(model.num_features) # number of features
print(model.get_classes()) # list of class labels

# Create with predefined classes
model = LRModel.with_classes(["positive", "negative"])

Constructor​

LRModel()

Static Methods​

MethodReturnsDescription
load(path)LRModelLoad model from file
with_classes(classes)LRModelCreate model with predefined classes

Properties​

PropertyTypeDescription
num_classesintNumber of classes
num_featuresintNumber of features

Methods​

MethodReturnsDescription
save(path)NoneSave model to file
get_classes()list[str]Get all class labels
num_weights()intGet number of non-zero weights
l2_norm_squared()floatGet L2 norm squared of all weights
l1_norm()floatGet L1 norm of all weights

LRClassifier​

Load a trained LR model and make predictions.

Usage​

from underthesea_core import LRClassifier

# Load from file
classifier = LRClassifier.load("lr_model.bin")

# Or create from model
from underthesea_core import LRModel
model = LRModel.load("lr_model.bin")
classifier = LRClassifier.from_model(model)

# Predict
features = ["word=tα»‘t", "len=3"]
label = classifier.predict(features)

# Predict with probability
label, prob = classifier.predict_with_prob(features)

# Get probability distribution
proba = classifier.predict_proba(features)
# [("positive", 0.85), ("negative", 0.15)]

# Get top-k predictions
top2 = classifier.predict_top_k(features, k=2)

Constructor​

LRClassifier()

Static Methods​

MethodReturnsDescription
load(path)LRClassifierLoad classifier from file
from_model(model)LRClassifierCreate classifier from an LRModel

Methods​

MethodReturnsDescription
predict(features)strPredict the most likely class
predict_with_prob(features)(str, float)Predict with probability
predict_proba(features)list[(str, float)]Get probability distribution over all classes
predict_top_k(features, k)list[(str, float)]Get top-k most likely classes with probabilities
num_classes()intGet number of classes
classes()list[str]Get all class labels