简体   繁体   English

如何在 JupyterLab 中解决这个 KeyError:0?

[英]How to solve this KeyError:0 in JupyterLab?

I run this code in Google Colab, it works well.我在 Google Colab 中运行此代码,效果很好。 But when I run it in my JupyterLab, this keyerror:0 happens.但是当我在 JupyterLab 中运行它时,会发生这个 keyerror:0。 How can I solve this problem?我怎么解决这个问题?

import sys
import sklearn
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)

# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "classification"
IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID)
os.makedirs(IMAGES_PATH, exist_ok=True)

def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300):
    path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension)
    print("Saving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.savefig(path, format=fig_extension, dpi=resolution)

from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)
X, y = mnist["data"], mnist["target"]

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt

some_digit = X[0]
some_digit_image = some_digit.reshape(28, 28)
plt.imshow(some_digit_image, cmap=mpl.cm.binary)
plt.axis("off")

save_fig("some_digit_plot")
plt.show()

This is the KeyError:0 shown in the JupyterLab这是 JupyterLab 中显示的 KeyError:0

According to the given screenshot, it is actually a pandas key error for X , but there is no pandas reference to the X in the given code, which it means that somehow X value was corrupted by mistake.根据给定的屏幕截图,它实际上是X的 pandas 键错误,但是在给定的代码中没有 pandas 对X的引用,这意味着X值被错误地损坏了。

Hence I recommend you to restart your jupyter kernel and try it again.因此,我建议您重新启动 jupyter kernel 并重试。

Note : According to your code type(X[0]) will be numpy.ndarray注意:根据您的代码type(X[0])将是numpy.ndarray

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

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