Exemplo n.º 1
0
    ks = KShape(n_clusters=numCluster, n_init=1, random_state=0)
    pred = ks.fit_predict(input_waves)

    clustFile = h5py.File(
        templatePath + str(numCluster) + "/" + str(numCluster) +
        "_cluster_predictions_" + str(prefiltFreq[0]) + "-" +
        str(prefiltFreq[1]) + "Hz.h5", "w")
    clustFile.create_dataset("cluster_index", data=pred)
    clustFile.create_dataset("centroids", data=ks.cluster_centers_)
    clustFile.create_dataset("inertia", data=ks.inertia_)
    clustFile.close()

    modelFile = templatePath + str(numCluster) + "/" + str(
        numCluster) + "_cluster_model_" + str(prefiltFreq[0]) + "-" + str(
            prefiltFreq[1]) + "Hz.h5"
    ks.to_hdf5(modelFile)

    # load some variables
    centroids = ks.cluster_centers_

# for each cluster, cross correlate, align and plot each event in the cluster in reference to the centroid
for c in range(numCluster):

    # load centroid into dummy obspy event as master waveform for cross correlation
    masterEvent = obspy.read(templatePath + "dummy_2Hz.h5")
    masterEvent[0].data = centroids[c].ravel()

    # make empty array for storage
    clusterEvents = waves[pred == c]
    clusterEvents_norm = input_waves[pred == c]
    clusterEventsAligned = np.zeros(
Exemplo n.º 2
0
# 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]

# Instantiate k-Shape model
ks = KShape(n_clusters=3, verbose=True, random_state=seed)

# Train
ks.fit(X_train)

# Save model
ks.to_hdf5('./ks_trained.hdf5')

# Load model
trained_ks = KShape.from_hdf5('./ks_trained.hdf5')

# Use loaded model to make predictions
y_pred = trained_ks.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)
    plt.plot(ks.cluster_centers_[yi].ravel(), "r-")
    plt.xlim(0, sz)
    plt.ylim(-4, 4)