【问题标题】:Principal component analysis with Matplotlib使用 Matplotlib 进行主成分分析
【发布时间】:2013-02-19 16:52:42
【问题描述】:

我有一个约 1500 行的数据集,其中包含 6 个变量,我使用 PCA 处理并显示:

from matplotlib.mlab import PCA
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
import numpy as np

data = np.array(data)

    try:
        results = PCA(data)
    except:
        raise

    #this will return an array of variance percentages for each component
    print results.fracs

    #
    print results.Wt

    #this will return a 2d array of the data projected into PCA space
    print results.Y 

    x = []
    y = []
    z = []
    for item in results.Y:
        x.append(item[0])
        y.append(item[1])
        z.append(item[2])

    fig1 = plt.figure() # Make a plotting figure
    ax = Axes3D(fig1) # use the plotting figure to create a Axis3D object.
    pltData = [x,y,z] 
    ax.scatter(pltData[0], pltData[1], pltData[2], 'bo') # make a scatter plot of blue dots from the data

    # make simple, bare axis lines through space:
    xAxisLine = ((min(pltData[0]), max(pltData[0])), (0, 0), (0,0)) # 2 points make the x-axis line at the data extrema along x-axis 
    ax.plot(xAxisLine[0], xAxisLine[1], xAxisLine[2], 'r') # make a red line for the x-axis.
    yAxisLine = ((0, 0), (min(pltData[1]), max(pltData[1])), (0,0)) # 2 points make the y-axis line at the data extrema along y-axis
    ax.plot(yAxisLine[0], yAxisLine[1], yAxisLine[2], 'r') # make a red line for the y-axis.
    zAxisLine = ((0, 0), (0,0), (min(pltData[2]), max(pltData[2]))) # 2 points make the z-axis line at the data extrema along z-axis
    ax.plot(zAxisLine[0], zAxisLine[1], zAxisLine[2], 'r') # make a red line for the z-axis.

    # label the axes 
    ax.set_xlabel("x-axis label") 
    ax.set_ylabel("y-axis label")
    ax.set_zlabel("y-axis label")
    ax.set_title("The title of the plot")

    plt.show() # show the plot

我现在想要的是,例如,如何根据另一个数据变量为颜色显示的点着色。例如,如果我在每一行数据中添加一个 ['blue', 'red', 'green'] 范围内的名为 color 的变量,我可以用它来显示点的颜色吗?

【问题讨论】:

    标签: python colors matplotlib pca


    【解决方案1】:

    我认为最简单的做法是,正如您所说,为每个数据点设置一个颜色列表 colors=[ 'blue', 'red', 'green',...] 然后绘制每个点分别:

    for i,xi in enumerate(x):
        ax.scatter(x[i],y[i],z[i],color=colors[i])
    

    另一种方法是查看this question。在这里,您可以为每个点分配一个数字,这些点在颜色图中表示为一种颜色。所以你可以显示给定参数的梯度。

    编辑: 为了回答您的评论,颜色可以以 0 到 1 之间的 3 个值 (RGB) 的元组形式给出

    from matplotlib.colors import Normalize
    xnorm=Normalize(x.min(),x.max())
    ynorm=Normalize(y.min(),y.max())
    colors=[(xnorm(x[i]),ynorm(y[i]),0) for i in range(x.size)]
    ax.scatter(x,y,c=colors)
    

    【讨论】:

    • 我选择从这样的地图创建颜色 col = [cm(float(row[0])/(len(data))) for row in data] 并将其提供给 ax。分散。但我想根据每个变量范围构建颜色。例如,假设我们有 x = [1,10] 'blue' 和 y = [50,100] 'red' :一个点 xy=(5,75) 将根据 5 和75.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2016-02-05
    • 2015-09-04
    • 1970-01-01
    • 2014-06-10
    相关资源
    最近更新 更多