简体   繁体   English

如何修复“plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)”的错误

[英]How to fix the error of "plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)"

I'm testing the " Variational autoencoder (VAE) " from this link: https://blog.keras.io/building-autoencoders-in-keras.html我正在通过此链接测试“ Variational autoencoder (VAE) ”: https://blog.keras.io/building-autoencoders-in-keras.ZFC35FDC70D5FC69D2569883A8EZCA7

But the following code will cause error:但是下面的代码会导致错误:

x_test_encoded = encoder.predict(x_test, batch_size=batch_size)
plt.figure(figsize=(6, 6))
plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)
plt.colorbar()
plt.show()

Here is the error:这是错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_3369/2564361963.py in <cell line: 2>()
      1 plt.figure(figsize=(6, 6))
----> 2 plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)
      3 plt.colorbar()
      4 plt.show()

TypeError: list indices must be integers or slices, not tuple

I have tried to print the data type of x_test_encoded :我试图打印x_test_encoded的数据类型:

print("x_test_encoded: type: {}, len: {}".format(
    type(x_test_encoded),
    len(x_test_encoded)
))
x_test_encoded

Output: Output:

x_test_encoded: type: <class 'list'>, len: 3

[array([[-0.5947027 , -0.52120334],
        [ 0.4796909 ,  0.3287477 ],
        [-3.005046  , -0.34468982],
        ...,
        [-0.62982583, -0.240578  ],
        [-0.9350344 ,  0.38294736],
        [ 0.82171804, -0.7310184 ]], dtype=float32),
 array([[-1.2073065 , -1.0317452 ],
        [-1.517373  , -1.224308  ],
        [-0.15326577, -0.5757952 ],
        ...,
        [-1.0883944 , -0.97350955],
        [-0.97157186, -1.1322904 ],
        [-1.5575923 , -1.2778456 ]], dtype=float32),
 array([[-0.60485274, -0.56204545],
        [ 0.46670908,  0.34251845],
        [-2.9650807 , -0.40624568],
        ...,
        [-0.5976225 , -0.25444514],
        [-0.95324576,  0.4048244 ],
        [ 0.8028061 , -0.66895425]], dtype=float32)]

Print the first element:打印第一个元素:

print("x_test_encoded[:][0]: type: {}, len: {}, shape: {}, dtype: {}".format(
    type(x_test_encoded[:][0]),
    len(x_test_encoded[:][0]),
    x_test_encoded[:][0].shape,
    x_test_encoded[:][0].dtype
))
x_test_encoded[:][0]

Output: Output:

x_test_encoded[:][0]: type: <class 'numpy.ndarray'>, len: 10000, shape: (10000, 2), dtype: float32

array([[-0.5947027 , -0.52120334],
       [ 0.4796909 ,  0.3287477 ],
       [-3.005046  , -0.34468982],
       ...,
       [-0.62982583, -0.240578  ],
       [-0.9350344 ,  0.38294736],
       [ 0.82171804, -0.7310184 ]], dtype=float32)

How to fix the error of " plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test) "?如何修复“ plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test) ”的错误?

What does " [:, 0] " mean in a " list " type variable? [:, 0] ”在“ list ”类型变量中是什么意思?

Your encoder is has three outputs [z_mean, z_log_sigma, z] , but you are actually only interested in z at position [-1].您的编码器具有三个输出[z_mean, z_log_sigma, z] ,但您实际上只对 position [-1] 处的z感兴趣。 So, it should actually be something like this (the tutorial seems to have an error):所以,它实际上应该是这样的(教程似乎有错误):

x_test_encoded = encoder.predict(x_test, batch_size=32)
plt.figure(figsize=(6, 6))
plt.scatter(x_test_encoded[-1][:, 0], x_test_encoded[-1][:, 1], c=y_test)
plt.colorbar()
plt.show()

在此处输入图像描述

Since z consists of two latent variables, you index 0 and 1.由于z包含两个潜在变量,因此您索引 0 和 1。

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

相关问题 如何将数据集拆分为 (X_train, y_train), (X_test, y_test)? - How to split dataset into (X_train, y_train), (X_test, y_test)? 如何在 X_train、y_train、X_test、y_test 中拆分图像数据集? - How to split an image dataset in X_train, y_train, X_test, y_test? Python-遇到x_test y_test适合错误 - Python - running into x_test y_test fit errors 如何在 y=x 的函数的一行中编写 plt.scatter(x, y) function - How to Write a plt.scatter(x, y) function in one line where y=function of x scikit-learn 中的交叉验证:(X_test, y_test) 的平均绝对误差 - Cross-validation in scikit-learn: mean absolute error of (X_test, y_test) clf.score(X_test,Y_test)如何用于线性回归? - How does clf.score(X_test,Y_test) work for linear regression? 如何将 tf.data.Dataset 拆分为 x_train、y_train、x_test、y_test for keras - how to split up tf.data.Dataset into x_train, y_train, x_test, y_test for keras sklearn中的x_test、x_train、y_test、y_train有什么区别? - What is the difference between x_test, x_train, y_test, y_train in sklearn? 在 tensorflow 中创建 X_test、X_train、Y_test、Y_train - Create X_test, X_train, Y_test, Y_train in tensorflow 使用 train_test_split() 后向我的 x_test 和 y_test 添加额外的实例 - Add extra instances to my x_test and y_test after using train_test_split()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM