ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python机器学习:PCA与梯度上升:05高维数据映射成低维数据(封装一个PCA)

2020-12-06 20:03:45  阅读:212  来源: 互联网

标签:PCA 05 Python self components np 100 pca


使用封装后的PCA进行操作

import numpy as np
import matplotlib.pyplot as plt
from pcaa.PCA import PCA

生成数据

X = np.empty((100,2))
X[:,0] = np.random.uniform(0,100,size=100)#产生实数
X[:,1] = 0.75 * X[:,0] + 3. +np.random.normal(0,10,size=100)
pca = PCA(n_components=2)
pca.fit(X)
print(pca.components_)
[[ 0.77420752  0.63293184]
 [-0.63292993  0.77420909]]

降维操作,此时维度变成1

#降维操作
pca = PCA(n_components=1)
pca.fit(X)
X_reduction = pca.transform(X)
print(X_reduction.shape)
(100, 1)

恢复维度

#恢复
X_restore = pca.inverse_transform(X_reduction)
print(X_restore.shape)
(100, 2)


绘图

  1. 蓝色是原本的样本点
  2. 红色是维度恢复之后的点
  3. 绿色的线是w1的方向
plt.scatter(X[:,0],X[:,1],color = 'b')
plt.scatter(X_restore[:,0],X_restore[:,1],color = 'r')
plt.plot([0,0.77 * 100] ,[0,0.63 * 100],color = 'g')

在这里插入图片描述

在这里插入图片描述
主成分分析法本质就是从一个坐标系转换到另一个坐标系

如何从n维转换成k维??

在这里插入图片描述
在这里插入图片描述

反向映射
在这里插入图片描述
封装的PCA.py

import numpy as np

class PCA():
    def __init__(self,n_components):
        """初始化pca"""
        assert n_components >=1,"n_components must be valid"

        self.n_components = n_components
        self.components_ = None

    def fit(self,X,eta = 0.01,n_iters = 1e4):
        """获得数据集X的前n个主成分"""

        assert self.n_components <= X.shape[1],\
        "n_components must not be greater than the feature number of X"

        def demean(X):
            return X - np.mean(X, axis=0)  # 1*n向量

        def f(w, X):
            return np.sum((X.dot(w) ** 2)) / len(X)

        def df(w, X):
            return X.T.dot(X.dot(w)) * 2 / len(X)

        def direction(w):
            return w / np.linalg.norm(w)  # 求模

        def first_component(X, initial_w, eta, n_iters=1e4, epsilon=1e-8):

            w = direction(initial_w)
            cur_iter = 0

            while cur_iter < n_iters:
                gradient = df(w, X)
                last_w = w
                w = w + eta * gradient
                w = direction(w)  # 注意 每次w都要求成单位向量
                if (np.abs(f(w, X) - f(last_w, X)) < epsilon):
                    break

                cur_iter += 1
            return w

        X_pca = demean(X)
        self.components_ = np.empty(shape = (self.n_components,X.shape[1]))

        for i in range(self.n_components):
            initial_w = np.random.random(X_pca.shape[1])
            w = first_component(X_pca, initial_w,eta,n_iters)
            self.components_[i,:] = w

            X_pca = X_pca - X_pca.dot(w).reshape(-1, 1) * w

        return self

    def transform(self,X):
        """将给定的X,映射到各个主成分分量中"""
        assert X.shape[1] == self.components_.shape[1]

        return X.dot(self.components_.T)

    def inverse_transform(self,X):
        """将给定的X,反向映射回来原来的特征空间"""
        assert X.shape[1] == self.components_.shape[0]

        return X.dot(self.components_)


    def __repr__(self):
        return 'PCA(n_components = %d)' % self.n_components
if __name__ == '__main__':
    X = np.empty((100, 2))
    X[:, 0] = np.random.uniform(0, 100, size=100)  # 产生实数
    X[:, 1] = 0.75 * X[:, 0] + 3. + np.random.normal(0, 10, size=100)
    # %%
    pca = PCA(n_components=2)
    pca.fit(X)
    print(pca.components_)
    #降维操作
    pca = PCA(n_components=1)
    pca.fit(X)
    X_reduction = pca.transform(X)
    print(X_reduction.shape)
    #恢复
    X_restore = pca.inverse_transform(X_reduction)
    print(X_restore.shape)

标签:PCA,05,Python,self,components,np,100,pca
来源: https://blog.csdn.net/weixin_46815330/article/details/110739469

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有