Ejemplo n.º 1
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
Ejemplo n.º 2
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
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
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  #返回最终聚类中心的位置  和每个样本所属的聚类中心序号
Ejemplo n.º 5
0
# part, you should complete the code in the findClosestCentroids.py
#

print('Finding closest centroids.')

# Load an example dataset that we will be using
data = scio.loadmat('ex7data2.mat')
X = data['X']

# Select an initial set of centroids
k = 3  # Three centroids
initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])

# Find the closest centroids for the examples using the
# initial_centroids
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')

# ===================== 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.
Ejemplo n.º 6
0

if __name__ == '__main__':
    # plt.ioff()
    # ================= Part 1: Find Closest Centroids ====================
    print('Finding closest centroids.\n\n')
    # Load an example dataset that we will be using
    data = load_mat_file('./data/ex7data2.mat')
    # Visualize the example dataset
    X = data['X']
    # Select an initial set of centroids
    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')
  To help implement K-Means, we have divided the learning algorithm 
 into two functions -- findClosestCentroids and computeCentroids.
"""

# Load an example dataset that we will be using
mat_contents = sio.loadmat('ex7data2.mat')
X = mat_contents['X']

# Select an initial set of centroids
K = 3  # 3 Centroids
initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])

# Find the closest centroids for the examples using the
# initial_centroids
print('Finding closest centroids.\n\n')
idx = find_closest_centroids(X, initial_centroids)

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')