简体   繁体   English

由于调色板,matplotlib scatter plot 出错

[英]Error with matplotlib scatter plot due to color palette

I used this method to crate a scatter plot for another model for mnist dataset, and it works fine for the other model and I cannot figure out what I did wrong with this other model. I used this method to crate a scatter plot for another model for mnist dataset, and it works fine for the other model and I cannot figure out what I did wrong with this other model.

The method is方法是

def scatter(x, labels, subtitle=None):
    # Create a scatter plot of all the 
    # the embeddings of the model.
    # We choose a color palette with seaborn.
    palette = np.array(sns.color_palette("hls", 10))
    # We create a scatter plot.
    f = plt.figure(figsize=(8, 8))
    ax = plt.subplot(aspect='equal')
    sc = ax.scatter(x[:,0], x[:,1], lw=0,alpha = 0.5, s=40,
                c=palette[labels.astype(np.int)])
    plt.xlim(-25, 25)
    plt.ylim(-25, 25)
    ax.axis('off')
    ax.axis('tight')

I use this to create the data for the plot using the mnist dataset from keras我使用它来使用来自 keras 的 mnist 数据集为 plot 创建数据

# Using the newly trained model compute the embeddings 
# for a number images
sample_size = 5000
X_train_trm = model.predict(X_train[:sample_size].reshape(-1,28,28,1))
X_test_trm = model.predict(X_test[:sample_size].reshape(-1,28,28,1))
# TSNE to use dimensionality reduction to visulaise the resultant embeddings
tsne = TSNE()
train_tsne_embeds = tsne.fit_transform(X_train_trm)
scatter(train_tsne_embeds, y_train[:sample_size])

This then gives this error which I do not understand when I check the size of the palette and c as well which should be 5000 and not 150000. The error is this这会给出这个错误,当我检查调色板和 c 的大小时,我不明白这个错误应该是 5000 而不是 150000。错误是这个

ValueError: 'c' argument has 150000 elements, which is inconsistent with 'x' and 'y' with size 5000.

I found out what was the issue after some googling and running into a few dead ins.经过一番谷歌搜索并遇到一些死插件后,我发现了问题所在。 The code I posted works fine.我发布的代码工作正常。 The labels I was using were converted into categorical using我使用的标签被转换为分类使用

y_train = keras.utils.to_categorical(y_train, 10)

Which is why the error had 150000 elements to it.这就是错误包含 150000 个元素的原因。 It was all the onehot encoding.这都是 onehot 编码。 To fix this I made a copy of the labels at the start and then onehot encoded them.为了解决这个问题,我在开始时复制了标签,然后用 onehot 对它们进行编码。

# convert class vectors to binary class matrices
Y_train_raw = y_train

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

And then used the raw labels in the scatter plot然后使用散点图中的原始标签 plot

scatter(train_tsne_embeds, Y_train_raw[:sample_size])

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

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