Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logistic Regression: Binary class labels getter #325

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions algorithms/linfa-logistic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,12 @@ impl<F: Float, C: PartialOrd + Clone> FittedLogisticRegression<F, C> {
&self.params
}

/// Get the model positive and negative classes mapped to their
/// corresponding problem input labels.
pub fn labels(&self) -> &BinaryClassLabels<F, C> {
&self.labels
}

/// Given a feature matrix, predict the probabilities that a sample
/// should be classified as the larger of the two classes learned when the
/// model was fitted.
Expand Down Expand Up @@ -745,9 +751,9 @@ impl<C: PartialOrd + Clone + Default, F: Float, D: Data<Elem = F>>
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
struct ClassLabel<F, C: PartialOrd> {
class: C,
label: F,
pub struct ClassLabel<F, C: PartialOrd> {
pub class: C,
pub label: F,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -756,9 +762,9 @@ struct ClassLabel<F, C: PartialOrd> {
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
struct BinaryClassLabels<F, C: PartialOrd> {
pos: ClassLabel<F, C>,
neg: ClassLabel<F, C>,
pub struct BinaryClassLabels<F, C: PartialOrd> {
pub pos: ClassLabel<F, C>,
pub neg: ClassLabel<F, C>,
}

/// Internal representation of a logistic regression problem.
Expand Down Expand Up @@ -1008,6 +1014,8 @@ mod test {
&res.predict(dataset.records()),
dataset.targets().as_single_targets()
);
assert_eq!(res.labels().pos.class, "dog");
assert_eq!(res.labels().neg.class, "cat");
}

#[test]
Expand Down
Loading