Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def row_col(position, n_cols=5):
idx_row = (position - 1) // n_cols
idx_col = position - n_cols * idx_row - 1
return idx_row, idx_col
def get_color(weights):
baselines = numpy.zeros((4, 3))
weights = numpy.array(weights).reshape(1, 4)
for i, c in enumerate(["r", "g", "b", "y"]):
baselines[i] = matplotlib.colors.ColorConverter().to_rgb(c)
return numpy.dot(weights, baselines).ravel()
numpy.random.seed(0)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
X_out = numpy.empty((4, X_train.shape[1], X_train.shape[2]))
plt.figure()
for i in range(4):
X_out[i] = X_train[y_train == (i + 1)][0]
X_out = TimeSeriesScalerMinMax().fit_transform(X_out)
for i, pos in enumerate([1, 5, 21, 25]):
plt.subplot(5, 5, pos)
w = [0.] * 4
w[i] = 1.
plt.plot(X_out[i].ravel(),
color=matplotlib.colors.rgb2hex(get_color(w)),
linewidth=2)
plt.text(X_out[i].shape[0], 0., "$X_%d$" % i,
horizontalalignment="right",
>>> clf = ShapeletModel(n_shapelets_per_size={10: 5}, max_iter=1, verbose_level=0)
>>> clf.fit(X, y).get_weights("softmax")[0].shape
(5, 3)
"""
if layer_name is None:
return self.model.get_weights()
else:
return self.model.get_layer(layer_name).get_weights()
if __name__ == "__main__":
from tslearn.datasets import CachedDatasets
from tslearn.preprocessing import TimeSeriesScalerMeanVariance
import time
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
X_train = TimeSeriesScalerMeanVariance().fit_transform(X_train)
X_test = TimeSeriesScalerMeanVariance().fit_transform(X_test)
ts_sz = X_train.shape[1]
l, r = 0.1, 2 # Taken (for dataset Trace) from the Table at:
# http://fs.ismll.de/publicspace/LearningShapelets/
n_classes = len(set(y_train))
n_shapelets_per_size = grabocka_params_to_shapelet_size_dict(ts_sz, n_classes, l, r)
t0 = time.time()
clf = ShapeletModel(n_shapelets_per_size=n_shapelets_per_size,
max_iter=1000,
optimizer=RMSprop(lr=.001),
weight_regularizer=.01,
verbose_level=0)
clf.fit(X_train, y_train)
explained in details in "Fast global alignment kernels", by M. Cuturi
(ICML 2011).
"""
# Author: Romain Tavenard
# License: BSD 3 clause
import numpy
import matplotlib.pyplot as plt
from tslearn.datasets import CachedDatasets
from tslearn.preprocessing import TimeSeriesScalerMinMax
from tslearn.svm import TimeSeriesSVC
numpy.random.seed(0)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
X_train = TimeSeriesScalerMinMax().fit_transform(X_train)
X_test = TimeSeriesScalerMinMax().fit_transform(X_test)
clf = TimeSeriesSVC(kernel="gak",
gamma=.1)
clf.fit(X_train, y_train)
print("Correct classification rate:", clf.score(X_test, y_test))
n_classes = len(set(y_train))
plt.figure()
support_vectors = clf.support_vectors_time_series_(X_train)
for i, cl in enumerate(set(y_train)):
plt.subplot(n_classes, 1, i + 1)
plt.title("Support vectors for class %d" % (cl))
for ts in support_vectors[i]:
"""
# Author: Romain Tavenard
# License: BSD 3 clause
import numpy
from keras.optimizers import Adagrad
import matplotlib.pyplot as plt
from tslearn.datasets import CachedDatasets
from tslearn.preprocessing import TimeSeriesScalerMinMax
from tslearn.shapelets import ShapeletModel, \
grabocka_params_to_shapelet_size_dict
numpy.random.seed(0)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
X_train = TimeSeriesScalerMinMax().fit_transform(X_train)
X_test = TimeSeriesScalerMinMax().fit_transform(X_test)
n_ts, ts_sz = X_train.shape[:2]
n_classes = len(set(y_train))
# Set the number of shapelets per size as done in the original paper
shapelet_sizes = grabocka_params_to_shapelet_size_dict(n_ts=n_ts,
ts_sz=ts_sz,
n_classes=n_classes,
l=0.1,
r=2)
shp_clf = ShapeletModel(n_shapelets_per_size=shapelet_sizes,
optimizer=Adagrad(lr=.1),
weight_regularizer=.01,
# Author: Romain Tavenard
# License: BSD 3 clause
import numpy
from sklearn.metrics import accuracy_score
from keras.optimizers import Adagrad
import matplotlib.pyplot as plt
from tslearn.datasets import CachedDatasets
from tslearn.preprocessing import TimeSeriesScalerMinMax
from tslearn.shapelets import ShapeletModel, \
grabocka_params_to_shapelet_size_dict
from tslearn.utils import ts_size
numpy.random.seed(0)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
X_train = TimeSeriesScalerMinMax().fit_transform(X_train)
X_test = TimeSeriesScalerMinMax().fit_transform(X_test)
n_ts, ts_sz = X_train.shape[:2]
n_classes = len(set(y_train))
# Set the number of shapelets per size as done in the original paper
shapelet_sizes = grabocka_params_to_shapelet_size_dict(n_ts=n_ts,
ts_sz=ts_sz,
n_classes=n_classes,
l=0.1,
r=2)
# Define the model using parameters provided by the authors (except that we use
# fewer iterations here)
shp_clf = ShapeletModel(n_shapelets_per_size=shapelet_sizes,
"""
# Author: Romain Tavenard
# License: BSD 3 clause
import numpy
import matplotlib.pyplot as plt
from tslearn.clustering import TimeSeriesKMeans
from tslearn.datasets import CachedDatasets
from tslearn.preprocessing import TimeSeriesScalerMeanVariance, \
TimeSeriesResampler
seed = 0
numpy.random.seed(seed)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
X_train = X_train[y_train < 4] # Keep first 3 classes
numpy.random.shuffle(X_train)
# Keep only 50 time series
X_train = TimeSeriesScalerMeanVariance().fit_transform(X_train[:50])
# Make time series shorter
X_train = TimeSeriesResampler(sz=40).fit_transform(X_train)
sz = X_train.shape[1]
# Euclidean k-means
print("Euclidean k-means")
km = TimeSeriesKMeans(n_clusters=3, verbose=True, random_state=seed)
y_pred = km.fit_predict(X_train)
plt.figure()
for yi in range(3):
plt.subplot(3, 3, yi + 1)
This example shows three methods to compute barycenters of time series.
"""
# Author: Romain Tavenard
# License: BSD 3 clause
import numpy
import matplotlib.pyplot as plt
from tslearn.barycenters import euclidean_barycenter, \
dtw_barycenter_averaging, softdtw_barycenter
from tslearn.datasets import CachedDatasets
numpy.random.seed(0)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
X = X_train[y_train == 2]
plt.figure()
plt.subplot(3, 1, 1)
for ts in X:
plt.plot(ts.ravel(), "k-", alpha=.2)
plt.plot(euclidean_barycenter(X).ravel(), "r-", linewidth=2)
plt.title("Euclidean barycenter")
plt.subplot(3, 1, 2)
dba_bar = dtw_barycenter_averaging(X, max_iter=100, verbose=False)
for ts in X:
plt.plot(ts.ravel(), "k-", alpha=.2)
plt.plot(dba_bar.ravel(), "r-", linewidth=2)
plt.title("DBA")
# Our pipeline consists of two phases. First, data will be normalized using
# min-max normalization. Afterwards, it is fed to a KNN classifier. For the
# KNN classifier, we tune the n_neighbors and weights hyper-parameters.
n_splits = 3
pipeline = GridSearchCV(
Pipeline([
('normalize', TimeSeriesScalerMinMax()),
('knn', KNeighborsTimeSeriesClassifier())
]),
{'knn__n_neighbors': [5, 25], 'knn__weights': ['uniform', 'distance']},
cv=StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=42),
iid=True
)
X_train, y_train, _, _ = CachedDatasets().load_dataset("Trace")
# Keep only timeseries of class 0, 1 or 2
X_train = X_train[y_train < 4]
y_train = y_train[y_train < 4]
# Keep only the first 50 timeseries of both train and
# retain only a small amount of each of the timeseries
X_train, y_train = X_train[:50, 50:150], y_train[:50]
# Plot our timeseries
colors = ['g', 'b', 'r']
plt.figure()
for ts, label in zip(X_train, y_train):
plt.plot(ts, c=colors[label - 1], alpha=0.5)
plt.title('The timeseries in the dataset')
plt.tight_layout()
"""
# Author: Romain Tavenard
# License: BSD 3 clause
import numpy
import matplotlib.pyplot as plt
from tslearn.clustering import GlobalAlignmentKernelKMeans
from tslearn.metrics import sigma_gak
from tslearn.datasets import CachedDatasets
from tslearn.preprocessing import TimeSeriesScalerMeanVariance
seed = 0
numpy.random.seed(seed)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
# Keep first 3 classes
X_train = X_train[y_train < 4]
numpy.random.shuffle(X_train)
# Keep only 50 time series
X_train = TimeSeriesScalerMeanVariance().fit_transform(X_train[:50])
sz = X_train.shape[1]
gak_km = GlobalAlignmentKernelKMeans(n_clusters=3,
sigma=sigma_gak(X_train),
n_init=20,
verbose=True,
random_state=seed)
y_pred = gak_km.fit_predict(X_train)
plt.figure()
for yi in range(3):
cross-correlation to cluster time series.
"""
# Author: Romain Tavenard
# License: BSD 3 clause
import numpy
import matplotlib.pyplot as plt
from tslearn.clustering import KShape
from tslearn.datasets import CachedDatasets
from tslearn.preprocessing import TimeSeriesScalerMeanVariance
seed = 0
numpy.random.seed(seed)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
# Keep first 3 classes
X_train = X_train[y_train < 4]
numpy.random.shuffle(X_train)
# Keep only 50 time series
X_train = TimeSeriesScalerMeanVariance().fit_transform(X_train[:50])
sz = X_train.shape[1]
# Euclidean k-means
ks = KShape(n_clusters=3, verbose=True, random_state=seed)
y_pred = ks.fit_predict(X_train)
plt.figure()
for yi in range(3):
plt.subplot(3, 1, 1 + yi)
for xx in X_train[y_pred == yi]:
plt.plot(xx.ravel(), "k-", alpha=.2)