PrefixSpan

class pyspark.ml.fpm.PrefixSpan(minSupport=0.1, maxPatternLength=10, maxLocalProjDBSize=32000000, sequenceCol='sequence')[source]

A parallel PrefixSpan algorithm to mine frequent sequential patterns. The PrefixSpan algorithm is described in J. Pei, et al., PrefixSpan: Mining Sequential Patterns Efficiently by Prefix-Projected Pattern Growth (see <a href=”https://doi.org/10.1109/ICDE.2001.914830”>here</a>). This class is not yet an Estimator/Transformer, use findFrequentSequentialPatterns() method to run the PrefixSpan algorithm.

@see <a href=”https://en.wikipedia.org/wiki/Sequential_Pattern_Mining”>Sequential Pattern Mining (Wikipedia)</a>

>>> from pyspark.ml.fpm import PrefixSpan
>>> from pyspark.sql import Row
>>> df = sc.parallelize([Row(sequence=[[1, 2], [3]]),
...                      Row(sequence=[[1], [3, 2], [1, 2]]),
...                      Row(sequence=[[1, 2], [5]]),
...                      Row(sequence=[[6]])]).toDF()
>>> prefixSpan = PrefixSpan()
>>> prefixSpan.getMaxLocalProjDBSize()
32000000
>>> prefixSpan.getSequenceCol()
'sequence'
>>> prefixSpan.setMinSupport(0.5)
PrefixSpan...
>>> prefixSpan.setMaxPatternLength(5)
PrefixSpan...
>>> prefixSpan.findFrequentSequentialPatterns(df).sort("sequence").show(truncate=False)
+----------+----+
|sequence  |freq|
+----------+----+
|[[1]]     |3   |
|[[1], [3]]|2   |
|[[2]]     |3   |
|[[2, 1]]  |3   |
|[[3]]     |2   |
+----------+----+
...

New in version 2.4.0.

Methods

Attributes

Methods Documentation

clear(param)

Clears a param from the param map if it has been explicitly set.

copy(extra=None)

Creates a copy of this instance with the same uid and some extra params. This implementation first calls Params.copy and then make a copy of the companion Java pipeline component with extra params. So both the Python wrapper and the Java pipeline component get copied.

Parameters

extra – Extra parameters to copy to the new instance

Returns

Copy of this instance

explainParam(param)

Explains a single param and returns its name, doc, and optional default value and user-supplied value in a string.

explainParams()

Returns the documentation of all params with their optionally default values and user-supplied values.

extractParamMap(extra=None)

Extracts the embedded default param values and user-supplied values, and then merges them with extra values from input into a flat param map, where the latter value is used if there exist conflicts, i.e., with ordering: default param values < user-supplied values < extra.

Parameters

extra – extra param values

Returns

merged param map

findFrequentSequentialPatterns(dataset)[source]

Finds the complete set of frequent sequential patterns in the input sequences of itemsets.

Parameters

dataset – A dataframe containing a sequence column which is ArrayType(ArrayType(T)) type, T is the item type for the input dataset.

Returns

A DataFrame that contains columns of sequence and corresponding frequency. The schema of it will be: - sequence: ArrayType(ArrayType(T)) (T is the item type) - freq: Long

New in version 2.4.0.

New in version 2.4.0.

getMaxLocalProjDBSize()[source]

Gets the value of maxLocalProjDBSize or its default value.

New in version 3.0.0.

getMaxPatternLength()[source]

Gets the value of maxPatternLength or its default value.

New in version 3.0.0.

getMinSupport()[source]

Gets the value of minSupport or its default value.

New in version 3.0.0.

getOrDefault(param)

Gets the value of a param in the user-supplied param map or its default value. Raises an error if neither is set.

getParam(paramName)

Gets a param by its name.

getSequenceCol()[source]

Gets the value of sequenceCol or its default value.

New in version 3.0.0.

hasDefault(param)

Checks whether a param has a default value.

hasParam(paramName)

Tests whether this instance contains a param with a given (string) name.

isDefined(param)

Checks whether a param is explicitly set by user or has a default value.

isSet(param)

Checks whether a param is explicitly set by user.

set(param, value)

Sets a parameter in the embedded param map.

setMaxLocalProjDBSize(value)[source]

Sets the value of maxLocalProjDBSize.

New in version 3.0.0.

setMaxPatternLength(value)[source]

Sets the value of maxPatternLength.

New in version 3.0.0.

setMinSupport(value)[source]

Sets the value of minSupport.

New in version 3.0.0.

setParams(self, minSupport=0.1, maxPatternLength=10, maxLocalProjDBSize=32000000, sequenceCol='sequence')[source]

New in version 2.4.0.

setSequenceCol(value)[source]

Sets the value of sequenceCol.

New in version 3.0.0.

Attributes Documentation

maxLocalProjDBSize = Param(parent='undefined', name='maxLocalProjDBSize', doc='The maximum number of items (including delimiters used in the internal storage format) allowed in a projected database before local processing. If a projected database exceeds this size, another iteration of distributed prefix growth is run. Must be > 0.')
maxPatternLength = Param(parent='undefined', name='maxPatternLength', doc='The maximal length of the sequential pattern. Must be > 0.')
minSupport = Param(parent='undefined', name='minSupport', doc='The minimal support level of the sequential pattern. Sequential pattern that appears more than (minSupport * size-of-the-dataset) times will be output. Must be >= 0.')
params

Returns all params ordered by name. The default implementation uses dir() to get all attributes of type Param.

sequenceCol = Param(parent='undefined', name='sequenceCol', doc='The name of the sequence column in dataset, rows with nulls in this column are ignored.')