Clustering Help¶
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
tof, theta = np.loadtxt('tof.txt')
plt.plot(tof, theta, 'o')
plt.xlabel(r'Absolute ToF/$\mu s$')
plt.ylabel(r'$\theta$/rad')
plt.show()
X = np.array([tof, theta]).T
k = KMeans(n_clusters=19).fit(X)
labels = k.predict(X)
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.xlabel(r'Absolute ToF/$\mu s$')
plt.ylabel(r'$\theta$/rad')
plt.show()
L = np.copy(X)
L[:, 0] = X[:, 0] - k.cluster_centers_[k.predict(X)][:, 0] + k.cluster_centers_[:, 0].min()
plt.scatter(L[:, 0], L[:, 1], c=labels)
plt.xlabel(r'Relative ToF/$\mu s$')
plt.ylabel(r'$\theta$/rad')
plt.show()