83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
from itertools import cycle, islice
|
|
from sklearn import cluster
|
|
|
|
figsize = (10, 10)
|
|
point_size = 150
|
|
point_border = 0.8
|
|
|
|
|
|
def plot_dataset(dataset, xlim=(-15, 15), ylim=(-15, 15)):
|
|
plt.figure(figsize=figsize)
|
|
plt.scatter(dataset[:, 0], dataset[:, 1], s=point_size,
|
|
color="#00B3E9", edgecolor='black', lw=point_border)
|
|
plt.xlim(xlim)
|
|
plt.ylim(ylim)
|
|
plt.show()
|
|
|
|
|
|
def plot_clustered_dataset(dataset, y_pred, xlim=(-15, 15), ylim=(-15, 15), neighborhood=False, epsilon=0.5):
|
|
|
|
fig, ax = plt.subplots(figsize=figsize)
|
|
|
|
colors = np.array(list(islice(cycle(['#df8efd', '#78c465', '#ff8e34',
|
|
'#f65e97', '#a65628', '#984ea3',
|
|
'#999999', '#e41a1c', '#dede00']),
|
|
int(max(y_pred) + 1))))
|
|
colors = np.append(colors, '#BECBD6')
|
|
|
|
if neighborhood:
|
|
for point in dataset:
|
|
circle1 = plt.Circle(
|
|
point, epsilon, color='#666666', fill=False, zorder=0, alpha=0.3)
|
|
ax.add_artist(circle1)
|
|
|
|
ax.scatter(dataset[:, 0], dataset[:, 1], s=point_size,
|
|
color=colors[y_pred], zorder=10, edgecolor='black', lw=point_border)
|
|
plt.xlim(xlim)
|
|
plt.ylim(ylim)
|
|
plt.show()
|
|
|
|
|
|
def plot_dbscan_grid(dataset, eps_values, min_samples_values):
|
|
|
|
fig = plt.figure(figsize=(16, 20))
|
|
plt.subplots_adjust(left=.02, right=.98, bottom=0.001, top=.96, wspace=.05,
|
|
hspace=0.25)
|
|
|
|
plot_num = 1
|
|
|
|
for i, min_samples in enumerate(min_samples_values):
|
|
for j, eps in enumerate(eps_values):
|
|
ax = fig.add_subplot(len(min_samples_values),
|
|
len(eps_values), plot_num)
|
|
|
|
dbscan = cluster.DBSCAN(eps=eps, min_samples=min_samples)
|
|
y_pred_2 = dbscan.fit_predict(dataset)
|
|
|
|
colors = np.array(list(islice(cycle(['#df8efd', '#78c465', '#ff8e34',
|
|
'#f65e97', '#a65628', '#984ea3',
|
|
'#999999', '#e41a1c', '#dede00']),
|
|
int(max(y_pred_2) + 1))))
|
|
colors = np.append(colors, '#BECBD6')
|
|
|
|
for point in dataset:
|
|
circle1 = plt.Circle(
|
|
point, eps, color='#666666', fill=False, zorder=0, alpha=0.3)
|
|
ax.add_artist(circle1)
|
|
|
|
ax.text(0, -0.03, 'Epsilon: {} \nMin_samples: {}'.format(eps,
|
|
min_samples), transform=ax.transAxes, fontsize=16, va='top')
|
|
ax.scatter(dataset[:, 0], dataset[:, 1], s=50,
|
|
color=colors[y_pred_2], zorder=10, edgecolor='black', lw=0.5)
|
|
|
|
plt.xticks(())
|
|
plt.yticks(())
|
|
plt.xlim(-14, 5)
|
|
plt.ylim(-12, 7)
|
|
|
|
plot_num = plot_num + 1
|
|
|
|
plt.show()
|