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

adding new parameter to nlp learner #235

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion src/fklearn/training/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ def nlp_logistic_classification_learner(df: pd.DataFrame,
target: str,
vectorizer_params: LogType = None,
logistic_params: LogType = None,
prediction_column: str = "prediction") -> LearnerReturnType:
prediction_column: str = "prediction",
keep_tfidf_object: bool = False) -> LearnerReturnType:
"""
Fits a text vectorizer (TfidfVectorizer) followed by
a logistic regression (LogisticRegression).
Expand Down Expand Up @@ -466,6 +467,9 @@ def nlp_logistic_classification_learner(df: pd.DataFrame,

prediction_column : str
The name of the column with the predictions from the model.

keep_tfidf_object : bool (default: False)
If True, return the tfidf object as a separate object in the logs.
"""

# set default params
Expand Down Expand Up @@ -511,6 +515,9 @@ def p(new_df: pd.DataFrame) -> pd.DataFrame:
'training_samples': len(df)},
'object': clf}

if keep_tfidf_object:
log["tf_idf_object"] = vect

return p, p(df), log


Expand Down
22 changes: 22 additions & 0 deletions tests/training/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,28 @@ def test_nlp_logistic_classification_learner():
assert Counter(expected_col_test) == Counter(pred_test.columns.tolist())
assert (pred_test.columns == pred_train.columns).all()

# test keep_tfidf_object case
learner_keep_tfidf = nlp_logistic_classification_learner(text_feature_cols=["text1", "text2"],
target="y",
vectorizer_params={"min_df": 1},
logistic_params=None,
prediction_column="prediction",
keep_tfidf_object=True)

predict_fn, pred_train, log = learner_keep_tfidf(df_train_binary)

pred_test = predict_fn(df_test_binary)

expected_col_train = df_train_binary.columns.tolist() + ["prediction"]
expected_col_test = df_test_binary.columns.tolist() + ["prediction"]

assert Counter(expected_col_train) == Counter(pred_train.columns.tolist())
assert Counter(expected_col_test) == Counter(pred_test.columns.tolist())
assert pred_test.prediction.max() < 1
assert pred_test.prediction.min() > 0
assert (pred_test.columns == pred_train.columns).all()
assert "tf_idf_object" in log


def test_lgbm_classification_learner():
df_train_binary = pd.DataFrame({
Expand Down
Loading