"""
SLIM vectorizer, using SLIM as a feature extraction scheme
"""
import numpy as np
import pandas as pd
from skmine.itemsets import SLIM
from skmine.itemsets.slim import _to_vertical, cover
STRATEGIES = ("codes", "one-hot")
def _filter_stop_items(D, stop_items):
for t in D:
yield set(t).difference(stop_items)
[docs]class SLIMVectorizer(SLIM):
"""SLIM mining, turned into a feature extraction step for sklearn
`k` new itemsets (associations of one or more items) are learned at training time
The model (pattern set) is then used to cover new data, in order of usage.
This is similar to one-hot-encoding, except the dimension will be much more concise,
because the columns will be patterns learned via an MDL criterion.
Parameters
----------
strategy: str, default="codes"
If the chosen strategy is set to `one-hot`, non-zero cells are filled with ones.
If the chosen `strategy` is left to `codes`, non-zero cells are filled with code lengths,
i.e the probabity of the pattern in the training data.
k: int, default=5
Number of non-singleton itemsets to mine.
A singleton is an itemset containing a single item.
Calls to `.transform` will output pandas.DataFrame with `k` columns
pruning: bool, default=False
Either to activate pruning or not.
stop_items: iterable, default=None
Set of items to filter out while ingesting the input data.
Examples
--------
>>> from skmine.feature_extraction import SLIMVectorizer
>>> D = [['bananas', 'milk'], ['milk', 'bananas', 'cookies'], ['cookies', 'butter', 'tea']]
>>> res = SLIMVectorizer(k=2).fit_transform(D)
>>> print(res.to_string())
(bananas, milk) (cookies,)
0 0.4 0.0
1 0.4 0.4
2 0.0 0.4
See Also
--------
skmine.itemsets.SLIM
Notes
-----
This transformer does not output scipy.sparse matrices,
as SLIM should learn a concise description of the data,
and covering new data with this small set of high usage
patterns should output matrices with very few zeros.
"""
def __init__(self, strategy="codes", k=5, pruning=False, stop_items=None, **kwargs):
super().__init__(**kwargs)
self.k = k
self.pruning = pruning
self.stop_items = stop_items
if strategy not in STRATEGIES:
raise ValueError(f"strategy must be one of {STRATEGIES}")
self.strategy = strategy
[docs] def fit(self, D, y=None):
if self.stop_items is not None:
D = _filter_stop_items(D, stop_items=self.stop_items)
return super().fit(D)