python中数据的多维伸缩

2024-05-23 14:44:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码将多维缩放应用于名为parkinsonData的数据示例:

iterations=4
count=0
while(count<iterations):
    mds1=manifold.MDS(n_components=2, max_iter=3000)
    pos=mds1.fit(parkinsonData).embedding_
    plt.scatter(pos[:, 0], pos[:, 1])
    count=count+1

用这个方法,我得到了4个不同的MDS算法图,由于随机种子的存在,它们都是不同的。这些图有不同的颜色,但是parkinsonData有一个名为status的列,它有0或1个值,我想用不同的颜色在每个绘图中显示这种差异。在

例如,我想实现:

一个绘图,状态字段中的0值使用一种颜色,状态字段中的1个值使用不同的颜色。在

第二次绘图,状态字段中的0值使用一种颜色,状态字段中的1值使用另一种颜色。(两种颜色都与第一个图不同)

第三次绘图,状态字段中的0值使用一种颜色,状态字段中的1值使用不同的颜色。(两种颜色都与第一和第二种绘图不同)

第四个绘图,状态字段中的0值使用一种颜色,状态字段中的1个值使用不同的颜色。(两种颜色都与第一、第二和第三个绘图区不同)

有人知道如何实现这种预期的行为吗?在


Tags: 数据代码pos绘图示例颜色countmds
1条回答
网友
1楼 · 发布于 2024-05-23 14:44:18

你可以这样做

%matplotlib inline
import matplotlib.pyplot as plt

# example data
Y = [[ 1 , 2 , 3 ,6], [ 1 , 2 , 3 ,6], [ 1 , 2 , 3 ,6], [ 1 , 2 , 3 ,6]]
X = [[ 1 , 2 , 4 ,5], [ 1 , 2 , 3 ,6], [ 1 , 2 , 3 ,6], [ 1 , 2 , 3 ,6]]
status = [[0,1,0,0], [0,0,1,1], [1,1,0,0], [0,1,0,1]]

# create a list of list of unique colors for 4 plots
my_colors = [['red','green'],['blue','black'],['magenta','grey'],['purple','cyan']]


iterations=4
count=0
while(count<iterations):
    plt.figure()
    for i,j in enumerate(X):
        plt.scatter(X[count][i],Y[count][i],color = my_colors[count][status[count][i]])
    count=count+1
    plt.show()

结果是(我只附加了2个图像,但4个图像是用唯一的颜色集创建的)

enter image description hereenter image description here

相关问题 更多 >