吴恩达机器学习作业python实现(ex7,kmean,PCA)

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)

 

你可能感兴趣的:(机器学习)