机器学习week8 ex7 review
機(jī)器學(xué)習(xí)week8 ex7 review
這周學(xué)習(xí)K-means,并將其運(yùn)用于圖片壓縮。
1 K-means clustering
先從二維的點(diǎn)開(kāi)始,使用K-means進(jìn)行分類。
1.1 Implement K-means
K-means步驟如上,在每次循環(huán)中,先對(duì)所有點(diǎn)更新分類,再更新每一類的中心坐標(biāo)。
1.1.1 Finding closest centroids
對(duì)每個(gè)example,根據(jù)公式:
找到距離它最近的centroid,并標(biāo)記。若有數(shù)個(gè)距離相同且均為最近,任取一個(gè)即可。
代碼如下:
1.1.2 Compute centroid means
對(duì)每個(gè)centroid,根據(jù)公式:
求出該類所有點(diǎn)的平均值(即中心點(diǎn))進(jìn)行更新。
代碼如下:
1.2 K-means on example dataset
ex7.m中提供了一個(gè)例子,其中中 K 已經(jīng)被手動(dòng)初始化過(guò)了。
% Settings for running K-Means K = 3; max_iters = 10;% For consistency, here we set centroids to specific values % but in practice you want to generate them automatically, such as by % settings them to be random examples (as can be seen in % kMeansInitCentroids). initial_centroids = [3 3; 6 2; 8 5];如上,我們要把點(diǎn)分成三類,迭代次數(shù)為10次。三類的中心點(diǎn)初始化為.
得到如下圖像。(中間的圖像略去,只展示開(kāi)始和完成時(shí)的圖像)
這是初始圖像:
進(jìn)行10次迭代后的圖像:
可以看到三堆點(diǎn)被很好地分成了三類。圖片上同時(shí)也展示了中心點(diǎn)的移動(dòng)軌跡。
1.3 Random initialization
ex7.m中為了方便檢驗(yàn)結(jié)果正確性,給定了K的初始化。而實(shí)際應(yīng)用中,我們需要隨機(jī)初始化。
完成如下代碼:
這樣初始的中心點(diǎn)就是從X中隨機(jī)選擇的K個(gè)點(diǎn)。
1.4 Image compression with K-means
用K-means進(jìn)行圖片壓縮。
用一張的圖片為例,采用RGB,總共需要個(gè)bit。
這里我們對(duì)他進(jìn)行壓縮,把所有顏色分成16類,以其centroid對(duì)應(yīng)的顏色代替整個(gè)一類中的顏色,可以將空間壓縮至 個(gè)bit。
用題目中提供的例子,效果大概如下:
1.5 (Ungraded)Use your own image
隨便找一張本地圖片,先用PS調(diào)整大小,最好在 以下(否則速度會(huì)很慢),運(yùn)行,效果如下:
2 Principal component analysis
我們使用PCA來(lái)減少向量維數(shù)。
2.1 Example dataset
先對(duì)例子中的二維向量實(shí)現(xiàn)降低到一維。
繪制散點(diǎn)圖如下:
2.2 Implementing PCA
首先需要計(jì)算數(shù)據(jù)的協(xié)方差矩陣(covariance matrix)。
然后使用 Octave/MATLAB中的SVD函數(shù)計(jì)算特征向量(eigenvector)。
可以先對(duì)數(shù)據(jù)進(jìn)行normalization和feature scaling的處理。
協(xié)方差矩陣如下計(jì)算:
然后用SVD函數(shù)求特征向量。
故完成pca.m如下:
把求出的特征向量繪制在圖上:
2.3 Dimensionality reduction with PCA
將高維的examples投影到低維上。
2.3.1 Projecting the data onto the principal components
完成projectData.m如下:
function Z = projectData(X, U, K) %PROJECTDATA Computes the reduced data representation when projecting only %on to the top k eigenvectors % Z = projectData(X, U, K) computes the projection of % the normalized inputs X into the reduced dimensional 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 projection of the data using only the top K % eigenvectors in U (first K columns). % For the i-th example X(i,:), the projection on to the k-th % eigenvector is given as follows: % x = X(i, :)'; % projection_k = x' * U(:, k); %Ureduce = U(:,1:K); Z = X * Ureduce;% =============================================================end將X投影到K維空間上。
2.3.2 Reconstructing an approximation of the data
從投影過(guò)的低維恢復(fù)高維:
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 % original data that has been reduced to K dimensions. 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: Compute 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 as follows: % v = Z(i, :)'; % recovered_j = v' * U(j, 1:K)'; % % Notice that U(j, 1:K) is a row vector. % Ureduce = U(:, 1:K); X_rec = Z * Ureduce';% =============================================================end2.3.3 Visualizing the projections
根據(jù)上圖可以看出,恢復(fù)后的圖只保留了其中一個(gè)特征向量上的信息,而垂直方向的信息丟失了。
2.4 Face image dataset
對(duì)人臉圖片進(jìn)行dimension reduction。ex7faces.mat中存有大量人臉的灰度圖() , 因此每一個(gè)向量的維數(shù)是 。
如下是前一百?gòu)埲四槇D:
2.4.1 PCA on faces
用PCA得到其主成分,將其重新轉(zhuǎn)化為 的矩陣后,對(duì)其可視化,如下:(只展示前36個(gè))
2.4.2 Dimensionality reduction
取前100個(gè)特征向量進(jìn)行投影,
可以看出,降低維度后,人臉部的大致框架還保留著,但是失去了一些細(xì)節(jié)。這給我們的啟發(fā)是,當(dāng)我們?cè)谟蒙窠?jīng)網(wǎng)絡(luò)訓(xùn)練人臉識(shí)別時(shí),有時(shí)候可以用這種方式來(lái)提高速度。
2.5 Optional (Ungraded) exercise: PCA for visualization
PCA常用于高維向量的可視化。
如下圖,用K-means對(duì)三維空間上的點(diǎn)進(jìn)行分類。
對(duì)圖片進(jìn)行旋轉(zhuǎn),可以看出這些點(diǎn)大致在一個(gè)平面上
因此我們使用PCA將其降低到二維,并觀察散點(diǎn)圖:
這樣就更利于觀察分類的情況了。
?轉(zhuǎn)載于:https://www.cnblogs.com/EtoDemerzel/p/7881396.html
總結(jié)
以上是生活随笔為你收集整理的机器学习week8 ex7 review的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MediaProxy的Web监控界面及多
- 下一篇: spring boot 初步学习