How to plot OPTICS clustering results using seaborn?

935 views Asked by At

I obtained features from 10 images from 2 categories (cats and dogs) using CNN. So I have a (10, 2500) numpy array. I applied the OPTICS clustering algorithm on the array to find which image belongs to which cluster

clustering = OPTICS(min_samples=2).fit(train_data_array)

Now I'm trying to plot the clusters using seaborn

sns.scatterplot(data=train_data_array).plot

But there's no plot.

1

There are 1 answers

0
StupidWolf On

There's two issues.

  1. The object returned by OPTICS only contain the labels, so you need to add it to your training data.
  2. The training data has 2500 variables, most likely you need to do a dimension reduction to render a 2-D plot.

Below is an example using iris dataset:

from sklearn.cluster import OPTICS
import seaborn as sns
import pandas as pd

df = sns.load_dataset("iris").iloc[:,:4]

Peform the clustering like you did:

clustering = OPTICS(min_samples=20).fit(df)

Perform PCA on this data with 4 variables, return top 2 components:

from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(df)

Add PC scores and clustering results to training data, or you can make a separate data.frame:

df['PC1'] = pca.fit_transform(df)[:,0]
df['PC2'] = pca.fit_transform(df)[:,1]
df['clustering'] = clustering.labels_

Plot:

sns.scatterplot(data=df,x="PC1",y="PC2",hue=df['clustering'])

enter image description here