繁体   English   中英

为什么我可以调用从原始数据到 plot 的 PCA numpy.ndarray 的密钥?

[英]Why can I call upon a key from the original data to a plot for a PCA numpy.ndarray?

嗨,我有一个理论上的问题,来自一个运行良好的代码。

我正在对 sklearn 的 load_breast_cancer 数据集运行 PCA。 运行 PCA 后,我 plot 基于前两个主要成分的数据,我知道我可以通过原始 load_breast_cancer 数据集中的一个键为数据点着色,即“目标”。

我特别关心的代码是当我 plot 和我写“c=cancer['target']”时。 由于 x_pca 是形状为 (569, 2) 的 numpy.ndarray,如何通过所有 PCA 和缩放保留“目标”列?

代码如下:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
%matplotlib inline

#importing dataset
from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
df = pd.DataFrame(cancer['data'],columns=cancer['feature_names'])

#scalling
scaler = StandardScaler()
scaler.fit(df)
scaled_data = scaler.transform(df)

# PCA
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(scaled_data)
x_pca = pca.transform(scaled_data)

#Plotting
plt.figure(figsize=(8,6))
#Note that it is an array, not a dataframe so brackets refer to order
plt.scatter(x_pca[:,0],x_pca[:,1],c=cancer['target'],cmap='plasma')
plt.xlabel('First PC')
plt.ylabel('Second PC')

谢谢!

似乎您通过管道运行df ,并且df不包含target作为列。 所以它在这个过程中没有被转化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM