{
  "id": 402620,
  "title": "Newbie Question about Inferring Model in second notebook",
  "url": "/competitions/predict-student-performance-from-game-play/discussion/402620",
  "author_name": "",
  "post_date": "2023-04-19T03:27:16.676227800Z",
  "votes": 1,
  "comment_count": 5,
  "views": 0,
  "content": "<p>I'm trying to load in my models into my second notebook using a simple for loop like this:</p>\n<p>modelling_list = {}<br>\nmodel_list  = XGBClassifier()</p>\n<p>for t in range(1,19):</p>\n<pre><code>modelling_list = model_list.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\") \n</code></pre>\n<p>print(modelling_list)</p>\n<p>But it doesn't end up saving anything into the list after I run the code. I have attached an image of the outputs I'm trying to read in, and it works if I just load one model (e.g. \"/kaggle/input/student-performance-notebook/XGB_question1.xgb\"). I'm an R user by trade, so if I'm messing up some Python syntax please let me know. Thanks for any help. </p>",
  "messages": [
    {
      "id": "2226564",
      "postDate": "04/19/2023 03:27:16",
      "content": "<p>I'm trying to load in my models into my second notebook using a simple for loop like this:</p>\n<p>modelling_list = {}<br>\nmodel_list  = XGBClassifier()</p>\n<p>for t in range(1,19):</p>\n<pre><code>modelling_list = model_list.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\") \n</code></pre>\n<p>print(modelling_list)</p>\n<p>But it doesn't end up saving anything into the list after I run the code. I have attached an image of the outputs I'm trying to read in, and it works if I just load one model (e.g. \"/kaggle/input/student-performance-notebook/XGB_question1.xgb\"). I'm an R user by trade, so if I'm messing up some Python syntax please let me know. Thanks for any help. </p>",
      "rawMarkdown": "I'm trying to load in my models into my second notebook using a simple for loop like this:\n\nmodelling_list = {}\nmodel_list  = XGBClassifier()\n\nfor t in range(1,19):\n\n    modelling_list = model_list.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\") \n    \n\nprint(modelling_list)\n\n\nBut it doesn't end up saving anything into the list after I run the code. I have attached an image of the outputs I'm trying to read in, and it works if I just load one model (e.g. \"/kaggle/input/student-performance-notebook/XGB_question1.xgb\"). I'm an R user by trade, so if I'm messing up some Python syntax please let me know. Thanks for any help.",
      "votes": null
    },
    {
      "id": "2226582",
      "postDate": "04/19/2023 03:52:02",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/paulmerica\" target=\"_blank\">@paulmerica</a> if you want to load multiple XGBClassifier models, you should use a list to store them. Here's what it's supposed to look like:</p>\n<pre><code>\nmodel_list = []\n t  (, ):\n    \n    model = XGBClassifier()\n    model.load_model()\n    model_list.append(model)\n\n\nprediction = model_list[].predict(\n(prediction)\n</code></pre>\n<p>Hope this helps ✌️<br>\n(Extra: You can also save <a href=\"https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/398500#2202834\" target=\"_blank\">XGB in JSON format</a>, work fine as well) </p>",
      "rawMarkdown": "Hi @paulmerica if you want to load multiple XGBClassifier models, you should use a list to store them. Here's what it's supposed to look like:\n\n```python\n# Save each model to list\nmodel_list = []\nfor t in range(1, 19):\n    # Includes XGBClassifier() inside so it loads the new model on every loop\n    model = XGBClassifier()\n    model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n    model_list.append(model)\n\n# Call model with prefer number\nprediction = model_list[0].predict(###your target feature###)\nprint(prediction)\n```\n\nHope this helps ✌️\n(Extra: You can also save [XGB in JSON format](https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/398500#2202834), work fine as well)",
      "votes": null
    },
    {
      "id": "2226584",
      "postDate": "04/19/2023 03:53:55",
      "content": "<p>I had the same question and I have documented what worked for me here. <a href=\"https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178\" target=\"_blank\">https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178</a></p>",
      "rawMarkdown": "I had the same question and I have documented what worked for me here. https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178",
      "votes": null
    },
    {
      "id": "2227396",
      "postDate": "04/19/2023 18:25:00",
      "content": "<blockquote>\n  <p>Hi <a href=\"https://www.kaggle.com/paulmerica\" target=\"_blank\">@paulmerica</a> if you want to load multiple XGBClassifier models, you should use a list to store them. Here's what it's supposed to look like:</p>\n<pre><code>\nmodel_list = []\n t  (, ):\n    \n    model = XGBClassifier()\n    model.load_model()\n    model_list.append(model)\n\n\nprediction = model_list[].predict(\n(prediction)\n</code></pre>\n  <p>Hope this helps ✌️<br>\n  (Extra: You can also save <a href=\"https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/398500#2202834\" target=\"_blank\">XGB in JSON format</a>, work fine as well)</p>\n</blockquote>\n<p>This solved my problem. Thanks so much for the help, I'm new to python so using .append was not something I knew I needed to use. Thanks again! I might end up making an inference notebook to help others soon. </p>",
      "rawMarkdown": "> Hi @paulmerica if you want to load multiple XGBClassifier models, you should use a list to store them. Here's what it's supposed to look like:\n> \n> ```python\n> # Save each model to list\n> model_list = []\n> for t in range(1, 19):\n>     # Includes XGBClassifier() inside so it loads the new model on every loop\n>     model = XGBClassifier()\n>     model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n>     model_list.append(model)\n> \n> # Call model with prefer number\n> prediction = model_list[0].predict(###your target feature###)\n> print(prediction)\n> ```\n> \n> Hope this helps ✌️\n> (Extra: You can also save [XGB in JSON format](https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/398500#2202834), work fine as well)\n\nThis solved my problem. Thanks so much for the help, I'm new to python so using .append was not something I knew I needed to use. Thanks again! I might end up making an inference notebook to help others soon.",
      "votes": null
    },
    {
      "id": "2227397",
      "postDate": "04/19/2023 18:25:20",
      "content": "<blockquote>\n  <p>I had the same question and I have documented what worked for me here. <a href=\"https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178\" target=\"_blank\">https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178</a></p>\n</blockquote>\n<p>Hey I got this part, but the other user answered my question above. </p>",
      "rawMarkdown": "> I had the same question and I have documented what worked for me here. https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178\n\nHey I got this part, but the other user answered my question above.",
      "votes": null
    },
    {
      "id": "2229528",
      "postDate": "04/21/2023 12:56:01",
      "content": "<p>To load multiple XGBClassifier models, use a list to store them. You can use a for loop to iterate over each model, load it into the list and then access it later on like this:</p>\n<pre><code>models = []\nfor t in range(1, 19):\n    model = XGBClassifier()\n    model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n    models.append(model)\n\ny_pred = np.mean(np.stack([model.predict(df) for model in models]), axis = 0)\n</code></pre>\n<p>This will load all models and take the avg of their predictions as the final prediction.</p>\n<p>Let me know if there is anything else you need.</p>\n<p>The Devastator.</p>",
      "rawMarkdown": "To load multiple XGBClassifier models, use a list to store them. You can use a for loop to iterate over each model, load it into the list and then access it later on like this:\n\n```\nmodels = []\nfor t in range(1, 19):\n    model = XGBClassifier()\n    model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n    models.append(model)\n\ny_pred = np.mean(np.stack([model.predict(df) for model in models]), axis = 0)\n``` \n\nThis will load all models and take the avg of their predictions as the final prediction.\n\nLet me know if there is anything else you need.\n\nThe Devastator.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2226582,
      "author_name": "thaweewatboy",
      "author_url": "",
      "post_date": "04/19/2023 03:52:02",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/paulmerica\" target=\"_blank\">@paulmerica</a> if you want to load multiple XGBClassifier models, you should use a list to store them. Here's what it's supposed to look like:</p>\n<pre><code>\nmodel_list = []\n t  (, ):\n    \n    model = XGBClassifier()\n    model.load_model()\n    model_list.append(model)\n\n\nprediction = model_list[].predict(\n(prediction)\n</code></pre>\n<p>Hope this helps ✌️<br>\n(Extra: You can also save <a href=\"https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/398500#2202834\" target=\"_blank\">XGB in JSON format</a>, work fine as well) </p>",
      "votes": null,
      "replies": [
        {
          "id": 2227396,
          "author_name": "paulmerica",
          "author_url": "",
          "post_date": "04/19/2023 18:25:00",
          "content": "<blockquote>\n  <p>Hi <a href=\"https://www.kaggle.com/paulmerica\" target=\"_blank\">@paulmerica</a> if you want to load multiple XGBClassifier models, you should use a list to store them. Here's what it's supposed to look like:</p>\n<pre><code>\nmodel_list = []\n t  (, ):\n    \n    model = XGBClassifier()\n    model.load_model()\n    model_list.append(model)\n\n\nprediction = model_list[].predict(\n(prediction)\n</code></pre>\n  <p>Hope this helps ✌️<br>\n  (Extra: You can also save <a href=\"https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/398500#2202834\" target=\"_blank\">XGB in JSON format</a>, work fine as well)</p>\n</blockquote>\n<p>This solved my problem. Thanks so much for the help, I'm new to python so using .append was not something I knew I needed to use. Thanks again! I might end up making an inference notebook to help others soon. </p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 2226584,
      "author_name": "spacehorse",
      "author_url": "",
      "post_date": "04/19/2023 03:53:55",
      "content": "<p>I had the same question and I have documented what worked for me here. <a href=\"https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178\" target=\"_blank\">https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 2227397,
          "author_name": "paulmerica",
          "author_url": "",
          "post_date": "04/19/2023 18:25:20",
          "content": "<blockquote>\n  <p>I had the same question and I have documented what worked for me here. <a href=\"https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178\" target=\"_blank\">https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178</a></p>\n</blockquote>\n<p>Hey I got this part, but the other user answered my question above. </p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 2229528,
      "author_name": "thedevastator",
      "author_url": "",
      "post_date": "04/21/2023 12:56:01",
      "content": "<p>To load multiple XGBClassifier models, use a list to store them. You can use a for loop to iterate over each model, load it into the list and then access it later on like this:</p>\n<pre><code>models = []\nfor t in range(1, 19):\n    model = XGBClassifier()\n    model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n    models.append(model)\n\ny_pred = np.mean(np.stack([model.predict(df) for model in models]), axis = 0)\n</code></pre>\n<p>This will load all models and take the avg of their predictions as the final prediction.</p>\n<p>Let me know if there is anything else you need.</p>\n<p>The Devastator.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2226564": "I'm trying to load in my models into my second notebook using a simple for loop like this:\n\nmodelling_list = {}\nmodel_list  = XGBClassifier()\n\nfor t in range(1,19):\n\n    modelling_list = model_list.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\") \n    \n\nprint(modelling_list)\n\n\nBut it doesn't end up saving anything into the list after I run the code. I have attached an image of the outputs I'm trying to read in, and it works if I just load one model (e.g. \"/kaggle/input/student-performance-notebook/XGB_question1.xgb\"). I'm an R user by trade, so if I'm messing up some Python syntax please let me know. Thanks for any help.",
    "2226582": "Hi @paulmerica if you want to load multiple XGBClassifier models, you should use a list to store them. Here's what it's supposed to look like:\n\n```python\n# Save each model to list\nmodel_list = []\nfor t in range(1, 19):\n    # Includes XGBClassifier() inside so it loads the new model on every loop\n    model = XGBClassifier()\n    model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n    model_list.append(model)\n\n# Call model with prefer number\nprediction = model_list[0].predict(###your target feature###)\nprint(prediction)\n```\n\nHope this helps ✌️\n(Extra: You can also save [XGB in JSON format](https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/398500#2202834), work fine as well)",
    "2226584": "I had the same question and I have documented what worked for me here. https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178",
    "2227396": "> Hi @paulmerica if you want to load multiple XGBClassifier models, you should use a list to store them. Here's what it's supposed to look like:\n> \n> ```python\n> # Save each model to list\n> model_list = []\n> for t in range(1, 19):\n>     # Includes XGBClassifier() inside so it loads the new model on every loop\n>     model = XGBClassifier()\n>     model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n>     model_list.append(model)\n> \n> # Call model with prefer number\n> prediction = model_list[0].predict(###your target feature###)\n> print(prediction)\n> ```\n> \n> Hope this helps ✌️\n> (Extra: You can also save [XGB in JSON format](https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/398500#2202834), work fine as well)\n\nThis solved my problem. Thanks so much for the help, I'm new to python so using .append was not something I knew I needed to use. Thanks again! I might end up making an inference notebook to help others soon.",
    "2227397": "> I had the same question and I have documented what worked for me here. https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/402178\n\nHey I got this part, but the other user answered my question above.",
    "2229528": "To load multiple XGBClassifier models, use a list to store them. You can use a for loop to iterate over each model, load it into the list and then access it later on like this:\n\n```\nmodels = []\nfor t in range(1, 19):\n    model = XGBClassifier()\n    model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n    models.append(model)\n\ny_pred = np.mean(np.stack([model.predict(df) for model in models]), axis = 0)\n``` \n\nThis will load all models and take the avg of their predictions as the final prediction.\n\nLet me know if there is anything else you need.\n\nThe Devastator."
  },
  "source": "meta"
}