官术网_书友最值得收藏!

Principal Component Analysis

Principal Component Analysis (PCA) projects the given dataset onto a lower dimensional linear space so that the variance of the projected data is maximized. PCA requires the eigenvalues and eigenvectors of the covariance matrix, which is the product where X is the data matrix.

SVD on the data matrix X is given as follows:

The following example shows PCA using SVD:

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as FF
import pandas as pd

path = "/neuralnetwork-programming/ch01/plots"
logs = "/neuralnetwork-programming/ch01/logs"

xMatrix = np.array([[0,2,1,0,0,0,0,0],
[2,0,0,1,0,1,0,0],
[1,0,0,0,0,0,1,0],
[0,1,0,0,1,0,0,0],
[0,0,0,1,0,0,0,1],
[0,1,0,0,0,0,0,1],
[0,0,1,0,0,0,0,1],
[0,0,0,0,1,1,1,0]], dtype=np.float32)

def pca(mat):
mat = tf.constant(mat, dtype=tf.float32)
mean = tf.reduce_mean(mat, 0)
less = mat - mean
s, u, v = tf.svd(less, full_matrices=True, compute_uv=True)

s2 = s ** 2
variance_ratio = s2 / tf.reduce_sum(s2)

with tf.Session() as session:
run = session.run([variance_ratio])
return run


if __name__ == '__main__':
print(pca(xMatrix))

The output of the listing is shown as follows:

[array([  4.15949494e-01,   2.08390564e-01,   1.90929279e-01,
8.36438537e-02, 5.55494241e-02, 2.46047471e-02,
2.09326427e-02, 3.57540098e-16], dtype=float32)]
主站蜘蛛池模板: 石嘴山市| 房山区| 盖州市| 贵港市| 会宁县| 冷水江市| 江达县| 清水河县| 高碑店市| 达孜县| 山东省| 永平县| 吴忠市| 宽城| 砀山县| 龙岩市| 抚顺市| 呼和浩特市| 合川市| 安乡县| 翁源县| 尤溪县| 大同县| 阳高县| 比如县| 屏南县| 兰州市| 辉县市| 西乌珠穆沁旗| 三河市| 宣汉县| 靖江市| 玉树县| 义乌市| 个旧市| 建平县| 桐柏县| 清水县| 松潘县| 太原市| 临泉县|