{
  "id": 397572,
  "title": "KNN predict only 1 as output",
  "url": "/competitions/digit-recognizer/discussion/397572",
  "author_name": "Bolu Vishal",
  "post_date": "2023-03-26T07:35:08.528000",
  "votes": 0,
  "comment_count": 1,
  "views": null,
  "content": "<p>Can anyone help me with my notebook? I am trying to train a basic KNN classifier using grid search, but the output on the test set is always coming out as 1.</p>\n<p><a href=\"https://www.kaggle.com/masterbolu/mnist-using-knn\" target=\"_blank\">https://www.kaggle.com/masterbolu/mnist-using-knn</a></p>",
  "messages": [
    {
      "id": 2218440,
      "postDate": "2023-04-11T17:13:03.633Z",
      "content": "<p>The issue you're encountering is due to the use of the non-normalized dataset in the <code>train_test_split</code> function. In the code block below, you're using the non-normalized version (<code>x_train_full</code>) instead of the normalized version (<code>x_train</code>):</p>\n<pre><code>x_train_full = train_df.drop('label',axis=1)\ny_train_full = train_df['label']\n\nx_train = x_train_full.astype('float32') / 255\nx_test = test_df.astype('float32') / 255\n\nx_train, x_val, y_train, y_val = train_test_split(x_train_full, y_train_full, test_size=.1, random_state=42)\n</code></pre>\n<p>As a result, your model is trained on the non-normalized dataset during the training step:</p>\n<pre><code>param = {\n    'n_neighbors':[3,4,5],\n    'weights':['uniform','distance']\n}\n\nknn = KNeighborsClassifier()\n\ngrid_search = GridSearchCV(knn, param, cv=5, verbose=2, n_jobs=1)\n\ngrid_search.fit(x_train, y_train)\nprint(grid_search.best_params_)\n</code></pre>\n<p>However, you're using the normalized version of the dataset for test predictions:</p>\n<pre><code>res = best_knn.predict(x_test)\n</code></pre>\n<p>To resolve this issue, you can either remove the <code>.astype('float32') / 255</code> part from the line <code>x_test = test_df.astype('float32') / 255</code>, or use the normalized version of the dataset for the <code>train_test_split</code>.</p>",
      "rawMarkdown": "The issue you're encountering is due to the use of the non-normalized dataset in the `train_test_split` function. In the code block below, you're using the non-normalized version (`x_train_full`) instead of the normalized version (`x_train`):\n\n```\nx_train_full = train_df.drop('label',axis=1)\ny_train_full = train_df['label']\n\nx_train = x_train_full.astype('float32') / 255\nx_test = test_df.astype('float32') / 255\n\nx_train, x_val, y_train, y_val = train_test_split(x_train_full, y_train_full, test_size=.1, random_state=42)\n```\n\nAs a result, your model is trained on the non-normalized dataset during the training step:\n\n```\nparam = {\n    'n_neighbors':[3,4,5],\n    'weights':['uniform','distance']\n}\n\nknn = KNeighborsClassifier()\n\ngrid_search = GridSearchCV(knn, param, cv=5, verbose=2, n_jobs=1)\n\ngrid_search.fit(x_train, y_train)\nprint(grid_search.best_params_)\n```\n\nHowever, you're using the normalized version of the dataset for test predictions:\n\n```\nres = best_knn.predict(x_test)\n```\n\nTo resolve this issue, you can either remove the `.astype('float32') / 255` part from the line `x_test = test_df.astype('float32') / 255`, or use the normalized version of the dataset for the `train_test_split`.\n",
      "votes": 1
    },
    {
      "id": 2197500,
      "postDate": "2023-03-26T07:35:08.527Z",
      "content": "<p>Can anyone help me with my notebook? I am trying to train a basic KNN classifier using grid search, but the output on the test set is always coming out as 1.</p>\n<p><a href=\"https://www.kaggle.com/masterbolu/mnist-using-knn\" target=\"_blank\">https://www.kaggle.com/masterbolu/mnist-using-knn</a></p>",
      "rawMarkdown": "Can anyone help me with my notebook? I am trying to train a basic KNN classifier using grid search, but the output on the test set is always coming out as 1.\n\n\nhttps://www.kaggle.com/masterbolu/mnist-using-knn"
    }
  ],
  "comments": [
    {
      "id": 2218440,
      "author_name": "402349-ชญานนท์",
      "author_url": "",
      "post_date": "2023-04-11T17:13:03.633000",
      "content": "<p>The issue you're encountering is due to the use of the non-normalized dataset in the <code>train_test_split</code> function. In the code block below, you're using the non-normalized version (<code>x_train_full</code>) instead of the normalized version (<code>x_train</code>):</p>\n<pre><code>x_train_full = train_df.drop('label',axis=1)\ny_train_full = train_df['label']\n\nx_train = x_train_full.astype('float32') / 255\nx_test = test_df.astype('float32') / 255\n\nx_train, x_val, y_train, y_val = train_test_split(x_train_full, y_train_full, test_size=.1, random_state=42)\n</code></pre>\n<p>As a result, your model is trained on the non-normalized dataset during the training step:</p>\n<pre><code>param = {\n    'n_neighbors':[3,4,5],\n    'weights':['uniform','distance']\n}\n\nknn = KNeighborsClassifier()\n\ngrid_search = GridSearchCV(knn, param, cv=5, verbose=2, n_jobs=1)\n\ngrid_search.fit(x_train, y_train)\nprint(grid_search.best_params_)\n</code></pre>\n<p>However, you're using the normalized version of the dataset for test predictions:</p>\n<pre><code>res = best_knn.predict(x_test)\n</code></pre>\n<p>To resolve this issue, you can either remove the <code>.astype('float32') / 255</code> part from the line <code>x_test = test_df.astype('float32') / 255</code>, or use the normalized version of the dataset for the <code>train_test_split</code>.</p>",
      "votes": 1,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2218440": "The issue you're encountering is due to the use of the non-normalized dataset in the `train_test_split` function. In the code block below, you're using the non-normalized version (`x_train_full`) instead of the normalized version (`x_train`):\n\n```\nx_train_full = train_df.drop('label',axis=1)\ny_train_full = train_df['label']\n\nx_train = x_train_full.astype('float32') / 255\nx_test = test_df.astype('float32') / 255\n\nx_train, x_val, y_train, y_val = train_test_split(x_train_full, y_train_full, test_size=.1, random_state=42)\n```\n\nAs a result, your model is trained on the non-normalized dataset during the training step:\n\n```\nparam = {\n    'n_neighbors':[3,4,5],\n    'weights':['uniform','distance']\n}\n\nknn = KNeighborsClassifier()\n\ngrid_search = GridSearchCV(knn, param, cv=5, verbose=2, n_jobs=1)\n\ngrid_search.fit(x_train, y_train)\nprint(grid_search.best_params_)\n```\n\nHowever, you're using the normalized version of the dataset for test predictions:\n\n```\nres = best_knn.predict(x_test)\n```\n\nTo resolve this issue, you can either remove the `.astype('float32') / 255` part from the line `x_test = test_df.astype('float32') / 255`, or use the normalized version of the dataset for the `train_test_split`.\n",
    "2197500": "Can anyone help me with my notebook? I am trying to train a basic KNN classifier using grid search, but the output on the test set is always coming out as 1.\n\n\nhttps://www.kaggle.com/masterbolu/mnist-using-knn"
  }
}