コード例 #1
0
def run_kmeans(X, initial_centroids, max_iters, plot):
    if plot:
        plt.figure()

    # Initialize values
    (m, n) = X.shape
    K = initial_centroids.shape[0]
    centroids = initial_centroids
    previous_centroids = centroids
    idx = np.zeros(m)

    # Run K-Means
    for i in range(max_iters):
        # Output progress
        print('K-Means iteration {}/{}'.format((i + 1), max_iters))

        # For each example in X, assign it to the closest centroid
        idx = fc.find_closest_centroids(X, centroids)

        # Optionally plot progress
        if plot:
            plot_progress(X, centroids, previous_centroids, idx, K, i)
            previous_centroids = centroids
            input('Press ENTER to continue')

        # Given the memberships, compute new centroids
        centroids = cc.compute_centroids(X, idx, K)

    return centroids, idx
コード例 #2
0
def run_k_means(x, initial_centroids, max_iters, plot_progress=False):
    if plot_progress:
        plt.ion()
        plt.figure()
    centroids = initial_centroids
    previous_centroids = centroids
    k = initial_centroids.shape[0]

    idx = 0

    for i in range(max_iters):
        # Output progress
        print('K-Means iteration {}/{}...\n'.format(i + 1, max_iters),
              flush=True)
        # For each example in X, assign it to the closest centroid
        idx = find_closest_centroids(x, centroids)
        # Optionally, plot progress here
        if plot_progress:
            plot_progressk_means(x, centroids, previous_centroids, idx, k, i)
            previous_centroids = centroids

        # Given the memberships, compute new centroids
        centroids = compute_centroids(x, idx, k)
    if plot_progress:
        plt.close()
    return centroids, idx
def run_kmeans(X, initial_centroids, max_iters, plot_progress=False):
    """[centroids, idx] = RUNKMEANS(X, initial_centroids, max_iters, ...
       plot_progress) runs the K-Means algorithm on data matrix X, where each
       row of X is a single example.
        It uses initial_centroids used as the
       initial centroids. max_iters specifies the total number of interactions
       of K-Means to execute. plot_progress is a true/false flag that
       indicates if the function should also plot its progress as the
       learning happens. This is set to false by default. runkMeans returns
       centroids, a Kxn matrix of the computed centroids and idx, a m x 1
       vector of centroid assignments (i.e. each entry in range [1..K])
    """

    # Initialize values
    m, n = X.shape
    K = initial_centroids.shape[0]
    centroids = initial_centroids
    previous_centroids = centroids
    idx = np.zeros((m, 1))

    # Run K-Means
    for i in range(max_iters):

        # Output progress
        print('K-Means iteration {}/{}...'.format(i + 1, max_iters))

        # For each example in X, assign it to the closest centroid
        idx = find_closest_centroids(X, centroids)

        # Optionally, plot progress here
        if plot_progress:
            plot_progress_kmeans(X, centroids, previous_centroids, idx, K, i)
            previous_centroids = centroids
            print('Press enter to continue.\n')
            pause()

        # Given the memberships, compute new centroids
        centroids = compute_centroids(X, idx, K)

    return centroids, idx
コード例 #4
0
def run_kmeans(X, initial_centroids, max_iters, plot): #plot设置是否进行可视化 
    if plot:
        plt.figure()

    (m, n) = X.shape #m样本数  n样本特征数
    K = initial_centroids.shape[0]  #聚类中心数量
    centroids = initial_centroids
    previous_centroids = centroids
    idx = np.zeros(m)  #存放每个样本所属的聚类中心序号

    # 运行k-means
    for i in range(max_iters):  #外循环
        print('K-Means iteration {}/{}'.format((i + 1), max_iters))  

        idx = fc.find_closest_centroids(X, centroids) #第一个内循环 为每个样本找到最近的聚类中心
        
        if plot:
            plot_progress(X, centroids, previous_centroids, idx, K, i) #画出此时簇分配的状态
            previous_centroids = centroids
            input('Press ENTER to continue')

        centroids = cc.compute_centroids(X, idx, K) #第2个内循环  更新聚类中心

    return centroids, idx  #返回最终聚类中心的位置  和每个样本所属的聚类中心序号
コード例 #5
0
print('Closest centroids for the first 3 examples: ')
print('{}'.format(idx[0:3]))
print('(the closest centroids should be 0, 2, 1 respectively)')

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

# ===================== Part 2: Compute Means =====================
# After implementing the closest centroids function, you should now
# complete the compute_centroids function.
#

print('Computing centroids means.')

# Compute means based on the closest centroids found in the previous part.
centroids = cc.compute_centroids(X, idx, k)

print('Centroids computed after initial finding of closest centroids: \n{}'.
      format(centroids))
print('the centroids should be')
print('[[ 2.428301 3.157924 ]')
print(' [ 5.813503 2.633656 ]')
print(' [ 7.119387 3.616684 ]]')

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

# ===================== Part 3: K-Means Clustering =====================
# After you have completed the two functions compute_centroids and
# find_closest_centroids, you will have all the necessary pieces to run the
# kMeans algorithm. In this part, you will run the K-Means algorithm on
# the example dataset we have provided.
コード例 #6
0
    K = 3  # 3 Centroids
    initial_centroids = [[3, 3], [6, 2], [8, 5]]
    initial_centroids = np.array(initial_centroids)
    # Find the closest centroids for the examples using the initial_centroids
    idx = find_closest_centroids(X, initial_centroids)
    print('Closest centroids for the first 3 examples: \n {}'.format(idx[0:3]))
    print('\n(the closest centroids should be 1, 3, 2 respectively)\n')
    print('Program paused. Press enter to continue.\n')
    # pause_func()

    # ===================== Part 2: Compute Means =========================
    # After implementing the closest centroids function, you should now
    # complete the computeCentroids function.
    print('\nComputing centroids means.\n\n')
    #  Compute means based on the closest centroids found in the previous part.
    centroids = compute_centroids(X, idx, K)
    print('Centroids computed after initial finding of closest centroids: \n')
    print(' %s \n' % centroids)
    print('\n(the centroids should be\n')
    print('   [ 2.428301 3.157924 ]\n')
    print('   [ 5.813503 2.633656 ]\n')
    print('   [ 7.119387 3.616684 ]\n\n')
    print('Program paused. Press enter to continue.\n')
    # pause_func()

    # =================== Part 3: K-Means Clustering ======================
    # After you have completed the two functions computeCentroids and
    # findClosestCentroids, you have all the necessary pieces to run the
    # kMeans algorithm. In this part, you will run the K-Means algorithm on
    # the example dataset we have provided.
コード例 #7
0
k = 3  # 随机初始化3个聚类中心
initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])

#找到离每个样本最近的初始聚类中心序号
idx = fc.find_closest_centroids(X, initial_centroids)

print('Closest centroids for the first 3 examples: ')
print('{}'.format(idx[0:3]))
print('(the closest centroids should be 0, 2, 1 respectively)')

input('Program paused. Press ENTER to continue')
'''第2部分 更新聚类中心'''

print('Computing centroids means.')

centroids = cc.compute_centroids(X, idx, k)  #在簇分配结束后 对每个簇的样本点重新计算聚类中心

print('Centroids computed after initial finding of closest centroids: \n{}'.
      format(centroids))
print('the centroids should be')
print('[[ 2.428301 3.157924 ]')
print(' [ 5.813503 2.633656 ]')
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']
コード例 #8
0
print('Closest centroids for the first 3 examples: \n')
print(idx[0:3])
print('\n(the closest centroids should be 0, 2, 1 respectively)\n')

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

"""
## Part 2: Compute Means 
  After implementing the closest centroids function, we compute centroids.
"""

print('\nComputing centroids means.\n\n')

#  Compute means based on the closest centroids found in the previous part.
centroids = compute_centroids(X, idx, K)

print('Centroids computed after initial finding of closest centroids: \n')
print(centroids)

print("centroids shape", centroids.shape)

print('\n(the centroids should be\n')
print('   [ 2.428301 3.157924 ]\n')
print('   [ 5.813503 2.633656 ]\n')
print('   [ 7.119387 3.616684 ]\n\n')

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

"""