NOTES
In [1]:
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [2]:
np.random.seed(0)
x1 = np.random.randn(30)
y1 = 4*x1 + 10 + np.random.randn(len(x1))
x2 = x1*10
y2 = (x2-5)**2 + np.random.randn(len(x2))*10
x3_1 = np.random.randn(10)
x3_2 = np.random.randn(10) + 2
x3_3 = np.random.randn(20) + 4
y3_1 = np.random.randn(len(x3_1))
y3_2 = np.random.randn(len(x3_2)) + 3
y3_3 = np.random.randn(len(x3_3)) + 2
x3 = np.concatenate([x3_1, x3_2, x3_3+3])
y3 = np.concatenate([y3_1, y3_2+3, y3_3])
In [3]:
plt.figure(figsize=(12,4))
plt.subplot(1,3,1)
plt.plot(x1,y1,'k.')
plt.subplot(1,3,2)
plt.plot(x2,y2,'k.')
plt.subplot(1,3,3)
plt.plot(x3,y3,'k.')
plt.savefig('./images/no_line.png')
In [4]:
plt.figure(figsize=(4,4))
plt.plot(x3,y3,'k.')
plt.savefig('./images/3clusters_black.png')
In [5]:
x3 = np.concatenate([x3_1, x3_2, x3_3])
y3 = np.concatenate([y3_1, y3_2, y3_3])
In [6]:
plt.figure(figsize=(6,6))
plt.plot(x3_1,y3_1,'r.',ms=12)
plt.plot(x3_2,y3_2,'g.',ms=12)
plt.plot(x3_3,y3_3,'b.',ms=12)
plt.savefig('./images/3clusters_1.png')
In [7]:
xnew = [2, 3]
plt.figure(figsize=(6,6))
plt.plot(x3_1,y3_1,'r.',ms=12)
plt.plot(x3_2,y3_2,'g.',ms=12)
plt.plot(x3_3,y3_3,'b.',ms=12)
plt.plot(xnew[0],xnew[1],'k*',ms=12)
plt.savefig('./images/3clusters_2.png')
In [8]:
from sklearn import neighbors
# Prepare data
X = np.vstack((x3,y3)).T
y = np.hstack((np.ones(len(x3_1))*0, np.ones(len(x3_2))*1, np.ones(len(x3_3))*2))
# Define k
k = 7
# Initialize kNN
clf = neighbors.KNeighborsClassifier(k)
clf.fit(X, y)
# Analyze new data point with kNN
new_class = clf.predict(xnew)
neighbs = clf.kneighbors(xnew)
# Make colormap
from matplotlib.colors import ListedColormap
cmap_bold = ListedColormap(['#FF0000', '#339933', '#0000FF'])
# Plot data
plt.figure(figsize=(6,6))
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
plt.plot(xnew[0],xnew[1],'*',ms=12, color=cmap_bold.colors[int(new_class[0])])
plt.plot(X[neighbs[1],0],X[neighbs[1],1],'ko',ms=12, mfc='none')
plt.savefig('./images/3clusters_3.png')
In [9]:
# Initialize small kNN
k = 3
clf = neighbors.KNeighborsClassifier(k)
clf.fit(X, y)
xnew = [4, 2]
new_class = clf.predict(xnew)
neighbs = clf.kneighbors(xnew)
# Plot data
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.title('k=3')
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
plt.plot(xnew[0],xnew[1],'*',ms=12, color=cmap_bold.colors[int(new_class[0])])
plt.plot(X[neighbs[1],0],X[neighbs[1],1],'ko',ms=12, mfc='none')
# Initialize large kNN
k = 30
clf = neighbors.KNeighborsClassifier(k)
clf.fit(X, y)
xnew = [1,3]
new_class = clf.predict(xnew)
neighbs = clf.kneighbors(xnew)
# Plot data
plt.subplot(1,2,2)
plt.title('k=30')
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
plt.plot(xnew[0],xnew[1],'*',ms=12, color=cmap_bold.colors[int(new_class[0])])
plt.plot(X[neighbs[1],0],X[neighbs[1],1],'ko',ms=12, mfc='none')
plt.savefig('./images/3clusters_changek.png')
In [10]:
from sklearn import neighbors
clf = neighbors.KNeighborsClassifier(7, weights = 'uniform')
clf.fit(X, y)
print(clf.predict([[0,0],[5,2]]))
In [11]:
from sklearn.datasets import make_blobs
plt.figure(figsize=(4,4))
X, y = make_blobs(n_samples=30, n_features=2, centers=3, cluster_std=2, random_state=0)
plt.scatter(X[:, 0], X[:, 1], c='k')
plt.show()
In [12]:
# Generate fake data
X, y = make_blobs(n_samples=100, n_features=2, centers=4, cluster_std=.6, random_state=1)
plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.scatter(X[:, 0], X[:, 1], c='k')
# Fit clusters for various numbers of clusters
from sklearn.cluster import KMeans
K = range(1,10)
KM = [KMeans(n_clusters=k, random_state=0).fit(X) for k in K]
centroids = [km.cluster_centers_ for km in KM] # cluster centroids
# Compute average euclidean distance between each point and its cluster centroid
from scipy.spatial.distance import cdist
D_k = [cdist(X, cent, 'euclidean') for cent in centroids]
cIdx = [np.argmin(D,axis=1) for D in D_k]
dist = [np.min(D,axis=1) for D in D_k]
avgWithinSS = [sum(d)/X.shape[0] for d in dist]
plt.subplot(1,2,2)
kIdx = 3
plt.plot(K, avgWithinSS, 'b.-', ms=10)
plt.plot(K[kIdx], avgWithinSS[kIdx], marker='o', markersize=15, mew=2, mec='r', mfc='None')
plt.xlabel('Number of clusters')
plt.ylabel('Average within-cluster squared error')
plt.savefig('images/elbow.png')
plt.show()
In [13]:
# Generate fake data
X, y = make_blobs(n_samples=100, n_features=2, centers=3, cluster_std=2, random_state=9)
plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.scatter(X[:, 0], X[:, 1], c='k')
# Fit clusters for various numbers of clusters
K = range(1,10)
KM = [KMeans(n_clusters=k, random_state=0).fit(X) for k in K]
centroids = [km.cluster_centers_ for km in KM] # cluster centroids
# Compute average euclidean distance between each point and its cluster centroid
D_k = [cdist(X, cent, 'euclidean') for cent in centroids]
cIdx = [np.argmin(D,axis=1) for D in D_k]
dist = [np.min(D,axis=1) for D in D_k]
avgWithinSS = [sum(d)/X.shape[0] for d in dist]
plt.subplot(1,2,2)
kIdx = 2
plt.plot(K, avgWithinSS, 'b.-', ms=10)
plt.plot(K[kIdx], avgWithinSS[kIdx], marker='o', markersize=15, mew=2, mec='r', mfc='None')
plt.xlabel('Number of clusters')
plt.ylabel('Average within-cluster squared error')
plt.savefig('images/noelbow.png')
plt.show()
In [14]:
X, y = make_blobs(n_samples=30, n_features=2, centers=3, cluster_std=2, random_state=0)
# Predict clusters using 2 different random seeds
from sklearn.cluster import KMeans
y_pred = KMeans(n_clusters=3, n_init=1, random_state=0).fit_predict(X)
y_pred2 = KMeans(n_clusters=3, n_init=1, random_state=1).fit_predict(X)
plt.figure(figsize=(12,4))
plt.subplot(1,3,1)
plt.scatter(X[:, 0], X[:, 1], c='k')
plt.subplot(1,3,2)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.subplot(1,3,3)
plt.scatter(X[:, 0], X[:, 1], c=y_pred2)
plt.show()
In [15]:
X, y = make_blobs(n_samples=30, n_features=2, centers=3, cluster_std=2, random_state=0)
# Predict clusters using 2 different random seeds
y_pred = KMeans(n_clusters=3, n_init=100, random_state=0).fit_predict(X)
y_pred2 = KMeans(n_clusters=3, n_init=100, random_state=1).fit_predict(X)
plt.figure(figsize=(12,4))
plt.subplot(1,3,1)
plt.scatter(X[:, 0], X[:, 1], c='k')
plt.subplot(1,3,2)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.subplot(1,3,3)
plt.scatter(X[:, 0], X[:, 1], c=y_pred2)
plt.show()
In [16]:
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
plt.figure(figsize=(12, 12))
n_samples = 300
random_state = 170
X, y = make_blobs(n_samples=n_samples, random_state=random_state)
# Incorrect number of clusters
y_pred = KMeans(n_clusters=2, random_state=random_state).fit_predict(X)
plt.subplot(221)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.title("Incorrect Number of Blobs")
# Anisotropicly distributed data
transformation = [[ 0.60834549, -0.63667341], [-0.40887718, 0.85253229]]
X_aniso = np.dot(X, transformation)
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_aniso)
plt.subplot(222)
plt.scatter(X_aniso[:, 0], X_aniso[:, 1], c=y_pred)
plt.title("Anisotropicly Distributed Blobs")
# Different variance
X_varied, y_varied = make_blobs(n_samples=n_samples,
cluster_std=[1.0, 2.5, 0.5],
random_state=random_state)
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_varied)
plt.subplot(223)
plt.scatter(X_varied[:, 0], X_varied[:, 1], c=y_pred)
plt.title("Unequal Variance")
# Unevenly sized blobs
X_filtered = np.vstack((X[y == 0][:500], X[y == 1][:100], X[y == 2][:10]))
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_filtered)
plt.subplot(224)
plt.scatter(X_filtered[:, 0], X_filtered[:, 1], c=y_pred)
plt.title("Unevenly Sized Blobs")
plt.savefig('images/limit.png')
plt.show()
In [ ]: