- Neural Network Programming with TensorFlow
- Manpreet Singh Ghotra Rajdeep Dua
- 173字
- 2021-07-02 15:17:09
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)]
推薦閱讀
- 數(shù)據(jù)要素安全流通
- Hands-On Data Structures and Algorithms with Rust
- Java Data Science Cookbook
- Effective Amazon Machine Learning
- App+軟件+游戲+網(wǎng)站界面設(shè)計(jì)教程
- Learning Spring Boot
- 區(qū)塊鏈通俗讀本
- Python醫(yī)學(xué)數(shù)據(jù)分析入門
- “互聯(lián)網(wǎng)+”時(shí)代立體化計(jì)算機(jī)組
- 金融商業(yè)算法建模:基于Python和SAS
- 數(shù)據(jù)分析師養(yǎng)成寶典
- 從實(shí)踐中學(xué)習(xí)sqlmap數(shù)據(jù)庫注入測(cè)試
- 中文版Access 2007實(shí)例與操作
- 數(shù)據(jù)庫查詢優(yōu)化器的藝術(shù):原理解析與SQL性能優(yōu)化
- 區(qū)塊鏈應(yīng)用開發(fā)指南:業(yè)務(wù)場(chǎng)景剖析與實(shí)戰(zhàn)