Arthur
Arthur

Reputation: 23

Time series clustering using DBSCAN or OPTICS

I am working with Fuzzy Time Series and am trying to build a model to predict the TAIEX time series.

Image of the time series: https://i.sstatic.net/04qkT.png

In order to do that, I want to use DBSCAN or OPTICS to cluster this time series into 'n' clusters and this number will be further used to build fuzzy sets.

Q1- Are DBSCAN or OPTICS really suitable ways of clustering a time series?

Q2- I am facing a problem when using DBSCAN. Even changing the parameters (eps and min_samples), most of the points in the series are being identified as outliers (black dots). What could be happening?

My code:

'time_data is a numpy array containing the time series'

db = DBSCAN(eps=50, min_samples=100).fit(time_data)

core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_

# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
n_noise_ = list(labels).count(-1)

print('Estimated number of clusters: %d' % n_clusters_)
print('Estimated number of noise points: %d' % n_noise_)

# Plot result
import matplotlib.pyplot as plt

# Black removed and is used for noise instead.
unique_labels = set(labels)
colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))]
for k, col in zip(unique_labels, colors):
    if k == -1:
        # Black used for noise.
        col = [0, 0, 0, 1]

    class_member_mask = (labels == k)

    xy = dados[class_member_mask & core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),markeredgecolor='k', markersize=14)

    xy = dados[class_member_mask & ~core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),markeredgecolor='k', markersize=6)

plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()

Thank you all for your time!

Upvotes: 0

Views: 668

Answers (1)

Marmud Vicen
Marmud Vicen

Reputation: 1

Maybe you could start by looking at kmeans for clustering. Although I am not sure it is suitable for time series.

Upvotes: 0

Related Questions