【问题标题】:How to solve this KeyError:0 in JupyterLab?如何在 JupyterLab 中解决这个 KeyError:0?
【发布时间】:2021-05-11 02:50:27
【问题描述】:

我在 Google Colab 中运行此代码,它运行良好。但是当我在 JupyterLab 中运行它时,会发生这个 keyerror:0。我该如何解决这个问题?

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

【问题讨论】:

  • 请不要发布数据/代码/错误消息的图像。在 SO 上直接在此处发布文本。

标签: python pandas matplotlib google-colaboratory jupyter-lab


【解决方案1】:

根据给定的屏幕截图,这实际上是X 的pandas key 错误,但是在给定的代码中没有pandas 对X 的引用,这意味着X 的值不知何故被错误损坏了.

因此,我建议您重新启动 jupyter 内核并重试。

注意:根据您的代码type(X[0]) 将是numpy.ndarray

【讨论】:

  • 非常感谢您的帮助,我测试了X的类型,发现它是DataFrame,现在问题已经通过将它转移到数组来解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2023-01-26
  • 1970-01-01
  • 2016-05-25
相关资源
最近更新 更多