trendy.
Trendy
Bases: object
object
Estimator to cluster trend-lines and assign new lines accordingly.
Notes
Scaling and missing values need to be handled externally.
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
Compute clustering based on given distance algorithm.
X (array of arrays) – Training instances to cluster.
>>> 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
Predict the closest cluster each sample in X belongs to.
X (array of arrays) – New data to predict.
Index of the cluster each sample belongs to.
list
>>> 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
Alias of predict()
fit_predict
Compute cluster centers and predict cluster index for each sample.
predicted labels
>>> 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
Pickle (serialize) object to a file.
path (str) – file path where the pickled object will be stored
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')