吴恩达《机器学习》——第七次作业:主要成分分析(PCA)

import numpy as np

import matplotlib.pyplot as plt
from scipy.io import loadmat

data = loadmat('data/ex7data1.mat')
X = data['X']
fig, ax = plt.subplots(figsize=(12, 8))
ax.scatter(X[:, 0], X[:, 1])
plt.show()

def pca(X):
    X = (X - X.mean()) / X.std()        #特征缩放

    X = np.matrix(X)

    cov = (X.T * X) / X.shape[0]        #协方差

    U, S, V = np.linalg.svd(cov)        #奇异值分解

    return U, S, V

def project_data(X, U, k):
    U_reduced = U[:, :k]
    return np.dot(X, U_reduced)

def recover_data(Z, U, k):
    U_reduced = U[:, :k]
    return np.dot(Z, U_reduced.T)

U, S, V = pca(X)

Z = project_data(X, U, 1)

你可能感兴趣的:(ML)