import numpy as np
from sklearn.datasets import load_iris
import pandas as pd
class myPCA():
def __init__(self, data, k):
self.data = np.asarray(data)
self.n = self.data.shape[1]
self.k = k
self.PCA()
def PCA(self):
"""
算法16.1
"""
# 归一化
mean = self.data.mean(axis=1).reshape(-1, 1)
S = (1 / (self.n - 1)) * (np.power(self.data - mean, 2)).sum(axis=1).reshape(-1, 1)
# print(S.shape,mean.shape)
self.data = (self.data - mean) / np.sqrt(S)
# 构造X'
X = (1 / np.sqrt(self.n - 1)) * self.data.T
#print(X.shape)
_, _, v = np.linalg.svd(X)
#print(v.shape)
#v=v.T
self.result = v[:, :self.k].T @ self.data
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label'] # 原数据集标签带'(cm)'
data = np.asarray(df.iloc[:, :4]).T
myPCA = myPCA(data, 3)
print(myPCA.result.shape)
PCA降维实现
最新推荐文章于 2024-04-09 21:19:27 发布