diff --git a/HW 5/Iris_knn_W2025_template.ipynb b/HW 5/Iris_knn_W2025_template.ipynb
index d128892..a443351 100644
--- a/HW 5/Iris_knn_W2025_template.ipynb
+++ b/HW 5/Iris_knn_W2025_template.ipynb
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": 7,
"metadata": {
"id": "Q6ifg03dKPR4"
},
@@ -19,7 +19,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 8,
"metadata": {
"id": "0ghtg7ecRQ50"
},
@@ -40,7 +40,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
@@ -107,13 +107,6 @@
"
0.2 | \n",
" \n",
" \n",
- " | 4 | \n",
- " 5.0 | \n",
- " 3.6 | \n",
- " 1.4 | \n",
- " 0.2 | \n",
- "
\n",
- " \n",
" | ... | \n",
" ... | \n",
" ... | \n",
@@ -121,13 +114,6 @@
" ... | \n",
"
\n",
" \n",
- " | 145 | \n",
- " 6.7 | \n",
- " 3.0 | \n",
- " 5.2 | \n",
- " 2.3 | \n",
- "
\n",
- " \n",
" | 146 | \n",
" 6.3 | \n",
" 2.5 | \n",
@@ -166,9 +152,7 @@
"1 4.9 3.0 1.4 0.2\n",
"2 4.7 3.2 1.3 0.2\n",
"3 4.6 3.1 1.5 0.2\n",
- "4 5.0 3.6 1.4 0.2\n",
".. ... ... ... ...\n",
- "145 6.7 3.0 5.2 2.3\n",
"146 6.3 2.5 5.0 1.9\n",
"147 6.5 3.0 5.2 2.0\n",
"148 6.2 3.4 5.4 2.3\n",
@@ -177,7 +161,7 @@
"[150 rows x 4 columns]"
]
},
- "execution_count": 4,
+ "execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -185,14 +169,14 @@
"source": [
"####################################\n",
"from sklearn.datasets import load_iris\n",
- "data = load_iris()\n",
+ "#data = load_iris()\n",
"df = pd.DataFrame(data.data, columns=data.feature_names)\n",
"df"
]
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 13,
"metadata": {
"id": "ANJhDRNPKPR5"
},
@@ -292,7 +276,7 @@
"4 5.0 3.6 1.4 0.2 setosa remove_me"
]
},
- "execution_count": 5,
+ "execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -312,7 +296,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 14,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
@@ -343,19 +327,23 @@
" \n",
" \n",
" | \n",
- " sepal length (cm) | \n",
- " sepal width (cm) | \n",
- " petal length (cm) | \n",
- " petal width (cm) | \n",
+ " sepallen | \n",
+ " sepalwid | \n",
+ " petallen | \n",
+ " petalwid | \n",
+ " irisname | \n",
+ " junk | \n",
"
\n",
" \n",
"
\n",
" \n",
" | 0 | \n",
- " 5.1 | \n",
+ " NaN | \n",
" 3.5 | \n",
" 1.4 | \n",
" 0.2 | \n",
+ " setosa | \n",
+ " remove_me | \n",
"
\n",
" \n",
" | 1 | \n",
@@ -363,6 +351,8 @@
" 3.0 | \n",
" 1.4 | \n",
" 0.2 | \n",
+ " setosa | \n",
+ " remove_me | \n",
"
\n",
" \n",
" | 2 | \n",
@@ -370,6 +360,8 @@
" 3.2 | \n",
" 1.3 | \n",
" 0.2 | \n",
+ " setosa | \n",
+ " remove_me | \n",
"
\n",
" \n",
" | 3 | \n",
@@ -377,6 +369,8 @@
" 3.1 | \n",
" 1.5 | \n",
" 0.2 | \n",
+ " setosa | \n",
+ " remove_me | \n",
"
\n",
" \n",
" | ... | \n",
@@ -384,6 +378,8 @@
" ... | \n",
" ... | \n",
" ... | \n",
+ " ... | \n",
+ " ... | \n",
"
\n",
" \n",
" | 146 | \n",
@@ -391,6 +387,8 @@
" 2.5 | \n",
" 5.0 | \n",
" 1.9 | \n",
+ " virginica | \n",
+ " remove_me | \n",
"
\n",
" \n",
" | 147 | \n",
@@ -398,6 +396,8 @@
" 3.0 | \n",
" 5.2 | \n",
" 2.0 | \n",
+ " virginica | \n",
+ " remove_me | \n",
"
\n",
" \n",
" | 148 | \n",
@@ -405,6 +405,8 @@
" 3.4 | \n",
" 5.4 | \n",
" 2.3 | \n",
+ " alieniris | \n",
+ " remove_me | \n",
"
\n",
" \n",
" | 149 | \n",
@@ -412,28 +414,30 @@
" 3.0 | \n",
" 5.1 | \n",
" 1.8 | \n",
+ " alieniris | \n",
+ " remove_me | \n",
"
\n",
" \n",
"\n",
- "150 rows × 4 columns
\n",
+ "150 rows × 6 columns
\n",
""
],
"text/plain": [
- " sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
- "0 5.1 3.5 1.4 0.2\n",
- "1 4.9 3.0 1.4 0.2\n",
- "2 4.7 3.2 1.3 0.2\n",
- "3 4.6 3.1 1.5 0.2\n",
- ".. ... ... ... ...\n",
- "146 6.3 2.5 5.0 1.9\n",
- "147 6.5 3.0 5.2 2.0\n",
- "148 6.2 3.4 5.4 2.3\n",
- "149 5.9 3.0 5.1 1.8\n",
+ " sepallen sepalwid petallen petalwid irisname junk\n",
+ "0 NaN 3.5 1.4 0.2 setosa remove_me\n",
+ "1 4.9 3.0 1.4 0.2 setosa remove_me\n",
+ "2 4.7 3.2 1.3 0.2 setosa remove_me\n",
+ "3 4.6 3.1 1.5 0.2 setosa remove_me\n",
+ ".. ... ... ... ... ... ...\n",
+ "146 6.3 2.5 5.0 1.9 virginica remove_me\n",
+ "147 6.5 3.0 5.2 2.0 virginica remove_me\n",
+ "148 6.2 3.4 5.4 2.3 alieniris remove_me\n",
+ "149 5.9 3.0 5.1 1.8 alieniris remove_me\n",
"\n",
- "[150 rows x 4 columns]"
+ "[150 rows x 6 columns]"
]
},
- "execution_count": 5,
+ "execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -455,7 +459,7 @@
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -470,15 +474,17 @@
"text": [
"\n",
"RangeIndex: 150 entries, 0 to 149\n",
- "Data columns (total 4 columns):\n",
- " # Column Non-Null Count Dtype \n",
- "--- ------ -------------- ----- \n",
- " 0 sepal length (cm) 150 non-null float64\n",
- " 1 sepal width (cm) 150 non-null float64\n",
- " 2 petal length (cm) 150 non-null float64\n",
- " 3 petal width (cm) 150 non-null float64\n",
- "dtypes: float64(4)\n",
- "memory usage: 4.8 KB\n"
+ "Data columns (total 6 columns):\n",
+ " # Column Non-Null Count Dtype \n",
+ "--- ------ -------------- ----- \n",
+ " 0 sepallen 149 non-null float64\n",
+ " 1 sepalwid 150 non-null float64\n",
+ " 2 petallen 150 non-null float64\n",
+ " 3 petalwid 149 non-null float64\n",
+ " 4 irisname 149 non-null str \n",
+ " 5 junk 150 non-null str \n",
+ "dtypes: float64(4), str(2)\n",
+ "memory usage: 7.2 KB\n"
]
}
],
@@ -495,7 +501,7 @@
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -510,15 +516,16 @@
"text": [
"\n",
"RangeIndex: 150 entries, 0 to 149\n",
- "Data columns (total 4 columns):\n",
- " # Column Non-Null Count Dtype \n",
- "--- ------ -------------- ----- \n",
- " 0 sepal length (cm) 150 non-null float64\n",
- " 1 sepal width (cm) 150 non-null float64\n",
- " 2 petal length (cm) 150 non-null float64\n",
- " 3 petal width (cm) 150 non-null float64\n",
- "dtypes: float64(4)\n",
- "memory usage: 4.8 KB\n"
+ "Data columns (total 5 columns):\n",
+ " # Column Non-Null Count Dtype \n",
+ "--- ------ -------------- ----- \n",
+ " 0 sepallen 149 non-null float64\n",
+ " 1 sepalwid 150 non-null float64\n",
+ " 2 petallen 150 non-null float64\n",
+ " 3 petalwid 149 non-null float64\n",
+ " 4 irisname 149 non-null str \n",
+ "dtypes: float64(4), str(1)\n",
+ "memory usage: 6.0 KB\n"
]
}
],
@@ -533,13 +540,13 @@
"# if you want a list of the column names use df.columns\n",
"#col5name = df.columns[5] # get column name at index 5\n",
"\n",
- "df_clean = df # drop by name is typical, but what else is possible?\n",
+ "df_clean = df.drop(columns=['junk']) # drop by name is typical, but what else is possible?\n",
"df_clean.info() # Is the bad last column gone?"
]
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -552,13 +559,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "FEATURES: Index(['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)',\n",
- " 'petal width (cm)'],\n",
- " dtype='str')\n",
+ "FEATURES: Index(['sepallen', 'sepalwid', 'petallen', 'petalwid', 'irisname'], dtype='str')\n",
"\n",
- "First feature: sepal length (cm)\n",
+ "First feature: sepallen\n",
"\n",
- "feature_name_to_index: {'sepal length (cm)': 0, 'sepal width (cm)': 1, 'petal length (cm)': 2, 'petal width (cm)': 3}\n"
+ "feature_name_to_index: {'sepallen': 0, 'sepalwid': 1, 'petallen': 2, 'petalwid': 3, 'irisname': 4}\n"
]
}
],
@@ -586,7 +591,7 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 20,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
@@ -602,50 +607,33 @@
"text": [
"\n",
"RangeIndex: 150 entries, 0 to 149\n",
- "Data columns (total 4 columns):\n",
- " # Column Non-Null Count Dtype \n",
- "--- ------ -------------- ----- \n",
- " 0 sepal length (cm) 150 non-null float64\n",
- " 1 sepal width (cm) 150 non-null float64\n",
- " 2 petal length (cm) 150 non-null float64\n",
- " 3 petal width (cm) 150 non-null float64\n",
- "dtypes: float64(4)\n",
- "memory usage: 4.8 KB\n",
+ "Data columns (total 5 columns):\n",
+ " # Column Non-Null Count Dtype \n",
+ "--- ------ -------------- ----- \n",
+ " 0 sepallen 149 non-null float64\n",
+ " 1 sepalwid 150 non-null float64\n",
+ " 2 petallen 150 non-null float64\n",
+ " 3 petalwid 149 non-null float64\n",
+ " 4 irisname 149 non-null str \n",
+ "dtypes: float64(4), str(1)\n",
+ "memory usage: 6.0 KB\n",
"======================================================================\n",
- " sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
- "0 5.1 3.5 1.4 0.2\n",
- "1 4.9 3.0 1.4 0.2\n",
- "2 4.7 3.2 1.3 0.2\n",
- "3 4.6 3.1 1.5 0.2\n",
- ".. ... ... ... ...\n",
- "146 6.3 2.5 5.0 1.9\n",
- "147 6.5 3.0 5.2 2.0\n",
- "148 6.2 3.4 5.4 2.3\n",
- "149 5.9 3.0 5.1 1.8\n",
+ " sepallen sepalwid petallen petalwid irisname\n",
+ "0 NaN 3.5 1.4 0.2 setosa\n",
+ "1 4.9 3.0 1.4 0.2 setosa\n",
+ "2 4.7 3.2 1.3 0.2 setosa\n",
+ "3 4.6 3.1 1.5 0.2 setosa\n",
+ ".. ... ... ... ... ...\n",
+ "146 6.3 2.5 5.0 1.9 virginica\n",
+ "147 6.5 3.0 5.2 2.0 virginica\n",
+ "148 6.2 3.4 5.4 2.3 alieniris\n",
+ "149 5.9 3.0 5.1 1.8 alieniris\n",
"\n",
- "[150 rows x 4 columns]\n",
- "======================================================================\n"
- ]
- },
- {
- "ename": "KeyError",
- "evalue": "'irisname'",
- "output_type": "error",
- "traceback": [
- "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
- "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)",
- "\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.14/site-packages/pandas/core/indexes/base.py:3641\u001b[39m, in \u001b[36mIndex.get_loc\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 3640\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m3641\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_engine\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcasted_key\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 3642\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n",
- "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/index.pyx:168\u001b[39m, in \u001b[36mpandas._libs.index.IndexEngine.get_loc\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m--> \u001b[39m\u001b[32m168\u001b[39m \u001b[33m'Could not get source, probably due dynamically evaluated source code.'\u001b[39m\n",
- "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/index.pyx:197\u001b[39m, in \u001b[36mpandas._libs.index.IndexEngine.get_loc\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m--> \u001b[39m\u001b[32m197\u001b[39m \u001b[33m'Could not get source, probably due dynamically evaluated source code.'\u001b[39m\n",
- "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/hashtable_class_helper.pxi:7668\u001b[39m, in \u001b[36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m7668\u001b[39m \u001b[33m'Could not get source, probably due dynamically evaluated source code.'\u001b[39m\n",
- "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/hashtable_class_helper.pxi:7676\u001b[39m, in \u001b[36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m7676\u001b[39m \u001b[33m'Could not get source, probably due dynamically evaluated source code.'\u001b[39m\n",
- "\u001b[31mKeyError\u001b[39m: 'irisname'",
- "\nThe above exception was the direct cause of the following exception:\n",
- "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)",
- "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 18\u001b[39m\n\u001b[32m 14\u001b[39m print(df_clean)\n\u001b[32m 15\u001b[39m print(\u001b[33m'='\u001b[39m * \u001b[32m70\u001b[39m)\n\u001b[32m 16\u001b[39m \n\u001b[32m 17\u001b[39m \u001b[38;5;66;03m# or more to the point, grab that column and inspect:\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m18\u001b[39m print(f\"Irisname entries: {df_clean[\u001b[33m'irisname'\u001b[39m].unique()}\")\n\u001b[32m 19\u001b[39m \n\u001b[32m 20\u001b[39m \u001b[38;5;66;03m# also note how the last two rows in the df have problems, among others...\u001b[39;00m\n",
- "\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.14/site-packages/pandas/core/frame.py:4378\u001b[39m, in \u001b[36mDataFrame.__getitem__\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 4374\u001b[39m \n\u001b[32m 4375\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m is_single_key:\n\u001b[32m 4376\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m self.columns.nlevels > \u001b[32m1\u001b[39m:\n\u001b[32m 4377\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m self._getitem_multilevel(key)\n\u001b[32m-> \u001b[39m\u001b[32m4378\u001b[39m indexer = self.columns.get_loc(key)\n\u001b[32m 4379\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m is_integer(indexer):\n\u001b[32m 4380\u001b[39m indexer = [indexer]\n\u001b[32m 4381\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n",
- "\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.14/site-packages/pandas/core/indexes/base.py:3648\u001b[39m, in \u001b[36mIndex.get_loc\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 3643\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(casted_key, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m (\n\u001b[32m 3644\u001b[39m \u001b[38;5;28misinstance\u001b[39m(casted_key, abc.Iterable)\n\u001b[32m 3645\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28many\u001b[39m(\u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m casted_key)\n\u001b[32m 3646\u001b[39m ):\n\u001b[32m 3647\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m InvalidIndexError(key) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01merr\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m3648\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01merr\u001b[39;00m\n\u001b[32m 3649\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[32m 3650\u001b[39m \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[32m 3651\u001b[39m \u001b[38;5;66;03m# InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[32m 3652\u001b[39m \u001b[38;5;66;03m# the TypeError.\u001b[39;00m\n\u001b[32m 3653\u001b[39m \u001b[38;5;28mself\u001b[39m._check_indexing_error(key)\n",
- "\u001b[31mKeyError\u001b[39m: 'irisname'"
+ "[150 rows x 5 columns]\n",
+ "======================================================================\n",
+ "Irisname entries: \n",
+ "['setosa', nan, 'versicolor', 'virginica', 'alieniris']\n",
+ "Length: 5, dtype: str\n"
]
}
],
@@ -674,7 +662,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -686,12 +674,10 @@
{
"data": {
"text/plain": [
- "Index(['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)',\n",
- " 'petal width (cm)'],\n",
- " dtype='object')"
+ "Index(['sepallen', 'sepalwid', 'petallen', 'petalwid', 'irisname', 'junk'], dtype='str')"
]
},
- "execution_count": 19,
+ "execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@@ -702,12 +688,157 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 22,
"metadata": {
"id": "xM22NWdqKPR-",
"scrolled": false
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "Index: 147 entries, 1 to 149\n",
+ "Data columns (total 5 columns):\n",
+ " # Column Non-Null Count Dtype \n",
+ "--- ------ -------------- ----- \n",
+ " 0 sepallen 147 non-null float64\n",
+ " 1 sepalwid 147 non-null float64\n",
+ " 2 petallen 147 non-null float64\n",
+ " 3 petalwid 147 non-null float64\n",
+ " 4 irisname 147 non-null str \n",
+ "dtypes: float64(4), str(1)\n",
+ "memory usage: 6.9 KB\n",
+ "======================================================================\n"
+ ]
+ },
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " sepallen | \n",
+ " sepalwid | \n",
+ " petallen | \n",
+ " petalwid | \n",
+ " irisname | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " 4.9 | \n",
+ " 3.0 | \n",
+ " 1.4 | \n",
+ " 0.2 | \n",
+ " setosa | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 4.7 | \n",
+ " 3.2 | \n",
+ " 1.3 | \n",
+ " 0.2 | \n",
+ " setosa | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 4.6 | \n",
+ " 3.1 | \n",
+ " 1.5 | \n",
+ " 0.2 | \n",
+ " setosa | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 5.0 | \n",
+ " 3.6 | \n",
+ " 1.4 | \n",
+ " 0.2 | \n",
+ " setosa | \n",
+ "
\n",
+ " \n",
+ " | ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " | 146 | \n",
+ " 6.3 | \n",
+ " 2.5 | \n",
+ " 5.0 | \n",
+ " 1.9 | \n",
+ " virginica | \n",
+ "
\n",
+ " \n",
+ " | 147 | \n",
+ " 6.5 | \n",
+ " 3.0 | \n",
+ " 5.2 | \n",
+ " 2.0 | \n",
+ " virginica | \n",
+ "
\n",
+ " \n",
+ " | 148 | \n",
+ " 6.2 | \n",
+ " 3.4 | \n",
+ " 5.4 | \n",
+ " 2.3 | \n",
+ " alieniris | \n",
+ "
\n",
+ " \n",
+ " | 149 | \n",
+ " 5.9 | \n",
+ " 3.0 | \n",
+ " 5.1 | \n",
+ " 1.8 | \n",
+ " alieniris | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
147 rows × 5 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " sepallen sepalwid petallen petalwid irisname\n",
+ "1 4.9 3.0 1.4 0.2 setosa\n",
+ "2 4.7 3.2 1.3 0.2 setosa\n",
+ "3 4.6 3.1 1.5 0.2 setosa\n",
+ "4 5.0 3.6 1.4 0.2 setosa\n",
+ ".. ... ... ... ... ...\n",
+ "146 6.3 2.5 5.0 1.9 virginica\n",
+ "147 6.5 3.0 5.2 2.0 virginica\n",
+ "148 6.2 3.4 5.4 2.3 alieniris\n",
+ "149 5.9 3.0 5.1 1.8 alieniris\n",
+ "\n",
+ "[147 rows x 5 columns]"
+ ]
+ },
+ "execution_count": 22,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"##############################\n",
"# BLOCK 9: USING DF'S dropna\n",
@@ -730,11 +861,167 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 23,
"metadata": {
"id": "h0spkxGzKPR-"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Irisname entries: \n",
+ "['setosa', 'versicolor', 'virginica', 'alieniris']\n",
+ "Length: 4, dtype: str\n",
+ "1 False\n",
+ "2 False\n",
+ "3 False\n",
+ "4 False\n",
+ " ... \n",
+ "146 False\n",
+ "147 False\n",
+ "148 True\n",
+ "149 True\n",
+ "Name: irisname, Length: 147, dtype: bool\n",
+ "1 True\n",
+ "2 True\n",
+ "3 True\n",
+ "4 True\n",
+ " ... \n",
+ "146 True\n",
+ "147 True\n",
+ "148 False\n",
+ "149 False\n",
+ "Name: irisname, Length: 147, dtype: bool\n",
+ "(145, 5)\n"
+ ]
+ },
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " sepallen | \n",
+ " sepalwid | \n",
+ " petallen | \n",
+ " petalwid | \n",
+ " irisname | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " 4.9 | \n",
+ " 3.0 | \n",
+ " 1.4 | \n",
+ " 0.2 | \n",
+ " setosa | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 4.7 | \n",
+ " 3.2 | \n",
+ " 1.3 | \n",
+ " 0.2 | \n",
+ " setosa | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 4.6 | \n",
+ " 3.1 | \n",
+ " 1.5 | \n",
+ " 0.2 | \n",
+ " setosa | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 5.0 | \n",
+ " 3.6 | \n",
+ " 1.4 | \n",
+ " 0.2 | \n",
+ " setosa | \n",
+ "
\n",
+ " \n",
+ " | ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " | 144 | \n",
+ " 6.7 | \n",
+ " 3.3 | \n",
+ " 5.7 | \n",
+ " 2.5 | \n",
+ " virginica | \n",
+ "
\n",
+ " \n",
+ " | 145 | \n",
+ " 6.7 | \n",
+ " 3.0 | \n",
+ " 5.2 | \n",
+ " 2.3 | \n",
+ " virginica | \n",
+ "
\n",
+ " \n",
+ " | 146 | \n",
+ " 6.3 | \n",
+ " 2.5 | \n",
+ " 5.0 | \n",
+ " 1.9 | \n",
+ " virginica | \n",
+ "
\n",
+ " \n",
+ " | 147 | \n",
+ " 6.5 | \n",
+ " 3.0 | \n",
+ " 5.2 | \n",
+ " 2.0 | \n",
+ " virginica | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
145 rows × 5 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " sepallen sepalwid petallen petalwid irisname\n",
+ "1 4.9 3.0 1.4 0.2 setosa\n",
+ "2 4.7 3.2 1.3 0.2 setosa\n",
+ "3 4.6 3.1 1.5 0.2 setosa\n",
+ "4 5.0 3.6 1.4 0.2 setosa\n",
+ ".. ... ... ... ... ...\n",
+ "144 6.7 3.3 5.7 2.5 virginica\n",
+ "145 6.7 3.0 5.2 2.3 virginica\n",
+ "146 6.3 2.5 5.0 1.9 virginica\n",
+ "147 6.5 3.0 5.2 2.0 virginica\n",
+ "\n",
+ "[145 rows x 5 columns]"
+ ]
+ },
+ "execution_count": 23,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"################################\n",
"# BLOCK 10: REMOVING BOGUS DATA\n",
@@ -748,7 +1035,7 @@
"# define a final version of the DataFrame by pulling out the\n",
"# bad alieniris data (remember that you can pass a boolean\n",
"# Series to select data that you want)\n",
- "df_final = pass\n",
+ "df_final = df_clean[df_clean['irisname'] != 'alieniris'].copy()\n",
"# ^^^^ YOU NEED TO WRITE CODE HERE...\n",
"\n",
"print(df_final.shape)\n",
@@ -757,11 +1044,23 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 24,
"metadata": {
"id": "EywtvRyMKPR-"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "{'setosa': 0, 'versicolor': 1, 'virginica': 2}\n",
+ "\n",
+ "setosa maps to 0\n",
+ "versicolor maps to 1\n",
+ "virginica maps to 2\n"
+ ]
+ }
+ ],
"source": [
"##########################################\n",
"# BLOCK 11: CONVERT SPECIES NAME TO INDEX\n",
@@ -788,11 +1087,30 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 28,
"metadata": {
"id": "NuKqemiiKPR_"
},
- "outputs": [],
+ "outputs": [
+ {
+ "ename": "KeyError",
+ "evalue": "0",
+ "output_type": "error",
+ "traceback": [
+ "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
+ "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)",
+ "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[28]\u001b[39m\u001b[32m, line 14\u001b[39m\n\u001b[32m 10\u001b[39m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[32m 11\u001b[39m \u001b[38;5;66;03m# >>> ADD CODE HERE TO KEEP THE WARNING FROM HAPPENING <<<\u001b[39;00m\n\u001b[32m 12\u001b[39m \n\u001b[32m 13\u001b[39m df_final[\u001b[33m'irisname'\u001b[39m] = df_final[\u001b[33m'irisname'\u001b[39m].apply(convertSpecies)\n\u001b[32m---> \u001b[39m\u001b[32m14\u001b[39m df_final.loc[:, \u001b[33m'irisname'\u001b[39m] = df_final[\u001b[33m'irisname'\u001b[39m].apply(convertSpecies)\n\u001b[32m 15\u001b[39m \u001b[38;5;66;03m# Don't run this twice! Why?! What's \"KeyError: 0\"?\u001b[39;00m\n\u001b[32m 16\u001b[39m \u001b[38;5;66;03m# (of course, you can always go back and re-establish definitions of df_final)\u001b[39;00m\n\u001b[32m 17\u001b[39m \n",
+ "\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.14/site-packages/pandas/core/series.py:5084\u001b[39m, in \u001b[36mSeries.apply\u001b[39m\u001b[34m(self, func, args, by_row, **kwargs)\u001b[39m\n\u001b[32m 4960\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mapply\u001b[39m(\n\u001b[32m 4961\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m 4962\u001b[39m func: AggFuncType,\n\u001b[32m (...)\u001b[39m\u001b[32m 4966\u001b[39m **kwargs,\n\u001b[32m 4967\u001b[39m ) -> DataFrame | Series:\n\u001b[32m 4968\u001b[39m \u001b[38;5;250m \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 4969\u001b[39m \u001b[33;03m Invoke function on values of Series.\u001b[39;00m\n\u001b[32m 4970\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 5076\u001b[39m \u001b[33;03m dtype: float64\u001b[39;00m\n\u001b[32m 5077\u001b[39m \u001b[33;03m \"\"\"\u001b[39;00m\n\u001b[32m 5078\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mSeriesApply\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 5079\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 5080\u001b[39m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5081\u001b[39m \u001b[43m \u001b[49m\u001b[43mby_row\u001b[49m\u001b[43m=\u001b[49m\u001b[43mby_row\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5082\u001b[39m \u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m=\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5083\u001b[39m \u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m=\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m-> \u001b[39m\u001b[32m5084\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m.\u001b[49m\u001b[43mapply\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
+ "\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.14/site-packages/pandas/core/apply.py:1520\u001b[39m, in \u001b[36mSeriesApply.apply\u001b[39m\u001b[34m(self)\u001b[39m\n\u001b[32m 1517\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m.apply_compat()\n\u001b[32m 1519\u001b[39m \u001b[38;5;66;03m# self.func is Callable\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m1520\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mapply_standard\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
+ "\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.14/site-packages/pandas/core/apply.py:1578\u001b[39m, in \u001b[36mSeriesApply.apply_standard\u001b[39m\u001b[34m(self)\u001b[39m\n\u001b[32m 1576\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 1577\u001b[39m curried = func\n\u001b[32m-> \u001b[39m\u001b[32m1578\u001b[39m mapped = \u001b[43mobj\u001b[49m\u001b[43m.\u001b[49m\u001b[43m_map_values\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmapper\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcurried\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 1580\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(mapped) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(mapped[\u001b[32m0\u001b[39m], ABCSeries):\n\u001b[32m 1581\u001b[39m \u001b[38;5;66;03m# GH#43986 Need to do list(mapped) in order to get treated as nested\u001b[39;00m\n\u001b[32m 1582\u001b[39m \u001b[38;5;66;03m# See also GH#25959 regarding EA support\u001b[39;00m\n\u001b[32m 1583\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m obj._constructor_expanddim(\u001b[38;5;28mlist\u001b[39m(mapped), index=obj.index)\n",
+ "\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.14/site-packages/pandas/core/base.py:1022\u001b[39m, in \u001b[36mIndexOpsMixin._map_values\u001b[39m\u001b[34m(self, mapper, na_action)\u001b[39m\n\u001b[32m 1019\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(arr, ExtensionArray):\n\u001b[32m 1020\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m arr.map(mapper, na_action=na_action)\n\u001b[32m-> \u001b[39m\u001b[32m1022\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43malgorithms\u001b[49m\u001b[43m.\u001b[49m\u001b[43mmap_array\u001b[49m\u001b[43m(\u001b[49m\u001b[43marr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmapper\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mna_action\u001b[49m\u001b[43m=\u001b[49m\u001b[43mna_action\u001b[49m\u001b[43m)\u001b[49m\n",
+ "\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.14/site-packages/pandas/core/algorithms.py:1710\u001b[39m, in \u001b[36mmap_array\u001b[39m\u001b[34m(arr, mapper, na_action)\u001b[39m\n\u001b[32m 1708\u001b[39m values = arr.astype(\u001b[38;5;28mobject\u001b[39m, copy=\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[32m 1709\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m na_action \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m1710\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mlib\u001b[49m\u001b[43m.\u001b[49m\u001b[43mmap_infer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalues\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmapper\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 1711\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 1712\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m lib.map_infer_mask(values, mapper, mask=isna(values).view(np.uint8))\n",
+ "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/lib.pyx:3071\u001b[39m, in \u001b[36mpandas._libs.lib.map_infer\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m3071\u001b[39m \u001b[33m'Could not get source, probably due dynamically evaluated source code.'\u001b[39m\n",
+ "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[24]\u001b[39m\u001b[32m, line 17\u001b[39m, in \u001b[36mconvertSpecies\u001b[39m\u001b[34m(species_name)\u001b[39m\n\u001b[32m 14\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m convertSpecies(species_name: str) -> int:\n\u001b[32m 15\u001b[39m \u001b[33m''' return the species index (a unique integer/category) '''\u001b[39m\n\u001b[32m 16\u001b[39m \u001b[38;5;66;03m#print(f\"converting {species_name}...\")\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m17\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m species_name_to_index[species_name]\n",
+ "\u001b[31mKeyError\u001b[39m: 0"
+ ]
+ }
+ ],
"source": [
"##########################################\n",
"# BLOCK 12: USING DF'S .apply\n",
@@ -805,8 +1123,9 @@
"# see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
"#\n",
"# >>> ADD CODE HERE TO KEEP THE WARNING FROM HAPPENING <<<\n",
- "df_final['irisname'] = df_final['irisname'].apply(convertSpecies)\n",
"\n",
+ "df_final['irisname'] = df_final['irisname'].apply(convertSpecies)\n",
+ "df_final.loc[:, 'irisname'] = df_final['irisname'].apply(convertSpecies)\n",
"# Don't run this twice! Why?! What's \"KeyError: 0\"?\n",
"# (of course, you can always go back and re-establish definitions of df_final)\n",
"\n"
@@ -814,11 +1133,137 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 29,
"metadata": {
"id": "tWoVKJBGKPR_"
},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " sepallen | \n",
+ " sepalwid | \n",
+ " petallen | \n",
+ " petalwid | \n",
+ " irisname | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " 4.9 | \n",
+ " 3.0 | \n",
+ " 1.4 | \n",
+ " 0.2 | \n",
+ " 0 | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 4.7 | \n",
+ " 3.2 | \n",
+ " 1.3 | \n",
+ " 0.2 | \n",
+ " 0 | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 4.6 | \n",
+ " 3.1 | \n",
+ " 1.5 | \n",
+ " 0.2 | \n",
+ " 0 | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 5.0 | \n",
+ " 3.6 | \n",
+ " 1.4 | \n",
+ " 0.2 | \n",
+ " 0 | \n",
+ "
\n",
+ " \n",
+ " | ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " | 144 | \n",
+ " 6.7 | \n",
+ " 3.3 | \n",
+ " 5.7 | \n",
+ " 2.5 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | 145 | \n",
+ " 6.7 | \n",
+ " 3.0 | \n",
+ " 5.2 | \n",
+ " 2.3 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | 146 | \n",
+ " 6.3 | \n",
+ " 2.5 | \n",
+ " 5.0 | \n",
+ " 1.9 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | 147 | \n",
+ " 6.5 | \n",
+ " 3.0 | \n",
+ " 5.2 | \n",
+ " 2.0 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
145 rows × 5 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " sepallen sepalwid petallen petalwid irisname\n",
+ "1 4.9 3.0 1.4 0.2 0\n",
+ "2 4.7 3.2 1.3 0.2 0\n",
+ "3 4.6 3.1 1.5 0.2 0\n",
+ "4 5.0 3.6 1.4 0.2 0\n",
+ ".. ... ... ... ... ...\n",
+ "144 6.7 3.3 5.7 2.5 2\n",
+ "145 6.7 3.0 5.2 2.3 2\n",
+ "146 6.3 2.5 5.0 1.9 2\n",
+ "147 6.5 3.0 5.2 2.0 2\n",
+ "\n",
+ "[145 rows x 5 columns]"
+ ]
+ },
+ "execution_count": 29,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"##########################################\n",
"# BLOCK 13: CONFIRMING FINAL DATAFRAME\n",
@@ -832,11 +1277,26 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 30,
"metadata": {
"id": "qxpBs7NnKPR_"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "type of A: \n",
+ " sepallen sepalwid petallen petalwid irisname\n",
+ "1 4.9 3.0 1.4 0.2 0\n",
+ "2 4.7 3.2 1.3 0.2 0\n",
+ "3 4.6 3.1 1.5 0.2 0\n",
+ "4 5.0 3.6 1.4 0.2 0\n",
+ "5 5.4 3.9 1.7 0.4 0\n",
+ "A[0] = [4.9 3. 1.4 0.2 0. ]\n"
+ ]
+ }
+ ],
"source": [
"##########################################\n",
"# BLOCK 14: CONVERTING TO NUMPY FORMAT\n",
@@ -856,11 +1316,166 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 31,
"metadata": {
"id": "bblbBuIiKPSA"
},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[4.9, 3. , 1.4, 0.2, 0. ],\n",
+ " [4.7, 3.2, 1.3, 0.2, 0. ],\n",
+ " [4.6, 3.1, 1.5, 0.2, 0. ],\n",
+ " [5. , 3.6, 1.4, 0.2, 0. ],\n",
+ " [5.4, 3.9, 1.7, 0.4, 0. ],\n",
+ " [4.6, 3.4, 1.4, 0.3, 0. ],\n",
+ " [4.4, 2.9, 1.4, 0.2, 0. ],\n",
+ " [4.9, 3.1, 1.5, 0.1, 0. ],\n",
+ " [5.4, 3.7, 1.5, 0.2, 0. ],\n",
+ " [4.8, 3.4, 1.6, 0.2, 0. ],\n",
+ " [4.8, 3. , 1.4, 0.1, 0. ],\n",
+ " [4.3, 3. , 1.1, 0.1, 0. ],\n",
+ " [5.8, 4. , 1.2, 0.2, 0. ],\n",
+ " [5.7, 4.4, 1.5, 0.4, 0. ],\n",
+ " [5.4, 3.9, 1.3, 0.4, 0. ],\n",
+ " [5.1, 3.5, 1.4, 0.3, 0. ],\n",
+ " [5.7, 3.8, 1.7, 0.3, 0. ],\n",
+ " [5.1, 3.8, 1.5, 0.3, 0. ],\n",
+ " [5.4, 3.4, 1.7, 0.2, 0. ],\n",
+ " [5.1, 3.7, 1.5, 0.4, 0. ],\n",
+ " [4.6, 3.6, 1. , 0.2, 0. ],\n",
+ " [5.1, 3.3, 1.7, 0.5, 0. ],\n",
+ " [4.8, 3.4, 1.9, 0.2, 0. ],\n",
+ " [5. , 3.4, 1.6, 0.4, 0. ],\n",
+ " [5.2, 3.5, 1.5, 0.2, 0. ],\n",
+ " [5.2, 3.4, 1.4, 0.2, 0. ],\n",
+ " [4.7, 3.2, 1.6, 0.2, 0. ],\n",
+ " [4.8, 3.1, 1.6, 0.2, 0. ],\n",
+ " [5.4, 3.4, 1.5, 0.4, 0. ],\n",
+ " [5.2, 4.1, 1.5, 0.1, 0. ],\n",
+ " [5.5, 4.2, 1.4, 0.2, 0. ],\n",
+ " [4.9, 3.1, 1.5, 0.2, 0. ],\n",
+ " [5. , 3.2, 1.2, 0.2, 0. ],\n",
+ " [5.5, 3.5, 1.3, 0.2, 0. ],\n",
+ " [4.9, 3.6, 1.4, 0.1, 0. ],\n",
+ " [4.4, 3. , 1.3, 0.2, 0. ],\n",
+ " [5.1, 3.4, 1.5, 0.2, 0. ],\n",
+ " [5. , 3.5, 1.3, 0.3, 0. ],\n",
+ " [4.5, 2.3, 1.3, 0.3, 0. ],\n",
+ " [4.4, 3.2, 1.3, 0.2, 0. ],\n",
+ " [5. , 3.5, 1.6, 0.6, 0. ],\n",
+ " [5.1, 3.8, 1.9, 0.4, 0. ],\n",
+ " [4.8, 3. , 1.4, 0.3, 0. ],\n",
+ " [5.1, 3.8, 1.6, 0.2, 0. ],\n",
+ " [4.6, 3.2, 1.4, 0.2, 0. ],\n",
+ " [5.3, 3.7, 1.5, 0.2, 0. ],\n",
+ " [5. , 3.3, 1.4, 0.2, 0. ],\n",
+ " [7. , 3.2, 4.7, 1.4, 1. ],\n",
+ " [6.4, 3.2, 4.5, 1.5, 1. ],\n",
+ " [6.9, 3.1, 4.9, 1.5, 1. ],\n",
+ " [5.5, 2.3, 4. , 1.3, 1. ],\n",
+ " [6.5, 2.8, 4.6, 1.5, 1. ],\n",
+ " [5.7, 2.8, 4.5, 1.3, 1. ],\n",
+ " [6.3, 3.3, 4.7, 1.6, 1. ],\n",
+ " [4.9, 2.4, 3.3, 1. , 1. ],\n",
+ " [6.6, 2.9, 4.6, 1.3, 1. ],\n",
+ " [5.2, 2.7, 3.9, 1.4, 1. ],\n",
+ " [5. , 2. , 3.5, 1. , 1. ],\n",
+ " [5.9, 3. , 4.2, 1.5, 1. ],\n",
+ " [6. , 2.2, 4. , 1. , 1. ],\n",
+ " [6.1, 2.9, 4.7, 1.4, 1. ],\n",
+ " [5.6, 2.9, 3.6, 1.3, 1. ],\n",
+ " [6.7, 3.1, 4.4, 1.4, 1. ],\n",
+ " [5.6, 3. , 4.5, 1.5, 1. ],\n",
+ " [5.8, 2.7, 4.1, 1. , 1. ],\n",
+ " [6.2, 2.2, 4.5, 1.5, 1. ],\n",
+ " [5.6, 2.5, 3.9, 1.1, 1. ],\n",
+ " [5.9, 3.2, 4.8, 1.8, 1. ],\n",
+ " [6.1, 2.8, 4. , 1.3, 1. ],\n",
+ " [6.3, 2.5, 4.9, 1.5, 1. ],\n",
+ " [6.1, 2.8, 4.7, 1.2, 1. ],\n",
+ " [6.4, 2.9, 4.3, 1.3, 1. ],\n",
+ " [6.6, 3. , 4.4, 1.4, 1. ],\n",
+ " [6.8, 2.8, 4.8, 1.4, 1. ],\n",
+ " [6.7, 3. , 5. , 1.7, 1. ],\n",
+ " [6. , 2.9, 4.5, 1.5, 1. ],\n",
+ " [5.7, 2.6, 3.5, 1. , 1. ],\n",
+ " [5.5, 2.4, 3.8, 1.1, 1. ],\n",
+ " [5.5, 2.4, 3.7, 1. , 1. ],\n",
+ " [5.8, 2.7, 3.9, 1.2, 1. ],\n",
+ " [6. , 2.7, 5.1, 1.6, 1. ],\n",
+ " [5.4, 3. , 4.5, 1.5, 1. ],\n",
+ " [6. , 3.4, 4.5, 1.6, 1. ],\n",
+ " [6.7, 3.1, 4.7, 1.5, 1. ],\n",
+ " [6.3, 2.3, 4.4, 1.3, 1. ],\n",
+ " [5.6, 3. , 4.1, 1.3, 1. ],\n",
+ " [5.5, 2.5, 4. , 1.3, 1. ],\n",
+ " [5.5, 2.6, 4.4, 1.2, 1. ],\n",
+ " [6.1, 3. , 4.6, 1.4, 1. ],\n",
+ " [5.8, 2.6, 4. , 1.2, 1. ],\n",
+ " [5. , 2.3, 3.3, 1. , 1. ],\n",
+ " [5.6, 2.7, 4.2, 1.3, 1. ],\n",
+ " [5.7, 3. , 4.2, 1.2, 1. ],\n",
+ " [5.7, 2.9, 4.2, 1.3, 1. ],\n",
+ " [6.2, 2.9, 4.3, 1.3, 1. ],\n",
+ " [5.1, 2.5, 3. , 1.1, 1. ],\n",
+ " [5.7, 2.8, 4.1, 1.3, 1. ],\n",
+ " [6.3, 3.3, 6. , 2.5, 2. ],\n",
+ " [5.8, 2.7, 5.1, 1.9, 2. ],\n",
+ " [7.1, 3. , 5.9, 2.1, 2. ],\n",
+ " [6.3, 2.9, 5.6, 1.8, 2. ],\n",
+ " [6.5, 3. , 5.8, 2.2, 2. ],\n",
+ " [7.6, 3. , 6.6, 2.1, 2. ],\n",
+ " [4.9, 2.5, 4.5, 1.7, 2. ],\n",
+ " [7.3, 2.9, 6.3, 1.8, 2. ],\n",
+ " [6.7, 2.5, 5.8, 1.8, 2. ],\n",
+ " [7.2, 3.6, 6.1, 2.5, 2. ],\n",
+ " [6.5, 3.2, 5.1, 2. , 2. ],\n",
+ " [6.4, 2.7, 5.3, 1.9, 2. ],\n",
+ " [6.8, 3. , 5.5, 2.1, 2. ],\n",
+ " [5.7, 2.5, 5. , 2. , 2. ],\n",
+ " [5.8, 2.8, 5.1, 2.4, 2. ],\n",
+ " [6.4, 3.2, 5.3, 2.3, 2. ],\n",
+ " [6.5, 3. , 5.5, 1.8, 2. ],\n",
+ " [7.7, 3.8, 6.7, 2.2, 2. ],\n",
+ " [7.7, 2.6, 6.9, 2.3, 2. ],\n",
+ " [6. , 2.2, 5. , 1.5, 2. ],\n",
+ " [6.9, 3.2, 5.7, 2.3, 2. ],\n",
+ " [5.6, 2.8, 4.9, 2. , 2. ],\n",
+ " [7.7, 2.8, 6.7, 2. , 2. ],\n",
+ " [6.3, 2.7, 4.9, 1.8, 2. ],\n",
+ " [6.7, 3.3, 5.7, 2.1, 2. ],\n",
+ " [7.2, 3.2, 6. , 1.8, 2. ],\n",
+ " [6.2, 2.8, 4.8, 1.8, 2. ],\n",
+ " [6.1, 3. , 4.9, 1.8, 2. ],\n",
+ " [6.4, 2.8, 5.6, 2.1, 2. ],\n",
+ " [7.2, 3. , 5.8, 1.6, 2. ],\n",
+ " [7.4, 2.8, 6.1, 1.9, 2. ],\n",
+ " [7.9, 3.8, 6.4, 2. , 2. ],\n",
+ " [6.4, 2.8, 5.6, 2.2, 2. ],\n",
+ " [6.3, 2.8, 5.1, 1.5, 2. ],\n",
+ " [6.1, 2.6, 5.6, 1.4, 2. ],\n",
+ " [7.7, 3. , 6.1, 2.3, 2. ],\n",
+ " [6.3, 3.4, 5.6, 2.4, 2. ],\n",
+ " [6.4, 3.1, 5.5, 1.8, 2. ],\n",
+ " [6. , 3. , 4.8, 1.8, 2. ],\n",
+ " [6.9, 3.1, 5.4, 2.1, 2. ],\n",
+ " [6.7, 3.1, 5.6, 2.4, 2. ],\n",
+ " [6.9, 3.1, 5.1, 2.3, 2. ],\n",
+ " [5.8, 2.7, 5.1, 1.9, 2. ],\n",
+ " [6.8, 3.2, 5.9, 2.3, 2. ],\n",
+ " [6.7, 3.3, 5.7, 2.5, 2. ],\n",
+ " [6.7, 3. , 5.2, 2.3, 2. ],\n",
+ " [6.3, 2.5, 5. , 1.9, 2. ],\n",
+ " [6.5, 3. , 5.2, 2. , 2. ]])"
+ ]
+ },
+ "execution_count": 31,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"##################################################\n",
"# BLOCK 15: CONVERTING NUMPY ARRAY TO ALL FLOATS\n",
@@ -875,11 +1490,165 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 32,
"metadata": {
"id": "wbns1qpJKPSA"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "The dataset has 145 rows and 5 cols\n",
+ "[[4.9 3. 1.4 0.2 0. ]\n",
+ " [4.7 3.2 1.3 0.2 0. ]\n",
+ " [4.6 3.1 1.5 0.2 0. ]\n",
+ " [5. 3.6 1.4 0.2 0. ]\n",
+ " [5.4 3.9 1.7 0.4 0. ]\n",
+ " [4.6 3.4 1.4 0.3 0. ]\n",
+ " [4.4 2.9 1.4 0.2 0. ]\n",
+ " [4.9 3.1 1.5 0.1 0. ]\n",
+ " [5.4 3.7 1.5 0.2 0. ]\n",
+ " [4.8 3.4 1.6 0.2 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3 5.7 2.1 2. ]\n",
+ " [7.2 3.2 6. 1.8 2. ]\n",
+ " [6.2 2.8 4.8 1.8 2. ]\n",
+ " [6.1 3. 4.9 1.8 2. ]\n",
+ " [6.4 2.8 5.6 2.1 2. ]\n",
+ " [7.2 3. 5.8 1.6 2. ]\n",
+ " [7.4 2.8 6.1 1.9 2. ]\n",
+ " [7.9 3.8 6.4 2. 2. ]\n",
+ " [6.4 2.8 5.6 2.2 2. ]\n",
+ " [6.3 2.8 5.1 1.5 2. ]\n",
+ " [6.1 2.6 5.6 1.4 2. ]\n",
+ " [7.7 3. 6.1 2.3 2. ]\n",
+ " [6.3 3.4 5.6 2.4 2. ]\n",
+ " [6.4 3.1 5.5 1.8 2. ]\n",
+ " [6. 3. 4.8 1.8 2. ]\n",
+ " [6.9 3.1 5.4 2.1 2. ]\n",
+ " [6.7 3.1 5.6 2.4 2. ]\n",
+ " [6.9 3.1 5.1 2.3 2. ]\n",
+ " [5.8 2.7 5.1 1.9 2. ]\n",
+ " [6.8 3.2 5.9 2.3 2. ]\n",
+ " [6.7 3.3 5.7 2.5 2. ]\n",
+ " [6.7 3. 5.2 2.3 2. ]\n",
+ " [6.3 2.5 5. 1.9 2. ]\n",
+ " [6.5 3. 5.2 2. 2. ]]\n"
+ ]
+ }
+ ],
"source": [
"##########################################\n",
"# BLOCK 16: USING NUMPY'S .shape\n",
@@ -895,12 +1664,25 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 33,
"metadata": {
"id": "ixiZhsuqKPSA",
"scrolled": true
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "flower #132 data is [7.7 3. 6.1 2.3 2. ]\n",
+ " Its sepallen is 7.7\n",
+ " Its sepalwid is 3.0\n",
+ " Its petallen is 6.1\n",
+ " Its petalwid is 2.3\n",
+ " Its irisname is virginica (2)\n"
+ ]
+ }
+ ],
"source": [
"##########################################\n",
"# BLOCK 17: PRINTING FLOWER INFO\n",
@@ -924,11 +1706,19 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 34,
"metadata": {
"id": "izGld2tSKPSA"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "I predict versicolor (1) from features [4.6, 3.6, 3.0, 1.2]\n"
+ ]
+ }
+ ],
"source": [
"##########################################\n",
"# BLOCK 18: WRITING OUR OWN 1-NN FUNCTION\n",
@@ -985,7 +1775,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 35,
"metadata": {
"id": "9nhDlGbRKPSB"
},
@@ -1006,11 +1796,174 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 36,
"metadata": {
"id": "64kSAiCvKPSB"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "+++ Start of data definitions +++\n",
+ "\n",
+ "X_all (just features) is \n",
+ " [[4.9 3. 1.4 0.2]\n",
+ " [4.7 3.2 1.3 0.2]\n",
+ " [4.6 3.1 1.5 0.2]\n",
+ " [5. 3.6 1.4 0.2]\n",
+ " [5.4 3.9 1.7 0.4]\n",
+ " [4.6 3.4 1.4 0.3]\n",
+ " [4.4 2.9 1.4 0.2]\n",
+ " [4.9 3.1 1.5 0.1]\n",
+ " [5.4 3.7 1.5 0.2]\n",
+ " [4.8 3.4 1.6 0.2]\n",
+ " [4.8 3. 1.4 0.1]\n",
+ " [4.3 3. 1.1 0.1]\n",
+ " [5.8 4. 1.2 0.2]\n",
+ " [5.7 4.4 1.5 0.4]\n",
+ " [5.4 3.9 1.3 0.4]\n",
+ " [5.1 3.5 1.4 0.3]\n",
+ " [5.7 3.8 1.7 0.3]\n",
+ " [5.1 3.8 1.5 0.3]\n",
+ " [5.4 3.4 1.7 0.2]\n",
+ " [5.1 3.7 1.5 0.4]\n",
+ " [4.6 3.6 1. 0.2]\n",
+ " [5.1 3.3 1.7 0.5]\n",
+ " [4.8 3.4 1.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1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
+ " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
+ " 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.\n",
+ " 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.\n",
+ " 2.]\n"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 20: DEFINIING FEATURES & LABELS FOR kNN\n",
@@ -1027,11 +1980,22 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 37,
"metadata": {
"id": "2ChEk2CDKPSC"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Weighting sepallen by 1.0\n",
+ "Weighting sepalwid by 1.0\n",
+ "Weighting petallen by 1.0\n",
+ "Weighting petalwid by 1.0\n"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 21: REWEIGHTING FEATURES\n",
@@ -1058,11 +2022,170 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 38,
"metadata": {
"id": "sTDi2mFnKPSC"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[[7.7 2.6 6.9 2.3]\n",
+ " [4.3 3. 1.1 0.1]\n",
+ " [5.4 3.7 1.5 0.2]\n",
+ " [7.2 3.6 6.1 2.5]\n",
+ " [6.3 3.3 6. 2.5]\n",
+ " [5.7 2.6 3.5 1. ]\n",
+ " [4.8 3.4 1.9 0.2]\n",
+ " [6.7 3.1 4.7 1.5]\n",
+ " [6.1 3. 4.9 1.8]\n",
+ " [6.9 3.1 5.4 2.1]\n",
+ "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6.7 3.3 5.7 2.5]\n",
+ " [5.1 3.8 1.6 0.2]\n",
+ " [5.7 2.5 5. 2. ]\n",
+ " [5.2 2.7 3.9 1.4]\n",
+ " [5.6 3. 4.1 1.3]]\n",
+ "[2. 0. 0. 2. 2. 1. 0. 1. 2. 2. 0. 2. 1. 2. 0. 2. 1. 0. 2. 0. 2. 0. 1. 0.\n",
+ " 0. 0. 1. 2. 1. 1. 0. 2. 0. 2. 0. 0. 2. 0. 1. 1. 0. 0. 0. 0. 0. 0. 1. 0.\n",
+ " 0. 2. 2. 1. 2. 2. 0. 2. 0. 1. 0. 0. 0. 1. 2. 2. 1. 1. 1. 1. 1. 2. 1. 0.\n",
+ " 1. 1. 0. 1. 0. 0. 2. 1. 2. 2. 1. 1. 2. 1. 1. 0. 0. 0. 1. 0. 0. 2. 2. 1.\n",
+ " 2. 1. 0. 2. 2. 1. 0. 1. 2. 1. 1. 0. 2. 2. 2. 0. 0. 1. 2. 1. 2. 2. 2. 2.\n",
+ " 1. 2. 2. 1. 2. 1. 0. 1. 1. 0. 1. 1. 0. 2. 2. 1. 1. 1. 2. 1. 2. 0. 2. 1.\n",
+ " 1.]\n"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 22: PERMUTING THE DATA\n",
@@ -1083,11 +2206,20 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 39,
"metadata": {
"id": "_pD7IZR1KPSC"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "total rows: 145; training with 116 rows; testing with 29 rows\n",
+ "\t(sanity check: 116 + 29 = 145)\n"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 23: DEFINING TRAIN VS TEST SETS\n",
@@ -1122,11 +2254,19 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 40,
"metadata": {
"id": "jvJMYC3eKPSD"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Created and trained a knn classifier with k = 84\n"
+ ]
+ }
+ ],
"source": [
"#######################################################\n",
"# BLOCK 24: FIRST ATTEMPT TO BUILD & TRAIN A kNN MODEL\n",
@@ -1149,11 +2289,24 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 41,
"metadata": {
"id": "d3cB0xryKPSD"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Predicted labels: [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
+ " 1. 1. 1. 1. 1.]\n",
+ "Actual labels : [2. 0. 0. 2. 2. 1. 0. 1. 2. 2. 0. 2. 1. 2. 0. 2. 1. 0. 2. 0. 2. 0. 1. 0.\n",
+ " 0. 0. 1. 2. 1.]\n",
+ "\n",
+ "Results on test set: 7 correct out of 29 total.\n"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 25: TEST THE kNN MODEL\n",
@@ -1181,11 +2334,59 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 42,
"metadata": {
"id": "liXBT5-IKPSD"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "row 0 : versicolor virginica incorrect\n",
+ "row 1 : versicolor setosa incorrect\n",
+ "row 2 : versicolor setosa incorrect\n",
+ "row 3 : versicolor virginica incorrect\n",
+ "row 4 : versicolor virginica incorrect\n",
+ "row 5 : versicolor versicolor \n",
+ "row 6 : versicolor setosa incorrect\n",
+ "row 7 : versicolor versicolor \n",
+ "row 8 : versicolor virginica incorrect\n",
+ "row 9 : versicolor virginica incorrect\n",
+ "row 10 : versicolor setosa incorrect\n",
+ "row 11 : versicolor virginica incorrect\n",
+ "row 12 : versicolor versicolor \n",
+ "row 13 : versicolor virginica incorrect\n",
+ "row 14 : versicolor setosa incorrect\n",
+ "row 15 : versicolor virginica incorrect\n",
+ "row 16 : versicolor versicolor \n",
+ "row 17 : versicolor setosa incorrect\n",
+ "row 18 : versicolor virginica incorrect\n",
+ "row 19 : versicolor setosa incorrect\n",
+ "row 20 : versicolor virginica incorrect\n",
+ "row 21 : versicolor setosa incorrect\n",
+ "row 22 : versicolor versicolor \n",
+ "row 23 : versicolor setosa incorrect\n",
+ "row 24 : versicolor setosa incorrect\n",
+ "row 25 : versicolor setosa incorrect\n",
+ "row 26 : versicolor versicolor \n",
+ "row 27 : versicolor virginica incorrect\n",
+ "row 28 : versicolor versicolor \n",
+ "\n",
+ "Correct: 7 out of 29\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "7"
+ ]
+ },
+ "execution_count": 42,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 26: PRETTY-PRINT PREDICTED LABELS\n",
@@ -1228,11 +2429,20 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 43,
"metadata": {
"id": "h_ZigXq2KPSE"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "predicted_species = [1.]\n",
+ "I predict versicolor (1) from features [6.7, 3.3, 5.7, 2.1]\n"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 27: USE THE FIRST-ATTEMPT kNN MODEL\n",
@@ -1272,7 +2482,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 44,
"metadata": {
"id": "kPByHBBnKPSE"
},
@@ -1296,11 +2506,30 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 45,
"metadata": {
"id": "ZUmotr7hKPSE"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "k: 1 cv accuracy: 0.9482\n"
+ ]
+ },
+ {
+ "ename": "NameError",
+ "evalue": "name 'best_accuracy' is not defined",
+ "output_type": "error",
+ "traceback": [
+ "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
+ "\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
+ "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[45]\u001b[39m\u001b[32m, line 28\u001b[39m\n\u001b[32m 24\u001b[39m cv_scores = cross_val_score(knn_cv_model, X_train, y_train, cv=\u001b[32m5\u001b[39m)\n\u001b[32m 25\u001b[39m this_cv_accuracy = cv_scores.mean()\n\u001b[32m 26\u001b[39m print(f\"k: {k:2d} cv accuracy: {this_cv_accuracy:7.4f}\")\n\u001b[32m 27\u001b[39m all_accuracies.append(this_cv_accuracy)\n\u001b[32m---> \u001b[39m\u001b[32m28\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m this_cv_accuracy > best_accuracy:\n\u001b[32m 29\u001b[39m best_accuracy = this_cv_accuracy\n\u001b[32m 30\u001b[39m best_k = k\n\u001b[32m 31\u001b[39m \u001b[38;5;66;03m# assign best value of k to best_k\u001b[39;00m\n",
+ "\u001b[31mNameError\u001b[39m: name 'best_accuracy' is not defined"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 29: USING CROSS VALIDATION\n",
@@ -1324,12 +2553,14 @@
"all_accuracies = []\n",
"\n",
"for k in range(1, max_k, 1):\n",
- " knn_cv_model = KNeighborsClassifier(n_neighbors = k) # build knn_model for every k!\n",
- " cv_scores = cross_val_score( knn_cv_model, X_train, y_train, cv = 5 ) # 5 means 80/20 split\n",
- " this_cv_accuracy = cv_scores.mean() # mean() is numpy's built-in average function\n",
+ " knn_cv_model = KNeighborsClassifier(n_neighbors=k)\n",
+ " cv_scores = cross_val_score(knn_cv_model, X_train, y_train, cv=5)\n",
+ " this_cv_accuracy = cv_scores.mean()\n",
" print(f\"k: {k:2d} cv accuracy: {this_cv_accuracy:7.4f}\")\n",
" all_accuracies.append(this_cv_accuracy)\n",
- "\n",
+ " if this_cv_accuracy > best_accuracy:\n",
+ " best_accuracy = this_cv_accuracy\n",
+ " best_k = k\n",
"# assign best value of k to best_k\n",
"best_k = k # *** AT THE MOMENT THIS IS INCORRECT ***\n",
"# you'll need to modify the loop above to find and remember the real best_k\n",
@@ -1346,11 +2577,23 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 46,
"metadata": {
"id": "pJW6c8XdKPSF"
},
- "outputs": [],
+ "outputs": [
+ {
+ "ename": "NameError",
+ "evalue": "name 'best_k' is not defined",
+ "output_type": "error",
+ "traceback": [
+ "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
+ "\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
+ "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[46]\u001b[39m\u001b[32m, line 11\u001b[39m\n\u001b[32m 7\u001b[39m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[32m 8\u001b[39m \u001b[38;5;66;03m# Now, using best_k instead of the original, randomly-guessed value How does it do?!\u001b[39;00m\n\u001b[32m 9\u001b[39m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[32m 10\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m sklearn.neighbors \u001b[38;5;28;01mimport\u001b[39;00m KNeighborsClassifier\n\u001b[32m---> \u001b[39m\u001b[32m11\u001b[39m knn_model_tuned = KNeighborsClassifier(n_neighbors = best_k) \u001b[38;5;66;03m# here, we use the best_k\u001b[39;00m\n\u001b[32m 12\u001b[39m \n\u001b[32m 13\u001b[39m \u001b[38;5;66;03m# we train the model (one line!)\u001b[39;00m\n\u001b[32m 14\u001b[39m knn_model_tuned.fit(X_train, y_train) \u001b[38;5;66;03m# yay! trained!\u001b[39;00m\n",
+ "\u001b[31mNameError\u001b[39m: name 'best_k' is not defined"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 30: RE-BUILD & RE-TRAIN USING BEST k\n",
@@ -1371,11 +2614,23 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 47,
"metadata": {
"id": "9h5-1TukKPSF"
},
- "outputs": [],
+ "outputs": [
+ {
+ "ename": "NameError",
+ "evalue": "name 'knn_model_tuned' is not defined",
+ "output_type": "error",
+ "traceback": [
+ "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
+ "\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
+ "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[47]\u001b[39m\u001b[32m, line 8\u001b[39m\n\u001b[32m 4\u001b[39m \n\u001b[32m 5\u001b[39m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[32m 6\u001b[39m \u001b[38;5;66;03m# Re-create and re-run the \"Model-testing Cell\" How does it do with best_k?!\u001b[39;00m\n\u001b[32m 7\u001b[39m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m8\u001b[39m predicted_labels = knn_model_tuned.predict(X_test)\n\u001b[32m 9\u001b[39m actual_labels = y_test\n\u001b[32m 10\u001b[39m \n\u001b[32m 11\u001b[39m \u001b[38;5;66;03m# Let's print them so we can compare...\u001b[39;00m\n",
+ "\u001b[31mNameError\u001b[39m: name 'knn_model_tuned' is not defined"
+ ]
+ }
+ ],
"source": [
"################################################\n",
"# BLOCK 31: RE-TEST THE BEST-k MODEL\n",
@@ -1503,11 +2758,41 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 48,
"metadata": {
"id": "MlnIrLPWRQ58"
},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " sepallen sepalwid petallen petalwid irisname\n",
+ "50 7.0 3.2 4.7 1.4 1\n",
+ "51 6.4 3.2 4.5 1.5 1\n",
+ "52 6.9 3.1 4.9 1.5 1\n",
+ "53 5.5 2.3 4.0 1.3 1\n",
+ ".. ... ... ... ... ...\n",
+ "96 5.7 2.9 4.2 1.3 1\n",
+ "97 6.2 2.9 4.3 1.3 1\n",
+ "98 5.1 2.5 3.0 1.1 1\n",
+ "99 5.7 2.8 4.1 1.3 1\n",
+ "\n",
+ "[50 rows x 5 columns]\n",
+ "======================================================================\n",
+ "sepallen 5.936\n",
+ "sepalwid 2.770\n",
+ "petallen 4.260\n",
+ "petalwid 1.326\n",
+ "irisname 1.000\n",
+ "dtype: float64\n",
+ "======================================================================\n",
+ "[5.936 2.77 4.26 1.326]\n",
+ "predicted_species = [1.]\n",
+ "Mean versicolor features [5.936 2.77 4.26 1.326] -> versicolor (1)\n"
+ ]
+ }
+ ],
"source": [
"################################################################\n",
"# BLOCK 35b: HOW DOES THE MODEL PERFORM ON MEAN OF EACH FLOWER?\n",
@@ -1529,7 +2814,9 @@
"# now run the predictive model (see Block 34 above) and print a corresponding\n",
"# message\n",
"\n",
- "# >>> YOU ADD CODE HERE"
+ "# >>> YOU ADD CODE HERE\n",
+ "result = predictiveModel(versicolor_features_means)\n",
+ "print(f\"Mean versicolor features {versicolor_features_means} -> {result}\")"
]
},
{
@@ -1552,7 +2839,12 @@
"# (c) call the predictive model using those features\n",
"# (d) print a corresponding message\n",
"\n",
- "# >> YOU ADD CODE HERE -- YOU SHOULD NEED NO MORE THAN 5 LINES OF CODE"
+ "# >> YOU ADD CODE HERE -- YOU SHOULD NEED NO MORE THAN 5 LINES OF CODE\n",
+ "for name in species_names:\n",
+ " species_data = df_final[df_final['irisname'] == convertSpecies(name)]\n",
+ " species_features_means = np.asarray(species_data.mean()[:-1])\n",
+ " result = predictiveModel(species_features_means)\n",
+ " print(f\"Mean {name} features {species_features_means} -> {result}\")"
]
}
],