Exemple #1
0
# This gives us our dataset matrix X that we will use K-Means on.

X = image.reshape(img_shape[0] * img_shape[1], 3)

# Run your K-Means algorithm on this data
# You should try different values of K and max_iters here
K = 16
max_iters = 10

# When using K-Means, it is important the initialize the centroids
# randomly.
# You should complete the code in kMeansInitCentroids.py before proceeding
initial_centroids = kmic.kmeans_init_centroids(X, K)

# Run K-Means
centroids, idx = km.run_kmeans(X, initial_centroids, max_iters, False)
# plt.show()
print('K-Means Done.')

input('Program paused. Press ENTER to continue')

# ===================== Part 5: Image Compression =====================
# In this part of the exercise, you will use the clusters of K-Means to
# compress an image. To do this, we first find the closest clusters for
# each example.
print('Applying K-Means to compress an image.')

# Find closest cluster members
idx = fc.find_closest_centroids(X, centroids)

# Essentially, now we have represented the image X as in terms of the
print(' [ 7.119387 3.616684 ]]')

input('Program paused. Press ENTER to continue')
'''第3部分 运行k-means聚类算法'''
print('Running K-Means Clustering on example dataset.')

#加载数据集
data = scio.loadmat('ex7data2.mat')
X = data['X']

K = 3  #聚类中心数量
max_iters = 10  #设置外循环迭代次数

initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])  #初始化聚类中心

centroids, idx = km.run_kmeans(X, initial_centroids, max_iters,
                               True)  #运行k-means算法 返回最终聚类中心位置即每个样本点所属的聚类中心
#并把中间过程以及最终聚类效果可视化
print('K-Means Done.')

input('Program paused. Press ENTER to continue')
'''第4部分 运行k-means聚类算法 压缩图片'''
print('Running K-Means clustering on pixels from an image')

#加载图片
image = io.imread('bird_small.png')
image = img_as_float(image)

# 图片大小
img_shape = image.shape

X = image.reshape(img_shape[0] * img_shape[1],
Exemple #3
0
# =================== Part 3: K-Means Clustering ======================
print('\nRunning K-Means clustering on example dataset.\n\n')

data = sio.loadmat('ex7data2.mat')
X = data['X']

# Setting for running K-Means
K = 3
max_iters = 10

# here we set centroids to specific values
initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])

# Run K-Means algorithm. The 'true' at the end tells our function to plot
# the progress of K-Means
centroids, idx = run_kmeans(X, initial_centroids, max_iters, True)
print('\nK-Means Done.\n\n')

input('Program paused. Press enter to continue.\n')

# ============= Part 4: K-Means Clustering on Pixels ===============
print('\nRunning K-Means clustering on pixels from an image.\n\n')

# Load an image of a bird
A = imread('bird_small.png')

# Divide by 255 so that all values are in the range 0 - 1
A = A/255.0
img_size = A.shape

X = np.reshape(A, (img_size[0]*img_size[1], 3))
Exemple #4
0
disp.display_data(X_rec[0:100])
plt.title('Recovered faces')
plt.axis('equal')

input('Program paused. Press ENTER to continue')
'''第8部分 利用PCA可视化高维数据'''
image = io.imread('bird_small.png')  #读取图片
image = img_as_float(image)

img_shape = image.shape

X = image.reshape((img_shape[0] * img_shape[1], 3))  #将图片格式转换为包含3列(3个颜色通道)的矩阵
K = 16  #聚类中心数量
max_iters = 10  #外循环迭代次数
initial_centroids = kmic.kmeans_init_centroids(X, K)  #初始化K个聚类中心
centroids, idx = km.run_kmeans(X, initial_centroids, max_iters,
                               False)  #执行k-means,得到最终的聚类中心和每个样本点所属的聚类中心序号

selected = np.random.randint(X.shape[0],
                             size=1000)  #随机选择1000(可以更改)个样本点 每个样本点3维

#可视化3维数据  不同颜色表示每个样本点的所属的簇
cm = plt.cm.get_cmap('RdYlBu')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[selected, 0],
           X[selected, 1],
           X[selected, 2],
           c=idx[selected],
           cmap=cm,
           s=15,
           vmin=0,