• 大小: 52.03MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2022-11-10
  • 语言: Python
  • 标签: 降维方法  

资源简介

PCA 是最常用的一种降维方法。python3.6,亲测通过,和大家交流学习

资源截图

代码片段和文件信息

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasetsdecompositionmanifold

def load_data():
    iris=datasets.load_iris()
    return iris.datairis.target

def test_PCA(*data):
    XY=data
    pca=decomposition.PCA(n_components=None)
    pca.fit(X)
    print(“explained variance ratio:%s“%str(pca.explained_variance_ratio_))

def plot_PCA(*data):
    XY=data
    pca=decomposition.PCA(n_components=2)
    pca.fit(X)
    X_r=pca.transform(X)
 #   print(X_r)

    fig=plt.figure()
    ax=fig.add_subplot(111)
    colors=((100)(010)(001)(0.50.50)(00.50.5)(0.500.5)(0.40.60)(0.60.40)(00.60.4)(0.50.30.2))
    for labelcolor in zip(np.unique(Y)colors):
        position=Y==label
  #      print(position)
        ax.scatter(X_r[position0]X_r[position1]label=“target=%d“%labelcolor=color)
    ax.set_xlabel(“X[0]“)
    ax.set_ylabel(“Y[0]“)
    ax.legend(loc=“best“)
    ax.set_title(“PCA“)
    plt.show()

XY=load_data()
test_PCA(XY)
plot_PCA(XY)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        300  2018-10-09 20:45  PCA\.idea\misc.xml

     文件        265  2018-10-09 20:44  PCA\.idea\modules.xml

     文件        478  2018-10-09 20:45  PCA\.idea\PCA.iml

     文件       9036  2018-10-10 20:59  PCA\.idea\workspace.xml

     文件       1049  2018-10-09 21:04  PCA\PCA.py

     文件         10  2018-10-09 20:57  PCA\venv\Lib\site-packages\cycler-0.10.0.dist-info\DEscriptION.rst

     文件          4  2018-10-09 20:57  PCA\venv\Lib\site-packages\cycler-0.10.0.dist-info\INSTALLER

     文件        722  2018-10-09 20:57  PCA\venv\Lib\site-packages\cycler-0.10.0.dist-info\metaDATA

     文件        875  2018-10-09 20:57  PCA\venv\Lib\site-packages\cycler-0.10.0.dist-info\metadata.json

     文件        683  2018-10-09 20:57  PCA\venv\Lib\site-packages\cycler-0.10.0.dist-info\RECORD

     文件          7  2018-10-09 20:57  PCA\venv\Lib\site-packages\cycler-0.10.0.dist-info\top_level.txt

     文件        110  2018-10-09 20:57  PCA\venv\Lib\site-packages\cycler-0.10.0.dist-info\WHEEL

     文件      15959  2018-10-09 20:57  PCA\venv\Lib\site-packages\cycler.py

     文件       2684  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\easter.py

     文件      12902  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\parser\isoparser.py

     文件      57607  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\parser\_parser.py

     文件       1727  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\parser\__init__.py

     文件      11068  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\parser\__pycache__\isoparser.cpython-36.pyc

     文件      39709  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\parser\__pycache__\_parser.cpython-36.pyc

     文件       2054  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\parser\__pycache__\__init__.cpython-36.pyc

     文件      24418  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\relativedelta.py

     文件      64802  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\rrule.py

     文件      60472  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\tz\tz.py

     文件      11318  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\tz\win.py

     文件      12892  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\tz\_common.py

     文件       1434  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\tz\_factories.py

     文件        551  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\tz\__init__.py

     文件      44222  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\tz\__pycache__\tz.cpython-36.pyc

     文件       9513  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\tz\__pycache__\win.cpython-36.pyc

     文件      12265  2018-10-09 20:57  PCA\venv\Lib\site-packages\dateutil\tz\__pycache__\_common.cpython-36.pyc

............此处省略4887个文件信息

评论

共有 条评论

相关资源