【发布时间】: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