trendy

class trendy.Trendy(n_clusters, algorithm=<function fastdtw_distance>)[source]

Bases: object

Estimator to cluster trend-lines and assign new lines accordingly.

Notes

Scaling and missing values need to be handled externally.

Parameters
  • n_clusters (int) – The number of clusters to form.

  • algorithm (callable) – Algorithm to calculate the difference. Default is fast DTW with Euclidean.

Example

>>> a = [1, 2, 3, 4, 5] # increasing trend
>>> b = [1, 2.1, 2.9, 4.4, 5.1] # increasing trend
>>> c = [6.2, 5, 4, 3, 2] # decreasing trend
>>> d = [7, 6, 5, 4, 3, 2, 1] # decreasing trend
>>> trendy = Trendy(n_clusters=2)
>>> trendy.fit([a, b, c, d])
>>> print(trendy.labels_)
[0, 0, 1, 1]
>>> trendy.predict([[0.9, 2, 3.1, 4]]) # another increasing trend
[0]
fit(X)[source]

Compute clustering based on given distance algorithm.

Parameters

X (array of arrays) – Training instances to cluster.

Example

>>> a = [1, 2, 3, 4, 5] # increasing
>>> b = [1, 2.1, 2.9, 4.4, 5.1] # increasing
>>> c = [6.2, 5, 4, 3, 2] # decreasing
>>> d = [7, 6, 5, 4, 3, 2, 1] # decreasing
>>> trendy = Trendy(2)
>>> trendy.fit([a, b, c, d])
>>> print(trendy.labels_)
[0, 0, 1, 1]
predict(X)[source]

Predict the closest cluster each sample in X belongs to.

Parameters

X (array of arrays) – New data to predict.

Returns

Index of the cluster each sample belongs to.

Return type

list

Example

>>> a = [1, 2, 3, 4, 5] # increasing
>>> b = [1, 2.1, 2.9, 4.4, 5.1] # increasing
>>> c = [6.2, 5, 4, 3, 2] # decreasing
>>> d = [7, 6, 5, 4, 3, 2, 1] # decreasing
>>> trendy = Trendy(2)
>>> trendy.fit([a, b, c, d])
>>> trendy.predict([[0.9, 2, 3.1, 4]])
[0]
>>> trendy.predict([[0.9, 2, 3.1], [7, 6.6, 5.5, 4.4]])
[0, 1]
assign(X)[source]

Alias of predict()

fit_predict(X)[source]

Compute cluster centers and predict cluster index for each sample.

Parameters

X (array of arrays) – Training instances to cluster.

Returns

predicted labels

Return type

list

Example

>>> a = [1, 2, 3, 4, 5] # increasing
>>> b = [1, 2.1, 2.9, 4.4, 5.1] # increasing
>>> c = [6.2, 5, 4, 3, 2] # decreasing
>>> d = [7, 6, 5, 4, 3, 2, 1] # decreasing
>>> trendy = Trendy(2)
>>> trendy.fit_predict([a, b, c, d])
[0, 0, 1, 1]
to_pickle(path)[source]

Pickle (serialize) object to a file.

Parameters

path (str) – file path where the pickled object will be stored

Example

To save a *.pkl file:

>>> t1 = Trendy(n_clusters=2)
>>> t1.fit([[1, 2, 3], [2, 3, 3]])
>>> t1.to_pickle(path='trendy.pkl')

To load the same object later:

>>> import pickle, os
>>> pkl_file = open('trendy.pkl', 'rb')
>>> t2 = pickle.load(pkl_file)
>>> pkl_file.close()
>>> os.remove('trendy.pkl')