【问题标题】:How to get weights for PCA如何获得 PCA 的权重
【发布时间】:2020-06-19 14:04:39
【问题描述】:

我正在尝试使用小品学习来查找 PCA 的权重。但是,这些方法都不起作用。

代码:

import pandas as pd
import numpy as np
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
# load dataset into Pandas DataFrame
df = pd.read_csv(url, names=['sepal length','sepal width','petal length','petal width','target'])

from sklearn.preprocessing import StandardScaler
features = ['sepal length', 'sepal width', 'petal length', 'petal width']
# Separating out the features
x = df.loc[:, features].values
# Standardizing the features
x = StandardScaler().fit_transform(x)
from sklearn.decomposition import PCA
pca = PCA(n_components=1)
principalComponents = pca.fit_transform(x)

寻找权重

方法一

weights = pca.components_*np.sqrt(pca.explained_variance_)
# recovering original data
pca_recovered = np.dot(weights, x)
### This output is not matching with PCA

方法二

# Standardising the weights then recovering
weights1 = weights/np.sum(weights)
pca_recovered = np.dot(weights1, x)
### This output is not matching with PCA

如果我在这里做错了什么,请帮忙。或者,包中缺少某些东西。

【问题讨论】:

    标签: python scikit-learn pca


    【解决方案1】:

    代替

    weights = pca.components_*np.sqrt(pca.explained_variance_)
    

    如果我简单地使用

    weights = pca.components_
    

    可能是第一次尝试,出现计算错误。

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2021-06-19
      • 2018-02-23
      • 2017-02-06
      相关资源
      最近更新 更多