from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import DBSCAN # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model dbscan_model = DBSCAN(eps=0.25, min_samples=9) # train the model dbscan_model.fit(training_data) # assign each data point to a cluster dbscan_result = dbscan_model.predict(training_data) # get all of the unique clusters dbscan_cluster = unique(dbscan_result) # plot the DBSCAN clusters for dbscan_cluster in dbscan_clusters: # get data points that fall in this cluster index = where(dbscan_result == dbscan_clusters) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the DBSCAN plot pyplot.show()