简体   繁体   中英

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

I can no longer open a ipynb file in a VS-Code jupyter notebook. I have a problem with this one particular file as my other files open without a problem. I think that the problem comes from an error in the metadata as the metadata in this file looks very different to the metadata structure of the other files that successfully open.

This file used to open without any glitch in the past but now when I try to open the file, I see the following error:

Command failed: 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

Source: Python(Extension)

My understanding is that VS Code cannot find the designated python interpreter, which in turn prevents the kernel from being activated.(I'm not very experienced so please correct me if I'm wrong!).

I have tried copy-pasting the python interpreter from the file that works into the corrupted file but that has not worked.

Please see below a copy of the corrupted JSON file and also an example of the metadata structure of a ipynb file that opens successfully.

All help on this is greatly appreciated!

Many thanks, Tony

Corrupted JSON File

{
  "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": []
    }
  ]
}

Metadata of JSON file that opens successfully

 "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
}

I resolved the issue by deleting the metadata from the corrupted file. here is the metadata that I deleted.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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