.. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_segment_rep.py: =================================================== Classifying Segments Directly with a Neural Network =================================================== This is a basic example using a convolutional recurrent neural network to learn segments directly from time series data .. image:: /auto_examples/images/sphx_glr_plot_segment_rep_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none N series in train: 105 N series in test: 35 N segments in train: 1783 N segments in test: 586 Accuracy score: 0.19112628698349 | .. code-block:: default # Author: David Burns # License: BSD import matplotlib.image as mpimg import matplotlib.pyplot as plt from tensorflow.python.keras.layers import Dense, LSTM, Conv1D from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.wrappers.scikit_learn import KerasClassifier from sklearn.model_selection import train_test_split from seglearn.datasets import load_watch from seglearn.pipe import Pype from seglearn.transform import Segment def crnn_model(width=100, n_vars=6, n_classes=7, conv_kernel_size=5, conv_filters=3, lstm_units=3): input_shape = (width, n_vars) model = Sequential() model.add(Conv1D(filters=conv_filters, kernel_size=conv_kernel_size, padding='valid', activation='relu', input_shape=input_shape)) model.add(Conv1D(filters=conv_filters, kernel_size=conv_kernel_size, padding='valid', activation='relu')) model.add(LSTM(units=lstm_units, dropout=0.1, recurrent_dropout=0.1)) model.add(Dense(n_classes, activation="softmax")) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model # load the data data = load_watch() X = data['X'] y = data['y'] # create a segment learning pipeline pipe = Pype([('seg', Segment(width=100, step=100, order='C')), ('crnn', KerasClassifier(build_fn=crnn_model, epochs=1, batch_size=256, verbose=0))]) # split the data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42) pipe.fit(X_train, y_train) score = pipe.score(X_test, y_test) print("N series in train: ", len(X_train)) print("N series in test: ", len(X_test)) print("N segments in train: ", pipe.N_train) print("N segments in test: ", pipe.N_test) print("Accuracy score: ", score) img = mpimg.imread('segments.jpg') plt.imshow(img) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 7.691 seconds) .. _sphx_glr_download_auto_examples_plot_segment_rep.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_segment_rep.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_segment_rep.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_