示例#1
0
def build_gunpoint(random_state=0, verbose=True, label_encoder=True):
    X_all, X_test, y_all, y_test = load_gunpoint(return_X_y=True)
    X_all = X_all[:, :, np.newaxis]
    X_test = X_test[:, :, np.newaxis]
    if label_encoder:
        le = LabelEncoder()
        le.fit(y_all)
        y_all = le.transform(y_all)
        y_test = le.transform(y_test)

    if verbose:
        print("DATASET INFO:")
        print("X SHAPE: ", X_all.shape)
        print("y SHAPE: ", y_all.shape)
        unique, counts = np.unique(y_all, return_counts=True)
        print("\nCLASSES BALANCE")
        for i, label in enumerate(unique):
            print(label, ": ", round(counts[i] / sum(counts), 2))

    # BLACKBOX/EXPLANATION SETS SPLIT
    X_train, X_val, y_train, y_val = train_test_split(
        X_all,
        y_all,
        test_size=0.2,
        stratify=y_all, random_state=random_state
    )

    X_exp_train = X_train.copy()
    y_exp_train = y_train.copy()
    X_exp_val = X_val.copy()
    y_exp_val = y_val.copy()
    X_exp_test = X_test.copy()
    y_exp_test = y_test.copy()

    warnings.warn("Blackbox and Explanation sets are the same")

    if verbose:
        print("\nSHAPES:")
        print("BLACKBOX TRAINING SET: ", X_train.shape)
        print("BLACKBOX VALIDATION SET: ", X_val.shape)
        print("BLACKBOX TEST SET: ", X_test.shape)
        print("EXPLANATION TRAINING SET: ", X_exp_train.shape)
        print("EXPLANATION VALIDATION SET: ", X_exp_val.shape)
        print("EXPLANATION TEST SET: ", X_exp_test.shape)
        print("\nBlackbox and Explanation sets are the same!")

    return (X_train, y_train, X_val, y_val, X_test, y_test, X_exp_train,
            y_exp_train, X_exp_val, y_exp_val, X_exp_test, y_exp_test)
示例#2
0
For Gun-Draw the actors have their hands by their sides. They draw
a replicate gun from a hip-mounted holster, point it at a target for
approximately one second, then return the gun to the holster, and
their hands to their sides. For Point the actors have their gun by
their sides. They point with their index fingers to a target for
approximately one second, and then return their hands to their sides.
For both classes, we tracked the centroid of the actor's right hands
in both X- and Y-axes, which appear to be highly correlated. The
data in the archive is just the X-axis.
It is implemented as :func:`pyts.datasets.load_gunpoint`.
"""

import matplotlib.pyplot as plt
from pyts.datasets import load_gunpoint

X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)
n_samples_per_plot = 3

plt.figure(figsize=(12, 8))

for i, (
        X,
        y,
        set_,
        class_,
) in enumerate(
        zip([X_train, X_train, X_test, X_test],
            [y_train, y_train, y_test, y_test],
            ['Training', 'Training', 'Test', 'Test'], [1, 2, 1, 2])):
    plt.subplot(2, 2, i + 1)
    for i in range(n_samples_per_plot):
示例#3
0
set of values from consecutive time points. The distance between a shapelet
and a time series is defined as the minimum of the distances between this
shapelet and all the shapelets of same length extracted from this time series.
The most discriminative shapelets are selected.
This example illustrates the transformation of this algorithm and highlights
the most discriminative shapelets that have been selected. It is implemented
as :class:`pyts.transformation.ShapeletTransform`.
"""

import numpy as np
import matplotlib.pyplot as plt
from pyts.datasets import load_gunpoint
from pyts.transformation import ShapeletTransform

# Toy dataset
X_train, _, y_train, _ = load_gunpoint(return_X_y=True)

# Shapelet transformation
st = ShapeletTransform(window_sizes=[12, 24, 36, 48],
                       random_state=42,
                       sort=True)
X_new = st.fit_transform(X_train, y_train)

# Visualize the four most discriminative shapelets
plt.figure(figsize=(6, 4))
for i, index in enumerate(st.indices_[:4]):
    idx, start, end = index
    plt.plot(X_train[idx],
             color='C{}'.format(i),
             label='Sample {}'.format(idx))
    plt.plot(np.arange(start, end),
"""
@author: masterqkk, [email protected]
Environment:
    python: 3.6
    Pandas: 1.0.3
    matplotlib: 3.2.1
"""
import pickle
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as scisig
from mpl_toolkits.axes_grid1 import make_axes_locatable
from pyts.datasets import load_gunpoint

if __name__ == '__main__':
    X, _, _, _ = load_gunpoint(return_X_y=True)

    fs = 10e3  # sampling frequency
    N = 1e5  # 10 s 1signal
    amp = 2 * np.sqrt(2)
    time = np.arange(N) / float(fs)
    mod = 500 * np.cos(2 * np.pi * 0.25 * time)
    carrier = amp * np.sin(2 * np.pi * 3e3 * time + mod)
    noise_power = 0.01 * fs / 2
    noise = np.random.normal(loc=0.0,
                             scale=np.sqrt(noise_power),
                             size=time.shape)
    noise *= np.exp(-time / 5)
    x = carrier + noise  # signal with noise

    per_seg_length = 1000  # window length
示例#5
0
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import sys
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
np.set_printoptions(threshold=sys.maxsize)
#print("shape is", data_train.shape)
# km = KMeans(
#     n_clusters=3, init='random',
#     n_init=10, max_iter=300,
#     tol=1e-04, random_state=0
# )
# y_km = km.fit(data_train)
instances = readData("./venezia/Punta_Salute_1983_2015/Punta_Salute_2015.csv")

data_train, data_test, target_train, target_test = load_gunpoint(return_X_y=True)



window_size, word_size = 50, 5
bow = BagOfWords(window_size=window_size, word_size=word_size,
                 window_step=window_size, numerosity_reduction=False)
X_bow = bow.transform(data_train)
test_bow = bow.transform(data_test)


frequencyDictListX = []
frequencyDictListTest = []

for i in range(len(X_bow)):
    frequencyDict = {}
if __name__ == '__main__':

    import timeit
    import numpy as np
    import pandas as pd
    import datetime as dt

    from ast import literal_eval
    from pyts.datasets import load_gunpoint
    from sklearn.metrics import accuracy_score, recall_score, precision_score

    from sktime_dl.deeplearning import CNNClassifier, FCNClassifier, MLPClassifier, \
        InceptionTimeClassifier, ResNetClassifier, EncoderClassifier

    X_train, X_test, y_train, y_test = load_gunpoint(
        return_X_y=True)  # get gunpoint data from pyts

    # as of the date of this publication, please note the following issue needs to be fixed mannually before running
    # https://github.com/sktime/sktime-dl/issues/79
    '''
    def prepare_for_sktime_dl(data):
        df = pd.DataFrame(data)
        cols = df.columns
        df['dim_0'] = df[cols].apply(lambda row: ', '.join(row.values.astype(str)), axis=1)
        df['dim_0'] = ['[' + i + ']' for i in df.dim_0 ]
        df['dim_0'] = df['dim_0'].apply(literal_eval)
        return df.drop(columns=cols)'''

    X_train = np.reshape(X_train, X_train.shape + (1, ))
    X_test = np.reshape(X_test, X_test.shape + (1, ))
示例#7
0
"""Testing for base classes."""

# Author: Johann Faouzi <*****@*****.**>
# License: BSD-3-Clause

import numpy as np
import pytest
from sklearn.base import clone

from pyts.classification import SAXVSM
from pyts.datasets import load_gunpoint, load_basic_motions
from pyts.multivariate.image import JointRecurrencePlot
from pyts.multivariate.classification import MultivariateClassifier
from pyts.approximation import SymbolicFourierApproximation

X_uni, _, y_uni, _ = load_gunpoint(return_X_y=True)
X_multi, _, y_multi, _ = load_basic_motions(return_X_y=True)


@pytest.mark.parametrize('estimator, X, y', [
    (SymbolicFourierApproximation(n_bins=2), X_uni, None),
    (SymbolicFourierApproximation(n_bins=2, strategy='entropy'), X_uni, y_uni)
])
def test_univariate_transformer_mixin(estimator, X, y):
    sfa_1 = clone(estimator)
    sfa_2 = clone(estimator)
    np.testing.assert_array_equal(sfa_1.fit_transform(X, y),
                                  sfa_2.fit(X, y).transform(X))


@pytest.mark.parametrize('estimator, X, y',
    def getData(self,set_type): 
        ###################
        ## Data Generation
        ###################
        if set_type == "generator": 
            #Settings
            windowLength = 1
            samplesPerWindow = 100
            n_classes = 6
            n_windows = 1000
            Gen = ImbalancedDataGenerator(n_samples= samplesPerWindow,resolution = windowLength/samplesPerWindow,SNR_dB = 50, variation = 1, n_classes=n_classes, useseed = False, seed = 5)
            windows_pool, windows_test, y_pool, y_test = Gen.GeneratePool(n_windows)
            class_names = Gen.class_names

            ## Generator
            ##Data shape
            #n_samples = 100 #samples of 1 window (1 window exists of X samples)
            #resolution = 0.01 # time step between 2 samples
            #SNR_dB = 50 # Signal to Noise ration in dB
            #variation = 1 # 0 to 1 (0 to 100%), higher values possible
            #Gen = Datagenerator2.DataGenerator2(n_samples= n_samples,resolution = resolution,SNR_dB = SNR_dB, variation = variation, n_classes=30, useseed = False, seed = 5)
            
            # size var , length var, n classes 
            #if obj.fast_mode and not obj.singleErrorOutput:       
                #x_pool, x_test, y_pool, y_test = obj.GeneratePool(obj.n_windows)
                #train_windows = None
                #test_windows = None
            #else:
            #    windows_pool, windows_test, y_pool, y_test = Gen.GeneratePool(obj.n_windows)
            #    pass

        elif set_type == "GunPoint":
            ## Gunpoint Dataset
            windowLength = 1 #unspecified !
            samplesPerWindow = 50
            n_classes = 2
            n_windows = 50   
            from pyts.datasets import load_gunpoint
            windows_pool, windows_test, y_pool, y_test = load_gunpoint(return_X_y=True)  
            class_names = ['gun', 'point']
            if y_pool.min() > 0 or y_test.min() > 0 :
                print("1st class is decoded as zero (was 1)")
                y_pool -= 1  #class 1 = class 0
                y_test -= 1
            
        elif set_type == "Crop":
            #http://www.timeseriesclassification.com/description.php?Dataset=Crop
            windowLength = 1 #unspecified !
            samplesPerWindow = 46
            n_classes = 24
            n_windows = 7200 
            from sktime.utils.load_data import load_from_tsfile_to_dataframe
            windows_pool, y_pool = load_from_tsfile_to_dataframe("/home/tob/Datasets/Crop_TRAIN.ts")
            windows_test, y_test = load_from_tsfile_to_dataframe("/home/tob/Datasets/Crop_TEST.ts")
            windows_pool, windows_test, y_pool, y_test = self.postProcessSetfromTSCcom(windows_pool, windows_test, y_pool, y_test,samplesPerWindow)
            class_names = ['Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5', 'Class 6', 'Class 7', 'Class 8', 'Class 9', 'Class 10', 'Class 11', 'Class 12', 'Class 13', 'Class 14', 'Class 15', 'Class 16', 'Class 17', 'Class 18', 'Class 19', 'Class 20', 'Class 21', 'Class 22', 'Class 23', 'Class 24']
            
        elif set_type == "FaceAll": 
            #http://www.timeseriesclassification.com/description.php?Dataset=FaceAll
            windowLength = 1 #unspecified !
            samplesPerWindow = 131
            n_classes = 14
            n_windows = 560
            from sktime.utils.load_data import load_from_tsfile_to_dataframe
            windows_pool, y_pool = load_from_tsfile_to_dataframe("/home/tob/Datasets/FaceAll_TRAIN.ts")
            windows_test, y_test = load_from_tsfile_to_dataframe("/home/tob/Datasets/FaceAll_TEST.ts")
            windows_pool, windows_test, y_pool, y_test = postProcessSetfromTSCcom(windows_pool, windows_test, y_pool, y_test,samplesPerWindow)
            class_names = ['Student 1', 'Student 2', 'Student 3', 'Student 4', 'Student 5', 'Student 6', 'Student 7', 'Student 8', 'Student 9', 'Student 10', 'Student 11', 'Student 12', 'Student 13', 'Student 14']

        elif set_type == "InsectWingbeat":
            #http://www.timeseriesclassification.com/description.php?Dataset=InsectWingbeat
            windowLength = 1 #unspecified !
            samplesPerWindow = 30
            n_classes = 10
            n_windows = 30000
            from sktime.utils.load_data import load_from_tsfile_to_dataframe
            windows_pool, y_pool = load_from_tsfile_to_dataframe("/home/tob/Datasets/InsectWingbeat_TRAIN.ts")
            windows_test, y_test = load_from_tsfile_to_dataframe("/home/tob/Datasets/InsectWingbeat_TEST.ts")
            windows_pool, windows_test, y_pool, y_test = postProcessSetfromTSCcom(windows_pool, windows_test, y_pool, y_test,samplesPerWindow)
            class_names = ['Insect 1', 'Insect 2', 'Insect 3', 'Insect 4', 'Insect 5', 'Insect 6', 'Insect 7', 'Insect 8', 'Insect 9', 'Insect 10']

        #make the Dataset imbalanced (class 0 to class 4 matters, class 5 don't care: exists of all other classes)
        #Setting
        n_classes = 6

        y_pool, y_test = np.clip(y_pool,0,n_classes-1), np.clip(y_test,0,n_classes-1)
        class_names = class_names[:n_classes]
        class_names[-1] = "don't care"
        print("imbalanced classes:")
        print(class_names)


        #reshaping for NN
        #if useNeuralNet == True:
        #    X = np.reshape(X, (X.shape[0],1,X.shape[1]))
        #    Y = np.reshape(Y, (Y.shape[0], 1))
        #    Y = to_categorical(Y,num_classes=self.n_classes)

        ## USE CASE 1 POOL : test = complete pool
        #x_test = np.copy(x_pool)
        #y_test = np.copy(y_pool)
        #test_windows = np.copy(train_windows)
        
        #obj.visualizeBoss(x_pool,y_pool)

        #Testing
        #print("Gen+ FST " + str(time.time() -test ))

        #End of Data Generation
        return windows_pool, windows_test, y_pool, y_test, class_names, windowLength, n_classes, samplesPerWindow