K có nghĩa là trăn hình ảnh 3d

trên cùng bên phải. Ảnh hưởng của việc khởi tạo xấu là gì đối với quá trình phân loại. Bằng cách đặt n_init thành chỉ 1 [mặc định là 10], số lần thuật toán sẽ được chạy với các centroid seed khác nhau sẽ giảm đi

Trong một vài phần trước, chúng ta đã khám phá một loại mô hình học máy không giám sát. giảm kích thước. Ở đây chúng ta sẽ chuyển sang một lớp mô hình học máy không giám sát khác. thuật toán phân cụm. Các thuật toán phân cụm tìm cách học, từ các thuộc tính của dữ liệu, phân chia tối ưu hoặc ghi nhãn rời rạc của các nhóm điểm

Nhiều thuật toán phân cụm có sẵn trong Scikit-Learn và các nơi khác, nhưng có lẽ cách hiểu đơn giản nhất là thuật toán được gọi là phân cụm k-mean, được triển khai trong

plt.scatter[X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis']

centers = kmeans.cluster_centers_
plt.scatter[centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5];
0

Chúng tôi bắt đầu với việc nhập khẩu tiêu chuẩn

Trong 1]

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set[]  # for plot styling
import numpy as np

Giới thiệu k-Means

Thuật toán k-means tìm kiếm một số cụm được xác định trước trong tập dữ liệu đa chiều không được gắn nhãn. Nó thực hiện điều này bằng cách sử dụng một khái niệm đơn giản về phân cụm tối ưu trông như thế nào

  • "Tâm cụm" là trung bình cộng của tất cả các điểm thuộc cụm
  • Mỗi điểm gần trung tâm cụm của chính nó hơn so với các trung tâm cụm khác

Hai giả định đó là cơ sở của mô hình k-means. Chúng tôi sẽ sớm đi sâu vào chính xác cách thuật toán đạt được giải pháp này, nhưng bây giờ chúng ta hãy xem một bộ dữ liệu đơn giản và xem kết quả k-means

Đầu tiên, hãy tạo tập dữ liệu hai chiều chứa bốn đốm màu riêng biệt. Để nhấn mạnh rằng đây là một thuật toán không được giám sát, chúng tôi sẽ loại bỏ các nhãn khỏi trực quan hóa

Trong 2]

from sklearn.datasets.samples_generator import make_blobs
X, y_true = make_blobs[n_samples=300, centers=4,
                       cluster_std=0.60, random_state=0]
plt.scatter[X[:, 0], X[:, 1], s=50];

Thuật toán k-Means đủ đơn giản để chúng ta có thể viết nó trong một vài dòng mã. Sau đây là một thực hiện rất cơ bản

K-Means là một thuật toán phân cụm không giám sát, tương tự như các thuật toán phân loại được giám sát. Do tên, thuật toán K-Means thường bị nhầm lẫn với thuật toán KNN[K Nearest Neighbhours] được giám sát, được sử dụng cho cả bài toán phân loại và hồi quy

Như tên cho thấy, thuật toán K-Means bao gồm "K" "Phương tiện" tương ứng với số lượng cụm mà thuật toán cố gắng tìm trong dữ liệu chưa được gắn nhãn. Hoạt động của thuật toán mặc dù khá đơn giản nhưng thách thức nằm ở việc mở rộng thuật toán cho các tập dữ liệu lớn và chọn một thước đo thích hợp cho khoảng cách

Trước khi chúng tôi đi vào bất kỳ chi tiết nào khác, chúng ta hãy xem các bước bao gồm thuật toán K-Means-

  1. Đầu vào cho thuật toán-
    1. Dữ liệu đến cụm
    2. Số lượng cụm để xác định
    3. tiêu chí hội tụ i. e. khi nào dừng lại
      1. Thông thường, giá trị dung sai được đưa ra được sử dụng để quan sát khi vị trí "Trung bình" không thay đổi nữa
      2. Tùy chọn khác là số lần lặp lại tối đa để thực hiện
    4. Chức năng được sử dụng làm Thước đo - thường sử dụng khoảng cách cartesian nhưng khi dữ liệu là văn bản hoặc bất kỳ dữ liệu trừu tượng nào khác, phải chọn các thước đo đặc biệt
  2. Dựa trên không gian dữ liệu tôi. e. giới hạn [tối thiểu và tối đa] và số lượng cụm cần xác định, nhiều điểm Trung bình ngẫu nhiên trong cùng một không gian khi dữ liệu được tạo
  3. Khoảng cách của tất cả các mẫu/điểm dữ liệu trong không gian dữ liệu, giả sử $\mathbb{R}^n$, tương ứng với K số Phương tiện được tính toán [i. e. nếu có 10 mẫu và 2 cụm cần tìm thì số khoảng cách đo được là 20[1x10 + 1x10]]
  4. Dựa trên khoảng cách mà mỗi mẫu dữ liệu được liên kết với Giá trị trung bình gần nhất của chúng
  5. Dựa trên các liên kết được thực hiện ở bước 4, các giá trị Trung bình được cập nhật bằng cách tính giá trị trung bình của tất cả các giá trị được liên kết với một Giá trị trung bình cụ thể. Điều này được thực hiện cho K số Phương tiện
  6. Các bước từ 3 đến 5 sau đó được lặp lại cho đến khi thuật toán vượt quá số lần lặp tối đa được phép hoặc cho đến khi các giá trị của K Mean ổn định. e. thay đổi sau khi cập nhật nhỏ hơn giá trị dung sai được chỉ định trong đầu vào

Tiếp theo, chúng ta chuyển sang phần triển khai và viết mã của thuật toán và thảo luận về kết quả

mã bắt đầu

Xử lý nhập khẩu

%matplotlib widget

import time
import IPython
import numpy as np
import pandas as pd
import numpy.linalg as LA
import matplotlib.pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D

from IPython.display import Video

plt.rcParams['figure.figsize'] = [8.0, 8.0]
plt.rcParams['figure.dpi'] = 100

Lớp K-Means để giữ dữ liệu, phương pháp tính toán khoảng cách, cập nhật phương tiện và vẽ biểu đồ tiến trình và kết quả

class kmeans:
    def __init__[self, dimensions, sample_size, clusters, tolerance, max_iters]:
        """
        Use the initialisation parameters as attributes of the object and create
        a matplotlib figure canvas on which each iteration progress can be drawn
        :param dimesnions: Dimension of data
        :param sample_size: Not necessary for real data, here used for generating sample data
        :param clusters: Number of clusters to identify in the data, control the number of Means 
        :param tolerance: The tolearance value
        :param max_iters: Maximum iterations to execute
        """
        self.dimensions = dimensions
        self.sample_size = sample_size
        self.clusters = clusters
        self.tolerance = tolerance
        self.max_iters = max_iters
        self.colors = ['y', 'r', 'b', 'm', 'k', 'c', 'b', 'm', 'k', 'c']
        
        if self.dimensions == 1:
            self.fig, self.ax = plt.subplots[1,1]
            self.sample_pts = np.array[[[]]]
            self.ax.grid[True]
        elif self.dimensions == 2:
            self.fig, self.ax = plt.subplots[1,1]
            self.sample_pts = np.array[[[], []]]
            self.ax.grid[True]
        elif self.dimensions == 3:
            self.fig = plt.figure[1, figsize=[8, 6]]
            self.sample_pts = np.array[[[], [], []]]
            self.ax = Axes3D[self.fig, rect=[0.0, 0.0, .95, 1.0], elev=48, azim=134]
    
    def kmeans_init[self]:
        """
        Generate sample data and draw the initial state of the data and display the initial position
        of the Means
        """
        ##################################################################################################################################
        # Creating clusters using normal distribution and random variance and mean
        # every cluster will have equal number of points
        ##################################################################################################################################
        for i in range[0, self.clusters]:
            np.random.seed[int[[-i] ** 2]]
            tmp = np.random.randn[1, [self.sample_size // self.clusters] * self.dimensions] * np.random.randint[1, 10] + np.random.randint[-100, 100]
            self.sample_pts = np.hstack[[self.sample_pts, tmp.reshape[self.dimensions, self.sample_size // self.clusters]]]
        np.random.seed[22]
        self.previous_means = np.random.randn[self.clusters, self.dimensions] * np.random.randint[1, 12]  # Randomly selected means i.e., cluster centers
        # print[f'Starting means are: {self.previous_means}']
        self.new_means = np.zeros[[self.clusters, self.dimensions]]  # To store the new means after every iteration
    
        ##################################################################################################################################
        # plot initial means and all data samples to see the distribution
        ##################################################################################################################################
        if self.dimensions == 1:
            self.ax.scatter[self.previous_means[:, 0], np.zeros[[self.clusters, 1]], marker='o', c='r', label='Initial Means']
            self.ax.scatter[self.sample_pts[0, :], np.zeros[[1, self.sample_size]],
                        marker='*']  # Plotting all the points to see the clusters
        elif self.dimensions == 2:
            self.ax.scatter[self.previous_means[:, 0], self.previous_means[:, 1], marker='o', c='r', label='Initial Means']
            self.ax.scatter[self.sample_pts[0, :], self.sample_pts[1, :], marker='*']  # Plotting all the points to see the clusters
        elif self.dimensions == 3:
            self.ax.scatter[self.previous_means[:, 0], self.previous_means[:, 1], self.previous_means[:, 2], marker='o', c='r',
                       label='Initial Means', depthshade=False]
            self.ax.scatter[self.sample_pts[0, :], self.sample_pts[1, :], self.sample_pts[2, :],
                       marker='*']  # Plotting all the points to see the clusters
        self.ax.legend[loc='upper right']
    
    ##################################################################################################################################
    # Loop till convergence
    ##################################################################################################################################
    def kmeans_iter[self, iteration_count]:
        """
        Iteration part of the algorithm which iterates until the tolerance criteria is met while
        limiting the maximum number of iterations to preven infinite loops when the algorithm 
        cannot associate a Mean value with a cluster
        """
        if [abs[self.previous_means - self.new_means] > self.tolerance].any[] and [iteration_count  1:
                    if len[indices.index] != 0:
                        self.new_means[i, :] = np.mean[self.sample_pts[:, indices.index], axis=1]
                    else:
                        # Re-initialise a mean if it is not associated with any data sample
                        self.new_means[i, :] = np.random.randn[1, self.dimensions] * 100
                else:
                    if len[indices.index] != 0:
                        self.new_means[i, 0] = np.mean[self.sample_pts[0, indices.index]]
                    else:
                        # Re-initialise a mean if it is not associated with any data sample
                        self.new_means[i, 0] = np.random.randn[1, self.dimensions] * 100
            # print[f'New means are:{self.new_means}']

            ##################################################################################################################################
            # Plot the movement of the means
            ##################################################################################################################################
            if self.dimensions == 1:
                for i in range[0, self.clusters]:
                    self.ax.plot[[self.previous_means[i, 0], self.new_means[i, 0]],
                                 [0, 0], label='mean movement' if iteration_count == 1 else "", c=self.colors[i]]
                    self.ax.scatter[self.new_means[i, 0], 0, marker='o', c='g', label='new Means' if i == 0 and iteration_count == 1 else ""]
            elif self.dimensions == 2:
                for i in range[0, self.clusters]:
                    self.ax.plot[[self.previous_means[i, 0], self.new_means[i, 0]],
                                 [self.previous_means[i, 1], self.new_means[i, 1]],
                                 label='mean movement' if iteration_count == 1 else "", c=self.colors[i]]
                    self.ax.scatter[self.new_means[i, 0], self.new_means[i, 1], marker='o', c='g',
                                label='new Means' if i == 0 and iteration_count == 1 else ""]
            elif self.dimensions == 3:
                for i in range[0, self.clusters]:
                    self.ax.plot[[self.previous_means[i, 0], self.new_means[i, 0]],
                                 [self.previous_means[i, 1], self.new_means[i, 1]],
                                 [self.previous_means[i, 2], self.new_means[i, 2]],
                                 label='mean movement' if iteration_count == 1 else "", c=self.colors[i]]
                    self.ax.scatter[self.new_means[i, 0], self.new_means[i, 1], self.new_means[i, 2], marker='o', c='g',
                                   label='new Means' if i == 0 and iteration_count == 1 else ""]
            self.ax.legend[loc='upper right']
            # iteration_count += 1
            # self.fig.canvas.draw[]
            
            ##################################################################################################################################
            # Plot the clustering results upon convergence
            ##################################################################################################################################
            if [abs[self.previous_means - self.new_means] 

Chủ Đề