Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"wine_dfshape[0]\n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wine_dfshape is not defined.

]
},
{
Expand All @@ -114,7 +114,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"wine_dfshape[1]"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same issue as before, wine_dfshape is not defined so it doesn't understand what you're trying to access.

]
},
{
Expand All @@ -132,7 +132,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"print(wine_df['class'].dtype, wine_df['class'].unique())"
]
},
{
Expand All @@ -151,7 +151,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"wine_dfshape[1]-1"
]
},
{
Expand Down Expand Up @@ -204,7 +204,7 @@
"id": "403ef0bb",
"metadata": {},
"source": [
"> Your answer here..."
"> For uniform and fair comparision of data in same context"
]
},
{
Expand All @@ -220,7 +220,7 @@
"id": "fdee5a15",
"metadata": {},
"source": [
"> Your answer here..."
"> as this is the response class which we want to find"
]
},
{
Expand All @@ -236,7 +236,7 @@
"id": "f0676c21",
"metadata": {},
"source": [
"> Your answer here..."
"> seed value used so that sequence of random numbers is same each time. same seed value, guarantees the same results each time. random seed number is fine, nothing particular is needed"
]
},
{
Expand All @@ -261,7 +261,10 @@
"\n",
"# split the data into a training and testing set. hint: use train_test_split !\n",
"\n",
"# Your code here ..."
"A = predictors_standardized\n",
"B = wine_df['class']\n",
"A_train, A_test, B_train, B_test = train_test_split(A, B, test_size=0.25,stratify=wine_df['class'])\n"
"print(A_train.shape, A_test.shape)\n"
]
},
{
Expand Down Expand Up @@ -289,7 +292,14 @@
"metadata": {},
"outputs": [],
"source": [
"# Your code here..."
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.model_selection import GridSearchCV\n",
"knn = KNeighborsClassifier() \n",
"param_grid = {\"n_neighbors\": list(range(1, 51))}\n",
"grid_search = GridSearchCV(knn, param_grid, cv=10, scoring='accuracy')\n",
"grid_search.fit(A_train, B_train)\n",
"best_k = grid_search.best_params_['n_neighbors']\n",
"print(f'Best k value:{best_k}')"
]
},
{
Expand All @@ -310,7 +320,13 @@
"metadata": {},
"outputs": [],
"source": [
"# Your code here..."
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.metrics import accuracy_score\n",
"final_knn = KNeighborsClassifier(n_neighbors=best_k)\n",
"final_knn.fit(A_train, B_train)\n",
"B_pred = final_knn.predict(A_test)\n",
"accuracy = accuracy_score(A_test, B_pred)\n",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is how accuracy_score is supposed to be used?

"print(f'Test Set Accuracy: {accuracy}')"
]
},
{
Expand Down