簡體   English   中英

我應該如何修改 JSON 元數據以便我可以在 jupyter notebook 中打開 ipynb 文件?

[英]How should I modify the JSON metadata so that I can open a ipynb file in a jupyter notebook?

我無法再在 VS-Code jupyter 筆記本中打開 ipynb 文件。 我對這個特定文件有問題,因為我的其他文件打開沒有問題。 我認為問題來自元數據中的錯誤,因為此文件中的元數據與成功打開的其他文件的元數據結構看起來非常不同。

這個文件過去打開時沒有任何故障,但現在當我嘗試打開文件時,我看到以下錯誤:

命令失敗:C:/Users/Tony/anaconda3/Scripts/activate && conda activate base && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python c:\\Users\\Tony.vscode\\extensions\\ms-python.python -2020.9.112786\\pythonFiles\\pyvsc-run-isolated.py c:/Users/Tony/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/printEnvVariables.py

來源:Python(擴展)

我的理解是VS Code找不到指定的python解釋器,這反過來又會阻止內核被激活。(我不是很有經驗,如果我錯了,請糾正我!)。

我曾嘗試將 python 解釋器從文件中復制粘貼到損壞的文件中,但沒有奏效。

請參閱下面損壞的 JSON 文件的副本以及成功打開的 ipynb 文件的元數據結構示例。

非常感謝這方面的所有幫助!

非常感謝,托尼

損壞的 JSON 文件

{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "numpy_random.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lhmyI7sB_bKJ",
        "colab_type": "text"
      },
      "source": [
        "# Generate Random Numbers"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MulyzIdD_kxf",
        "colab_type": "text"
      },
      "source": [
        "`numpy.random` is frequently used for generating random numbers.\n",
        "\n",
        "For more details, please refer to [Random sampling (numpy.random)](https://docs.scipy.org/doc/numpy/reference/routines.random.html)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vtrQ-n7PAoYJ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nLB8lH051pDg",
        "colab_type": "text"
      },
      "source": [
        "`np.random.seed` sets the seed for the generator."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PzxXe5pR3QI0",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "np.random.seed(1)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cSSX-VCBAnuJ",
        "colab_type": "text"
      },
      "source": [
        "`np.random.rand` generates numbers uniformly distribution over $[0, 1)$"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ran81EdeAKl6",
        "colab_type": "code",
        "outputId": "9adb1089-0417-490b-f31e-6730df35d688",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 53
        }
      },
      "source": [
        "np.random.rand(2, 3) # Generate 2 * 3 random numbers"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[4.17022005e-01, 7.20324493e-01, 1.14374817e-04],\n",
              "       [3.02332573e-01, 1.46755891e-01, 9.23385948e-02]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "g_jgVxcfAi00",
        "colab_type": "text"
      },
      "source": [
        "`np.radnom.randn` generates numbers following standard normal distribtion."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OG-suj-PA72b",
        "colab_type": "code",
        "outputId": "6fda962e-bc37-4f93-9bdf-aa82b5502117",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 53
        }
      },
      "source": [
        "np.random.randn(2, 3) # Generate 2 * 3 random numbers"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[-0.52817175, -1.07296862,  0.86540763],\n",
              "       [-2.3015387 ,  1.74481176, -0.7612069 ]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SLw958olA9Uu",
        "colab_type": "text"
      },
      "source": [
        "`np.random.randint(low, high)` generates integers ranging from `low` (inclusive) to `high` (exclusive)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SkRVwD_yCd7r",
        "colab_type": "code",
        "outputId": "72c58ee8-4ed7-433e-8a95-60e58293f827",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 71
        }
      },
      "source": [
        "np.random.randint(low = 0, high = 4, size = (3, 3))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[3, 0, 2],\n",
              "       [0, 1, 2],\n",
              "       [2, 0, 3]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MLSvOE4UEA6n",
        "colab_type": "text"
      },
      "source": [
        "`np.random.choice` geneates random numbers following a given pmf."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NidDNwzK4FRY",
        "colab_type": "code",
        "outputId": "13962d5d-09fb-41cb-fe32-e90186c6bfcd",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 71
        }
      },
      "source": [
        "a = np.arange(4)\n",
        "p = [0.1, 0.2, 0.3, 0.4]\n",
        "np.random.choice(a = a, size=(3, 4), p = p)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[2, 2, 3, 3],\n",
              "       [3, 3, 0, 2],\n",
              "       [3, 3, 3, 1]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9Arc1wQq5a2V",
        "colab_type": "text"
      },
      "source": [
        "The sample space is not necessary to be number sets."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7JqVclm44hgH",
        "colab_type": "code",
        "outputId": "fc86099d-f55f-4d9c-f75f-930e91160710",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 71
        }
      },
      "source": [
        "a = ['Spade', 'Heart', 'Clud', 'Diamond']\n",
        "p = [0.25, 0.25, 0.25, 0.25]\n",
        "np.random.choice(a = a, size=(3, 4), p = p)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([['Spade', 'Clud', 'Clud', 'Clud'],\n",
              "       ['Heart', 'Spade', 'Heart', 'Spade'],\n",
              "       ['Diamond', 'Heart', 'Spade', 'Clud']], dtype='<U7')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "O7pLtNHP5XbL",
        "colab_type": "text"
      },
      "source": [
        "It is also possible to draw random samples from other distributions."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Wa7KH_rW6WaU",
        "colab_type": "text"
      },
      "source": [
        "Exponential: $f_X(x; \\beta) = \\frac{1}{\\beta}e^{-\\frac{x}{\\beta}}$, $\\beta$ is the scale parameter."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fstao_qt6Yn7",
        "colab_type": "code",
        "outputId": "e44f81bf-1d3f-4e52-c19b-969cd1da70e0",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 53
        }
      },
      "source": [
        "np.random.exponential(scale = 2, size = (2, 3))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[2.16136241, 0.70905534, 1.18166682],\n",
              "       [0.50237771, 0.15238928, 1.26688512]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cS-2YNh162s8",
        "colab_type": "text"
      },
      "source": [
        "Binomial: $P_X(k; n,p) = \\binom{n}{k} p^k (1 - p)^{n - k}$."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QCNVWxTx7Utf",
        "colab_type": "code",
        "outputId": "31fbcc4b-4710-4c67-9ffc-2921a5ddf7e7",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 71
        }
      },
      "source": [
        "n = 10\n",
        "p = 0.8\n",
        "np.random.binomial(n = n, p = p, size = (3, 3))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[10,  6,  9],\n",
              "       [ 8, 10,  6],\n",
              "       [ 6,  9,  8]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "e16J3-el71gV",
        "colab_type": "text"
      },
      "source": [
        "More distribution types are shown in the docs. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jkt_2jWu8DBe",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}

成功打開的 JSON 文件的元數據

 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.3 64-bit ('base': conda)",
   "language": "python",
   "name": "python_defaultSpec_1597947257101"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3-final"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

我通過從損壞的文件中刪除元數據解決了這個問題。 這是我刪除的元數據。

"metadata": {
    "colab": {
      "name": "numpy_random.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM