1、簡單kmean
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
from ex71_find import findClosestCentroids
from ex71_compute import computeClosestCentroids
data = loadmat('ex7data2.mat')
X = data['X']
#實驗
K = 3
initial_centroids = np.array([[3,3], [6,2], [8,5]])
idx = findClosestCentroids(X, initial_centroids)
centroids = computeClosestCentroids(X, idx, K)
#實例
centroids = initial_centroids
##隨機初始
#np.random.shuffle(X)
#centroids = X[0:K,:]
max_iters = 10
for i in range(max_iters):
idx = findClosestCentroids(X, centroids)
centroids = computeClosestCentroids(X, idx, K)
y_0 = np.where(idx==0)[0]
y_1 = np.where(idx==1)[0]
y_2 = np.where(idx==2)[0]
plt.scatter(X[y_0,0], X[y_0,1], c='r')
plt.scatter(X[y_1,0], X[y_1,1], c='g')
plt.scatter(X[y_2,0], X[y_2,1], c='b')
plt.scatter(centroids[:,0], centroids[:,1], c='k',marker='*')
import numpy as np
def findClosestCentroids(X, centroids):
K = np.size(centroids, 0)
idx = np.zeros([np.size(X,0), 1],dtype=int)
for i in range(np.size(X,0)):
idx[i] = 0
for j in range(K):
a=np.sum(pow((X[i,:] - centroids[j,:]) ,2))
b=np.sum(pow((X[i,:] - centroids[idx[i],:]) ,2))
if (a
import numpy as np
def computeClosestCentroids(X, idx , K):
shape_c = X.shape
Centroids = np.zeros([K, shape_c[1]])
for i in range(K):
loc = np.where(idx==i)
Centroids[i,:] = np.mean(X[loc[0]], axis=0)
return Centroids
2、kmean實踐
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 6 11:40:37 2019
@author: 無限未來
"""
import numpy as np
import matplotlib.pyplot as plt
from ex71_find import findClosestCentroids
from ex71_compute import computeClosestCentroids
import cv2
import os
def img_read(file_path):
image = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), cv2.IMREAD_COLOR)
return image
def img_write(image,save_path):
cv2.imencode(os.path.splitext(save_path)[-1], image)[1].tofile(save_path)
TargetI=img_read('G:/DeepLearning/吳恩達作業/bird_small.png')
pixel = TargetI.reshape((-1,3),order='F')
K=16
#隨機初始
init = np.random.permutation(pixel)
centroids = init[0:K,:]
max_iters = 10
for i in range(max_iters):
idx = findClosestCentroids(pixel, centroids)
centroids = computeClosestCentroids(pixel, idx, K)
result = np.zeros([16384,3])
for i in range(16):
loc = np.where(idx==i)[0]
result[loc,:] = centroids[i,:]
resultI = result.reshape((128,128,3),order='F')
save_path='G:/DeepLearning/吳恩達作業/bird_small2.png'
img_write(resultI,save_path)
3、PCA
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 7 19:41:24 2019
@author: 無限未來
"""
#函數:np.linalg.svd(a,full_matrices=1,compute_uv=1)。
#參數:
#
#a是一個形如(M,N)矩陣
#
#full_matrices的取值是為0或者1,默認值為1,這時u的大小為(M,M),v的大小為(N,N) 。
#否則u的大小為(M,K),v的大小為(K,N) ,K=min(M,N)。
#
#compute_uv的取值是為0或者1,默認值為1,表示計算u,s,v。為0的時候只計算s。
#返回值:
#
#總共有三個返回值u,s,v 特征值為u的對角線
#u大小為(M,M),s大小為(M,N),v大小為(N,N)。
#
#A = u*s*v
#
#其中s是對矩陣a的奇異值分解。s除了對角元素不為0,其他元素都為0,并且對角元素從大到小排列。
#s中有n個奇異值,一般排在后面的比較接近0,所以僅保留比較大的r個奇異值。
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
from scipy.io import loadmat
def featureNormalize(X):
for i in range(X.shape[1]):
m = np.mean(X[:,i])
std = np.std(X[:,i], ddof=1)
X[:,i] = (X[:,i] - m) / std
return X
data = loadmat('ex7data1.mat')
XX = data['X']
Y = featureNormalize(XX)
U = np.zeros(XX.shape[1])
S = np.zeros(XX.shape[1])
sigma = np.matmul( XX.T , XX )/ XX.shape[0]
[U,S,V] = np.linalg.svd(sigma)
K = 1
Z = np.zeros([XX.shape[0], K])
U_reduce = U[:, 0:K]
Z = np.matmul(XX , U_reduce)
X_rec = np.zeros([Z.shape[0], U.shape[0]])
U_reduce = U[:, 0:K]
X_rec = np.matmul(Z , U_reduce.T)
4、人臉PCA
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 7 22:05:55 2019
@author: 無限未來
"""
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
from scipy.io import loadmat
def img_read(file_path):
image = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), cv2.IMREAD_COLOR)
return image
def img_write(image,save_path):
cv2.imencode(os.path.splitext(save_path)[-1], image)[1].tofile(save_path)
def featureNormalize(X):
for i in range(X.shape[1]):
m = np.mean(X[:,i])
std = np.std(X[:,i], ddof=1)
X[:,i] = (X[:,i] - m) / std
return X
data = loadmat('ex7faces.mat')
XX = data['X']
Y = featureNormalize(XX)
U = np.zeros(XX.shape[1])
S = np.zeros(XX.shape[1])
sigma = np.matmul( XX.T , XX )/ XX.shape[0]
[U,S,V] = np.linalg.svd(sigma)
K = 100
Z = np.zeros([XX.shape[0], K])
U_reduce = U[:, 0:K]
Z = np.matmul(XX , U_reduce)
X_rec = np.zeros([Z.shape[0], U.shape[0]])
U_reduce = U[:, 0:K]
X_rec = np.matmul(Z , U_reduce.T)
save_path='G:/DeepLearning/吳恩達作業/face.png'
img_write(X_rec,save_path)
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
