Created
May 11, 2018 20:18
-
-
Save vihar/48739d02f0342159b74b03bd13d2410c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing Modules | |
from sklearn.datasets import load_iris | |
import matplotlib.pyplot as plt | |
from sklearn.cluster import DBSCAN | |
from sklearn.decomposition import PCA | |
# Load Dataset | |
iris = load_iris() | |
# Declaring Model | |
dbscan = DBSCAN() | |
# Fitting | |
dbscan.fit(iris.data) | |
# Transoring Using PCA | |
pca = PCA(n_components=2).fit(iris.data) | |
pca_2d = pca.transform(iris.data) | |
# Plot based on Class | |
for i in range(0, pca_2d.shape[0]): | |
if dbscan.labels_[i] == 0: | |
c1 = plt.scatter(pca_2d[i, 0], pca_2d[i, 1], c='r', marker='+') | |
elif dbscan.labels_[i] == 1: | |
c2 = plt.scatter(pca_2d[i, 0], pca_2d[i, 1], c='g', marker='o') | |
elif dbscan.labels_[i] == -1: | |
c3 = plt.scatter(pca_2d[i, 0], pca_2d[i, 1], c='b', marker='*') | |
plt.legend([c1, c2, c3], ['Cluster 1', 'Cluster 2', 'Noise']) | |
plt.title('DBSCAN finds 2 clusters and Noise') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used another dataset and have tried to run the plot based on class but the program said that c1 is not defined. When I used iris dataset, it works fine. Any reason?