scikit-learn
Combines a text feature vectorizer with a classifier in a pipeline.
- Downloads / mo
- 214.9M
- Stars
- 68.2K
- Last release
- 1.9.1
- License
- BSD-3-Clause
Strengths
- Combines text with structured features. Transform separate input columns and concatenate their features before classification.
- Tunes complete text pipelines. Search vectorizer and classifier settings together.
Limitations
- Unigrams lose word order. Use word n-grams when short phrases carry the label.
Quickstart
from sklearn.pipeline import Pipeline from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import LogisticRegression model = Pipeline([("text", TfidfVectorizer()), ("classifier", LogisticRegression())]) model.fit(["invoice due", "pay invoice", "reset password", "forgot password"], ["billing", "billing", "account", "account"]) print(model.predict(["invoice"]))
scikit-learn examples
def __init__(self, lemmatization=False): BugModel.__init__(self, lemmatization) self.cross_validation_enabled = False self.calculate_importance = False # The test set is made of the most recent bugs (see train_test_split), so # retrain on all bugs after the evaluation. self.entire_dataset_training = True feature_extractors = [ bug_features.HasSTR(), bug_features.Severity(), bug_features.Keywords(), bug_features.HasCrashSignature(), bug_features.HasURL(), bug_features.HasW3CURL(), bug_features.HasGithubURL(), bug_features.Whiteboard(), bug_features.Patches(), bug_features.Landings(), bug_features.BugReporter(), # Bugs are rolled back to their creation, so this is the product the bug # was filed in. bug_features.Product(), bug_features.OpSys(), bug_features.Platform(), bug_features.MentionedPathsComponents(), ] cleanup_functions = [ feature_cleanup.fileref(), feature_cleanup.url(), feature_cleanup.synonyms(), ] self.extraction_pipeline = Pipeline( [ ( "bug_extractor", bug_features.BugExtractor( feature_extractors, cleanup_functions, rollback=True ), ), ] ) # A linear model on sparse n-gram features is more accurate (and faster # to train) than gradient boosted trees for this task. The L1 penalty # keeps the weights sparse, so the model stays small despite the large # vocabulary. Probabilities are calibrated on out-of-fold predictions. estimator = CalibratedClassifierCV( LinearSVC(penalty="l1", dual=False), method="isotonic", cv=3, ensemble=False, n_jobs=3, ) self.clf = Pipeline( [ ( "union", ColumnTransformer( [ ("data", DictVectorizer(), "data"), ( "title", self.text_vectorizer( min_df=2, ngram_range=(1, 2), sublinear_tf=True, max_features=30000, ), "title", ), ( "title_char", TfidfVectorizer( min_df=3, analyzer="char_wb", ngram_range=(2, 5), sublinear_tf=True, max_features=30000, ), "title", ), ( "comments", self.text_vectorizer( min_df=2, ngram_range=(1, 2), sublinear_tf=True, max_features=80000, ), "comments", ), ] ), ), ( "estimator", estimator, ), ] )