- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 http://scikit-learn.org/stable/auto_examples/decomposition/plot_pca_iris.html为我自己的数据构建 3D PCA 图。但是,该教程没有具体说明如何添加图例。另一页,https://matplotlib.org/users/legend_guide.html做了,但我看不出如何将第二个教程中的信息应用到第一个教程中。
如何修改下面的代码来添加图例?
# Code source: Gae"l Varoquaux
# License: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import decomposition
from sklearn import datasets
np.random.seed(5)
centers = [[1, 1], [-1, -1], [1, -1]]
iris = datasets.load_iris()
X = iris.data#the floating point values
y = iris.target#unsigned integers specifying group
fig = plt.figure(1, figsize=(4, 3))
plt.clf()
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
plt.cla()
pca = decomposition.PCA(n_components=3)
pca.fit(X)
X = pca.transform(X)
for name, label in [('Setosa', 0), ('Versicolour', 1), ('Virginica', 2)]:
ax.text3D(X[y == label, 0].mean(),
X[y == label, 1].mean() + 1.5,
X[y == label, 2].mean(), name,
horizontalalignment='center',
bbox=dict(alpha=.5, edgecolor='w', facecolor='w'))
# Reorder the labels to have colors matching the cluster results
y = np.choose(y, [1, 2, 0]).astype(np.float)
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap=plt.cm.spectral,
edgecolor='k')
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
plt.show()
最佳答案
其他答案存在一些问题,OP 和回答者似乎都不清楚;因此,这不是完整的答案,而是现有答案的附录。
spectral
颜色图已从 2.2 版的 matplotlib 中删除,使用 Spectral
或 nipy_spectral
或任何 other valid colormap .
matplotlib 中的任何颜色图的范围都是从 0 到 1。如果您使用该范围之外的任何值调用它,它只会给你最外面的颜色。因此,要从颜色图中获取颜色,您需要对值进行归一化。这是通过 Normalize
实例完成的。在这种情况下,这是 scatter
的内部。
因此使用 sc = ax.scatter(...)
然后使用 sc.cmap(sc.norm(value))
根据相同的方法得到一个值散点图中使用的映射。因此代码应该使用
[sc.cmap(sc.norm(i)) for i in [1, 2, 0]]
图例在图外。该图的大小为 4 x 3 英寸 (figsize=(4, 3)
)。轴占据该空间宽度的 95% (rect=[0, 0, .95, 1]
)。对 legend
的调用将图例的右中心点置于坐标轴宽度的 1.7 倍处 = 4*0.95*1.7 = 6.46 英寸。 (bbox_to_anchor=(1.7,0.5)
)。
我这边的替代建议:使图形变大 (figsize=(5.5, 3)
),以便图例适合,使轴仅占据图形宽度的 70%,这样你还剩下30%给传奇。将图例的左侧靠近轴边界 (bbox_to_anchor=(1.0, .5)
)。
有关此主题的更多信息,请参阅 How to put the legend out of the plot .
您仍然在 jupyter notebook 中看到包括图例在内的完整图形的原因是 jupyter 只会将所有内容保存在 Canvas 内,即使它们重叠并因此放大了图形。
总的来说,代码可能看起来像
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np; np.random.seed(5)
from sklearn import decomposition, datasets
centers = [[1, 1], [-1, -1], [1, -1]]
iris = datasets.load_iris()
X = iris.data #the floating point values
y = iris.target #unsigned integers specifying group
fig = plt.figure(figsize=(5.5, 3))
ax = Axes3D(fig, rect=[0, 0, .7, 1], elev=48, azim=134)
pca = decomposition.PCA(n_components=3)
pca.fit(X)
X = pca.transform(X)
labelTups = [('Setosa', 0), ('Versicolour', 1), ('Virginica', 2)]
for name, label in labelTups:
ax.text3D(X[y == label, 0].mean(),
X[y == label, 1].mean() + 1.5,
X[y == label, 2].mean(), name,
horizontalalignment='center',
bbox=dict(alpha=.5, edgecolor='w', facecolor='w'))
# Reorder the labels to have colors matching the cluster results
y = np.choose(y, [1, 2, 0]).astype(np.float)
sc = ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap="Spectral", edgecolor='k')
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
colors = [sc.cmap(sc.norm(i)) for i in [1, 2, 0]]
custom_lines = [plt.Line2D([],[], ls="", marker='.',
mec='k', mfc=c, mew=.1, ms=20) for c in colors]
ax.legend(custom_lines, [lt[0] for lt in labelTups],
loc='center left', bbox_to_anchor=(1.0, .5))
plt.show()
并生产
关于python - matplotlib : how to add legend? 中的 3D PCA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49564844/
我尝试用 PCA 构建一个定向边界框。在图片中您可以看到我的结果: 红点:点云 蓝色向量:PCA 分量 我尝试将点投影到向量上,以获得最小值、最大值和平均值。 但是我现在如何定义我的盒子呢?有什么想法
我们如何将 PCA 应用于一维数组? double[][] data = new double [1][600]; PCA pca = new PCA(data, 20); data = pca.ge
我知道PCA和ICA都用于降维,并且在PCA中主成分是正交的(不一定独立),但在ICA中它们是独立的。有人能澄清一下什么时候使用 ICA 而不是 PCA 更好吗? 最佳答案 ICA 不是一种降维技术。
我正在使用 scikit-learning 做一些降维任务。 我的训练/测试数据采用 libsvm 格式。它是一个有 50 万列的大型稀疏矩阵。 我使用 load_svmlight_file 函数加载
我一直在尝试使用 PCA 进行降维。我目前有一个大小为 (100, 100) 的图像,我正在使用一个由 140 个 Gabor 滤波器组成的滤波器组,其中每个滤波器都会给我一个响应,这又是一个 (10
我使用以下简单代码在具有 10 个特征的数据框上运行 PCA: pca = PCA() fit = pca.fit(dfPca) pca.explained_variance_ratio_ 的结果显示
我正在使用 scikit-learn PCA查找具有大约 20000 个特征和 400 多个样本的数据集的主要成分。 但是,与Orange3 PCA相比应该使用 scikit-learn PCA,我得
Sklearn PCA 是 pca.components_ 的 loadings?我很确定是这样,但我正在尝试遵循一篇研究论文,但我从他们的加载中得到了不同的结果。我在 sklearn 文档中找不到它
我有一个包含 50 多个变量的数据框 data,我正在尝试使用 caret 包在 R 中执行 PCA。 library(caret) library(e1071) trans <- preProces
我正在使用 PCA 来降低 N 维数据集的维数,但我想增强对大异常值的稳健性,因此我一直在研究 Robust PCA 代码。 对于传统的 PCA,我使用的是 python 的 sklearn.deco
我正在降低 Spark DataFrame 的维度与 PCA带有 pyspark 的模型(使用 spark ml 库)如下: pca = PCA(k=3, inputCol="features", o
我在 matlab 和 python 中生成相同的矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13
概述 主成分分析(Principal Component Analysis,PCA)是一种常用的数据降维和特征提取技术,用于将高维数据转换为低维的特征空间。其目标是通过线性变换将原始特征转化为
目录 计算过程 投影分量计算 假设你有一家理发店,已经记录了过去一年中所有顾客的头发长度和发型偏好的数据。现在你想从这些数据中提取一些主要的信息,比如顾客最常
我正在考虑使用 PCA(TruncatedSVD) 来减少我的稀疏矩阵的维数。 我将我的数据拆分为训练和测试拆分。 X_train , X_test, y_train, y_test = train_
我有来自四个群体、四个处理和三个重复的个体数据集。每个个体仅在一个群体、处理和重复组合中。我对每个人进行了四次测量。我想对每个种群、底物和重复组合的这些测量进行 PCA。 我知道如何对所有个体进行 P
在考虑均值时,数字 1 和 2 背后的直觉是什么?这将如何影响性能和准确性? 1号: pca = decomposition.PCA(n_components=4) X_centere
我正在使用来自 here 的输入数据(见第 3.1 节)。 我正在尝试使用 scikit-learn 重现它们的协方差矩阵、特征值和特征向量。但是,我无法重现数据源中显示的结果。我也在别处看到过这个输
我要做的事情如下:我有一套 Vektors v1-vn对于这些,我需要协方差矩阵(我在做 pca 时得到的)。我还需要协方差矩阵的特征值和特征向量。我按降序对特征值进行排序,然后根据相应的特征值对特征
给定 http://docs.opencv.org/modules/core/doc/operations_on_arrays.html PCA 应该可以通过传递一个矩阵来初始化。 cv::Mat m
我是一名优秀的程序员,十分优秀!