Wunda machinelearning Week8 Knowledge Point Overview
1. K-means algorithm
K-means algorithm is used to solve clustering problems, which belongs to unsupervised learning. Data that is not tagged can be processed and divided into K classes. The steps are as follows:
- Randomly selects K as the starting mean point from a dataset
- Calculates the distance from each point to each mean point and classifies the points into the nearest point class. (assuming the shortest distance from X to M (M <= K), X is classified as Class m)
- Calculates the average of each class (that is, dividing the number of x divided by the class by the X in the class) to use the new coordinate as the new mean point
- Repeat from 2 until convergence
- Repeat starting from 1, and finally take the convergence point to the smallest group of the various points
2. PLA (Principal Component analysis)
It is used to reduce the data dimension and speed up the processing and calculation of the model. The steps are as follows:
1. Calculate the parameter sigma:
Where X is the matrix of the input data.
2. Bring the parameters into the formula:
The resulting U is a matrix of n * N, S is a diagonal matrix (except the main diagonal data is all 0)
Assuming we're going down to K-dimensional, take the first k column of the Matrix U, get U_reduce (n * K) and multiply X and u_reduce to get a new matrix Z, which is the descending matrix, to replace the X
3. By multiplying Z and u_reduce, we can get the matrix X_approx, we have the following formula
The smaller the value, the smaller the effect of dimensionality on the original data, which is typically between 0.01 and 0.1. The S-matrix makes it easy to calculate this value. Formula:
1-
K is the K dimension to be taken. We're going to find a K value that makes the value less than a certain value.
After-school exercises code PCA.M
function [u, s] = PCA (x)%PCA Run principal component analysis on the DataSet x [U, s, x] = PCA (x) computes Eigenvecto RS of covariance matrix of x Returns the eigenvectors U, the eigenvalues (on diagonal) in s%% useful Values[m, N] = Size (X);% you need to return the following variables correctly. U = zeros (n); S = zeros (n);% ====================== YOUR CODE here ======================% instructions:you should first compute the CO Variance matrix. Then, you% should use the "SVD" function to compute the eigenvectors% and eigenvalues of the covariance matrix.%s Igma = (x ' * x)/m; [U, S, V] = SVD (Sigma);% =========================================================================end
projectdata.m
function Z = Projectdata (X, U, K)%projectdata computes the reduced data representation when projecting is%on to the top K eigenvectors% Z = Projectdata (X, U, K) computes the projection of the normalized inputs X into the reduced di Mensional space spanned by% the first K columns of U. It returns the projected examples in z.%% you need to return the following variables correctly. Z = zeros (Size (X, 1), K);% ====================== YOUR CODE here ======================% instructions:compute the project Ion of the data using only the top K- eigenvectors in U (first K-columns).% for the i-th example X (i,:), the Pro Jection on to the k-th% eigenvector is given as follows:% x = x (i,:) ';% projection_k = X ' * U (:, k);%Z = x * U (:, 1:k);% =============================================================end
recoverdata.m
function X_rec = Recoverdata (Z, U, K)%recoverdata recovers an approximation of the original data when using the%projected data% X_rec = Recoverdata (Z, U, K) recovers an approximation the percent original data that have been reduced to K dimension S. It returns the% approximate reconstruction in x_rec.%% you need to return the following variables correctly. X_rec = zeros (Size (Z, 1), Size (U, 1));% ====================== YOUR CODE here ======================% instructions:comput E The approximation of the data by projecting back% onto, the original space using the top K eigenvectors in u.%% for the i-th example Z (i,:), the (approximate)% recovered data for Dimension J is given a s follows:% v = Z (i,:) ';% Recovered_j = V ' * U (J, 1:k) '; Percent Notice tha T U (j, 1:k) is a row vector.% X_rec = Z * U (:, 1:k) ';% ==================================================== =========end
Findclosestcentroids.m
function idx = Findclosestcentroids (X, centroids)%findclosestcentroids computes the centroid memberships for every Exampl e% idx = Findclosestcentroids (X, centroids) returns the closest centroids% in idx for a dataset X where each row is a Single example. IDX = m x 1 vector of centroid assignments (i.e. each entry in range [1..K]) percent Set KK = size (centroids, 1);% you need To return the following variables Correctly.idx = zeros (Size (x,1), 1);% ====================== YOUR CODE here ============ ==========% Instructions:go over every example, find its closest centroid, and store% the index inside IDX At the appropriate location.% concretely, IDX (i) should contain the index of the centroid% CLO Sest to Example I. Hence, it should is a value in the range 1..k%% note:you can use a for-loop over the E Xamples to compute this.%m = Size (X, 1); For i = 1:m x = x (i,:); min = SUM ((x-centroids (1,:)). ^ 2); IDX (i) = 1; For j = 2:k Sumnum = SUM ((X-centroids (J,:)). ^ 2); If sumnum < min min = sumnum; IDX (i) = j; End endend% =============================================================end
Computecentroids.m
function centroids = Computecentroids (X, idx, K)%computecentroids returns the new centroids by computing the means of the %data points assigned to all centroid.% centroids = Computecentroids (X, IDX, K) returns the new centroids by% Comput ing the means of the data points assigned to each centroid. It is% given a dataset X where each row was a single data point, a vector% idx of centroid assignments (i.e. each entry In range [1..K]) for each% example, and K, the number of centroids. You should return a matrix% centroids, where each row of centroids are the mean of the data points% assigned to it.%% U Seful variables[m N] = size (X);% you need to return the following variables correctly.centroids = zeros (K, n);% ========== ============ YOUR CODE here ======================% instructions:go over every centroid and compute mean of all points th at% belong to it. Concretely, the row vector centroids (i,:)% should contain the mean of the data points Assigned to% centroid i.%% note:you can use a for-loop over the centroids to compute this.%cnt = zeros (K, n); f or i = 1:m centroids (idx (i),:) = centroids (idx (i),:) + X (i,:) cnt (IDX (i),:) = CNT (idx (i),:) + 1;endcentr OIDs = centroids./cnt;% =============================================================end
Wunda machinelearning Week8