Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
greyhatguy007
GitHub Repository: greyhatguy007/Machine-Learning-Specialization-Coursera
Path: blob/main/C3 - Unsupervised Learning, Recommenders, Reinforcement Learning/week1/C3W1A/C3W1A1/utils.py
3566 views
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
def load_data():
5
X = np.load("data/ex7_X.npy")
6
return X
7
8
def draw_line(p1, p2, style="-k", linewidth=1):
9
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], style, linewidth=linewidth)
10
11
def plot_data_points(X, idx):
12
# plots data points in X, coloring them so that those with the same
13
# index assignments in idx have the same color
14
plt.scatter(X[:, 0], X[:, 1], c=idx)
15
16
def plot_progress_kMeans(X, centroids, previous_centroids, idx, K, i):
17
# Plot the examples
18
plot_data_points(X, idx)
19
20
# Plot the centroids as black 'x's
21
plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', c='k', linewidths=3)
22
23
# Plot history of the centroids with lines
24
for j in range(centroids.shape[0]):
25
draw_line(centroids[j, :], previous_centroids[j, :])
26
27
plt.title("Iteration number %d" %i)
28