noshot 6.0.0__py3-none-any.whl → 8.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- noshot/data/ML TS XAI/ML/CNN(Image_for_Folders_5).ipynb +201 -0
- noshot/data/ML TS XAI/ML/CNN(Image_form_Folder_2).ipynb +201 -0
- noshot/data/ML TS XAI/ML/ML 1/1. EDA-PCA (Balance Scale Dataset).ipynb +147 -0
- noshot/data/ML TS XAI/ML/ML 1/1. EDA-PCA (Rice Dataset).ipynb +181 -0
- noshot/data/ML TS XAI/ML/ML 1/10. HMM Veterbi.ipynb +152 -0
- noshot/data/ML TS XAI/ML/ML 1/2. KNN (Balance Scale Dataset).ipynb +117 -0
- noshot/data/ML TS XAI/ML/ML 1/2. KNN (Iris Dataset).ipynb +156 -0
- noshot/data/ML TS XAI/ML/ML 1/2. KNN (Sobar-72 Dataset).ipynb +215 -0
- noshot/data/ML TS XAI/ML/ML 1/3. LDA (Balance Scale Dataset).ipynb +78 -0
- noshot/data/ML TS XAI/ML/ML 1/3. LDA (NPHA Doctor Visits Dataset).ipynb +114 -0
- noshot/data/ML TS XAI/ML/ML 1/4. Linear Regression (Machine Dataset).ipynb +115 -0
- noshot/data/ML TS XAI/ML/ML 1/4. Linear Regression (Real Estate Dataset).ipynb +146 -0
- noshot/data/ML TS XAI/ML/ML 1/5. Logistic Regression (Magic04 Dataset).ipynb +130 -0
- noshot/data/ML TS XAI/ML/ML 1/5. Logistic Regression (Wine Dataset).ipynb +112 -0
- noshot/data/ML TS XAI/ML/ML 1/6. Naive Bayes Classifier (Agaricus Lepiota Dataset).ipynb +118 -0
- noshot/data/ML TS XAI/ML/ML 1/6. Naive Bayes Classifier (Wine Dataset).ipynb +89 -0
- noshot/data/ML TS XAI/ML/ML 1/7. SVM (Rice Dataset).ipynb +120 -0
- noshot/data/ML TS XAI/ML/ML 1/8. FeedForward NN (Sobar72 Dataset).ipynb +262 -0
- noshot/data/ML TS XAI/ML/ML 1/9. CNN (Cifar10 Dataset).ipynb +156 -0
- noshot/data/ML TS XAI/ML/ML 2/1. PCA.ipynb +162 -0
- noshot/data/ML TS XAI/ML/ML 2/10. CNN.ipynb +100 -0
- noshot/data/ML TS XAI/ML/ML 2/11. HMM.ipynb +336 -0
- noshot/data/ML TS XAI/ML/ML 2/2. KNN.ipynb +149 -0
- noshot/data/ML TS XAI/ML/ML 2/3. LDA.ipynb +132 -0
- noshot/data/ML TS XAI/ML/ML 2/4. Linear Regression.ipynb +86 -0
- noshot/data/ML TS XAI/ML/ML 2/5. Logistic Regression.ipynb +115 -0
- noshot/data/ML TS XAI/ML/ML 2/6. Naive Bayes (Titanic).ipynb +196 -0
- noshot/data/ML TS XAI/ML/ML 2/6. Naive Bayes (Wine).ipynb +98 -0
- noshot/data/ML TS XAI/ML/ML 2/7. SVM Linear.ipynb +109 -0
- noshot/data/ML TS XAI/ML/ML 2/8. SVM Non-Linear.ipynb +195 -0
- noshot/data/ML TS XAI/ML/ML 2/9. FNN With Regularization.ipynb +189 -0
- noshot/data/ML TS XAI/ML/ML 2/9. FNN Without Regularization.ipynb +197 -0
- noshot/data/ML TS XAI/ML/ML 2/All in One Lab CIA 1 Q.ipynb +1087 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/1. PCA EDA.ipynb +274 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/10. CNN.ipynb +170 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/11. HMM 2.ipynb +1087 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/11. HMM 3.ipynb +178 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/11. HMM 4.ipynb +185 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/11. HMM.ipynb +106 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/2. KNN.ipynb +177 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/3. LDA.ipynb +195 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/4. Linear Regression.ipynb +267 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/5. Logistic Regression.ipynb +104 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/6. Bayesian Classifier.ipynb +109 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/7. SVM.ipynb +220 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/8. MLP.ipynb +99 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/9. Ridge - Lasso.ipynb +211 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/9. Ridge Lasso 2.ipynb +99 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/Image Load Example.ipynb +118 -0
- noshot/data/ML TS XAI/ML/ML 3 (Latest)/Updated_Untitled.ipynb +603 -0
- noshot/data/ML TS XAI/ML/Rolls Royce AllinOne.ipynb +691 -0
- {noshot-6.0.0.dist-info → noshot-8.0.0.dist-info}/METADATA +1 -1
- noshot-8.0.0.dist-info/RECORD +60 -0
- {noshot-6.0.0.dist-info → noshot-8.0.0.dist-info}/WHEEL +1 -1
- noshot/data/ML TS XAI/XAI/Q1.ipynb +0 -377
- noshot/data/ML TS XAI/XAI/Q2.ipynb +0 -362
- noshot/data/ML TS XAI/XAI/Q3.ipynb +0 -637
- noshot/data/ML TS XAI/XAI/Q4.ipynb +0 -206
- noshot/data/ML TS XAI/XAI/Q5.ipynb +0 -1018
- noshot-6.0.0.dist-info/RECORD +0 -14
- {noshot-6.0.0.dist-info → noshot-8.0.0.dist-info}/licenses/LICENSE.txt +0 -0
- {noshot-6.0.0.dist-info → noshot-8.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,181 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": null,
|
6
|
+
"id": "d27dd480-bff5-4179-8b5d-145b15fe2527",
|
7
|
+
"metadata": {},
|
8
|
+
"outputs": [],
|
9
|
+
"source": [
|
10
|
+
"import numpy as np\n",
|
11
|
+
"import pandas as pd\n",
|
12
|
+
"import seaborn as sns\n",
|
13
|
+
"import scipy\n",
|
14
|
+
"import matplotlib.pyplot as plt"
|
15
|
+
]
|
16
|
+
},
|
17
|
+
{
|
18
|
+
"cell_type": "code",
|
19
|
+
"execution_count": null,
|
20
|
+
"id": "c34c5236-de26-47a7-b047-c084519f6ef7",
|
21
|
+
"metadata": {},
|
22
|
+
"outputs": [],
|
23
|
+
"source": [
|
24
|
+
"data, _ = scipy.io.arff.loadarff('data/rice.arff')\n",
|
25
|
+
"df = pd.DataFrame(data)\n",
|
26
|
+
"print(\"Shape:\", df.shape)\n",
|
27
|
+
"df.head()"
|
28
|
+
]
|
29
|
+
},
|
30
|
+
{
|
31
|
+
"cell_type": "code",
|
32
|
+
"execution_count": null,
|
33
|
+
"id": "9ef13fe2-6f7f-4e9e-97bf-f44c0b10694e",
|
34
|
+
"metadata": {},
|
35
|
+
"outputs": [],
|
36
|
+
"source": [
|
37
|
+
"df.describe()"
|
38
|
+
]
|
39
|
+
},
|
40
|
+
{
|
41
|
+
"cell_type": "code",
|
42
|
+
"execution_count": null,
|
43
|
+
"id": "0a51ae34-5fa4-4f78-a08c-de89bcc1f6b2",
|
44
|
+
"metadata": {},
|
45
|
+
"outputs": [],
|
46
|
+
"source": [
|
47
|
+
"df.info()"
|
48
|
+
]
|
49
|
+
},
|
50
|
+
{
|
51
|
+
"cell_type": "code",
|
52
|
+
"execution_count": null,
|
53
|
+
"id": "3a30c508-273f-46cd-8ecd-8361b255488b",
|
54
|
+
"metadata": {},
|
55
|
+
"outputs": [],
|
56
|
+
"source": [
|
57
|
+
"df.isnull().sum()"
|
58
|
+
]
|
59
|
+
},
|
60
|
+
{
|
61
|
+
"cell_type": "code",
|
62
|
+
"execution_count": null,
|
63
|
+
"id": "f0651a71-0bc1-489e-ad5f-7b080a9cb978",
|
64
|
+
"metadata": {},
|
65
|
+
"outputs": [],
|
66
|
+
"source": [
|
67
|
+
"sns.countplot(df, x='Class', hue='Class')\n",
|
68
|
+
"plt.title(\"Class Distribution\")\n",
|
69
|
+
"plt.show()"
|
70
|
+
]
|
71
|
+
},
|
72
|
+
{
|
73
|
+
"cell_type": "code",
|
74
|
+
"execution_count": null,
|
75
|
+
"id": "e2d1c93f-e097-4b08-b9ff-48c7d5fd9659",
|
76
|
+
"metadata": {},
|
77
|
+
"outputs": [],
|
78
|
+
"source": [
|
79
|
+
"print(\"Mean of Features\")\n",
|
80
|
+
"df.iloc[:, :-1].mean()"
|
81
|
+
]
|
82
|
+
},
|
83
|
+
{
|
84
|
+
"cell_type": "code",
|
85
|
+
"execution_count": null,
|
86
|
+
"id": "0977bb62-192e-43f4-8cd9-e054ea3526b9",
|
87
|
+
"metadata": {},
|
88
|
+
"outputs": [],
|
89
|
+
"source": [
|
90
|
+
"cov = df.iloc[:, :-1].cov().round(3)\n",
|
91
|
+
"print(\"Covariance Matrix\")\n",
|
92
|
+
"cov"
|
93
|
+
]
|
94
|
+
},
|
95
|
+
{
|
96
|
+
"cell_type": "code",
|
97
|
+
"execution_count": null,
|
98
|
+
"id": "e2862d9a-76b9-4367-a135-0fc36ae60478",
|
99
|
+
"metadata": {},
|
100
|
+
"outputs": [],
|
101
|
+
"source": [
|
102
|
+
"eigen_vals, eigen_vecs = np.linalg.eig(cov)\n",
|
103
|
+
"mapping = {round(eigen_vals[i], 2): eigen_vecs[:, i].round(2) \n",
|
104
|
+
" for i in range(len(eigen_vals))}\n",
|
105
|
+
"print(\"Eigen Value-Vector Pairs:\")\n",
|
106
|
+
"mapping"
|
107
|
+
]
|
108
|
+
},
|
109
|
+
{
|
110
|
+
"cell_type": "code",
|
111
|
+
"execution_count": null,
|
112
|
+
"id": "06a3c671-6cae-44c7-9960-56702042c0e5",
|
113
|
+
"metadata": {},
|
114
|
+
"outputs": [],
|
115
|
+
"source": [
|
116
|
+
"n = 2\n",
|
117
|
+
"sorted_eigen_vals = sorted(mapping.keys(), reverse=True)\n",
|
118
|
+
"top_eigen_vals = sorted_eigen_vals[:n]\n",
|
119
|
+
"top_eigen_vals"
|
120
|
+
]
|
121
|
+
},
|
122
|
+
{
|
123
|
+
"cell_type": "code",
|
124
|
+
"execution_count": null,
|
125
|
+
"id": "6473c348-871e-428d-a3d6-7942fc0df706",
|
126
|
+
"metadata": {},
|
127
|
+
"outputs": [],
|
128
|
+
"source": [
|
129
|
+
"projection_matrix = np.array([mapping[val] for val in top_eigen_vals]).T\n",
|
130
|
+
"projection_matrix"
|
131
|
+
]
|
132
|
+
},
|
133
|
+
{
|
134
|
+
"cell_type": "code",
|
135
|
+
"execution_count": null,
|
136
|
+
"id": "6014c705-cb9a-470f-905f-ec05eb5f89f9",
|
137
|
+
"metadata": {},
|
138
|
+
"outputs": [],
|
139
|
+
"source": [
|
140
|
+
"X = df.iloc[:, :-1].values\n",
|
141
|
+
"reduced_data = X.dot(projection_matrix)\n",
|
142
|
+
"reduced_df = pd.DataFrame(reduced_data, columns=[f'PC{i+1}' for i in range(n)])\n",
|
143
|
+
"reduced_df['Class'] = df['Class'].values\n",
|
144
|
+
"reduced_df.head()"
|
145
|
+
]
|
146
|
+
},
|
147
|
+
{
|
148
|
+
"cell_type": "code",
|
149
|
+
"execution_count": null,
|
150
|
+
"id": "f53faa36-3044-4a48-9512-f7bc947bd9ba",
|
151
|
+
"metadata": {},
|
152
|
+
"outputs": [],
|
153
|
+
"source": [
|
154
|
+
"sns.scatterplot(data=reduced_df, x='PC1', y='PC2', hue='Class')\n",
|
155
|
+
"plt.title('Principle Component Analysis')\n",
|
156
|
+
"plt.show()"
|
157
|
+
]
|
158
|
+
}
|
159
|
+
],
|
160
|
+
"metadata": {
|
161
|
+
"kernelspec": {
|
162
|
+
"display_name": "Python 3 (ipykernel)",
|
163
|
+
"language": "python",
|
164
|
+
"name": "python3"
|
165
|
+
},
|
166
|
+
"language_info": {
|
167
|
+
"codemirror_mode": {
|
168
|
+
"name": "ipython",
|
169
|
+
"version": 3
|
170
|
+
},
|
171
|
+
"file_extension": ".py",
|
172
|
+
"mimetype": "text/x-python",
|
173
|
+
"name": "python",
|
174
|
+
"nbconvert_exporter": "python",
|
175
|
+
"pygments_lexer": "ipython3",
|
176
|
+
"version": "3.12.4"
|
177
|
+
}
|
178
|
+
},
|
179
|
+
"nbformat": 4,
|
180
|
+
"nbformat_minor": 5
|
181
|
+
}
|
@@ -0,0 +1,152 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": null,
|
6
|
+
"metadata": {},
|
7
|
+
"outputs": [],
|
8
|
+
"source": [
|
9
|
+
"transition = {\"AtoA\": 0.7, \"AtoB\": 0.3, \"BtoA\": 0.5, \"BtoB\": 0.5}\n",
|
10
|
+
"emission = {\"A\": {\"S1\": 0.6, \"S2\": 0.1, \"S3\": 0.3}, \"B\": {\"S1\": 0.1, \"S2\": 0.7, \"S3\": 0.2}}\n",
|
11
|
+
"pi = (1, 0)\n",
|
12
|
+
"\n",
|
13
|
+
"a, b = pi\n",
|
14
|
+
"alpha1, alpha2 = [a], [b]\n",
|
15
|
+
"\n",
|
16
|
+
"for obs in [\"S3\", \"S2\", \"S1\"]:\n",
|
17
|
+
" a_new = (a * transition[\"AtoA\"] * emission[\"A\"][obs] + \n",
|
18
|
+
" b * transition[\"BtoA\"] * emission[\"B\"][obs])\n",
|
19
|
+
" b_new = (a * transition[\"AtoB\"] * emission[\"A\"][obs] + \n",
|
20
|
+
" b * transition[\"BtoB\"] * emission[\"B\"][obs])\n",
|
21
|
+
" a, b = a_new, b_new\n",
|
22
|
+
" alpha1.append(a)\n",
|
23
|
+
" alpha2.append(b)\n",
|
24
|
+
" if obs == \"S1\":\n",
|
25
|
+
" print(\"Using Forward Chaining:\", round(a + b, 4))\n",
|
26
|
+
"\n",
|
27
|
+
"print(alpha1, \"\\n\", alpha2)\n"
|
28
|
+
]
|
29
|
+
},
|
30
|
+
{
|
31
|
+
"cell_type": "code",
|
32
|
+
"execution_count": null,
|
33
|
+
"metadata": {},
|
34
|
+
"outputs": [],
|
35
|
+
"source": [
|
36
|
+
"transition = {\"AtoA\": 0.7, \"AtoB\": 0.3, \"BtoA\": 0.5, \"BtoB\": 0.5}\n",
|
37
|
+
"emission = {\"A\": {\"S1\": 0.6, \"S2\": 0.1, \"S3\": 0.3}, \"B\": {\"S1\": 0.1, \"S2\": 0.7, \"S3\": 0.2}}\n",
|
38
|
+
"pi = (1, 1)\n",
|
39
|
+
"\n",
|
40
|
+
"a, b = pi\n",
|
41
|
+
"beta1, beta2 = [], []\n",
|
42
|
+
"\n",
|
43
|
+
"for obs in reversed([\"S3\", \"S2\", \"S1\"]):\n",
|
44
|
+
" a_new = (a * transition[\"AtoA\"] * emission[\"A\"][obs] +\n",
|
45
|
+
" b * transition[\"AtoB\"] * emission[\"A\"][obs])\n",
|
46
|
+
" b_new = (a * transition[\"BtoA\"] * emission[\"B\"][obs] +\n",
|
47
|
+
" b * transition[\"BtoB\"] * emission[\"B\"][obs])\n",
|
48
|
+
" a, b = a_new, b_new\n",
|
49
|
+
" beta1.append(a)\n",
|
50
|
+
" beta2.append(b)\n",
|
51
|
+
" if obs == \"S3\":\n",
|
52
|
+
" print(\"Using Backward Chaining:\", round(a, 4))\n",
|
53
|
+
"\n",
|
54
|
+
"print(beta1, \"\\n\", beta2)"
|
55
|
+
]
|
56
|
+
},
|
57
|
+
{
|
58
|
+
"cell_type": "code",
|
59
|
+
"execution_count": null,
|
60
|
+
"metadata": {},
|
61
|
+
"outputs": [],
|
62
|
+
"source": [
|
63
|
+
"transition = {\"AtoA\": 0.7, \"AtoB\": 0.3, \"BtoA\": 0.5, \"BtoB\": 0.5}\n",
|
64
|
+
"emission = {\"A\": {\"S1\": 0.6, \"S2\": 0.1, \"S3\": 0.3}, \"B\": {\"S1\": 0.1, \"S2\": 0.7, \"S3\": 0.2}}\n",
|
65
|
+
"pi = (1, 0)\n",
|
66
|
+
"\n",
|
67
|
+
"a, b = pi\n",
|
68
|
+
"delta1, delta2, chi = [a], [b], []\n",
|
69
|
+
"\n",
|
70
|
+
"for obs in [\"S3\", \"S2\", \"S1\"]:\n",
|
71
|
+
" a_new = max(a * transition[\"AtoA\"] * emission[\"A\"][obs],\n",
|
72
|
+
" b * transition[\"BtoA\"] * emission[\"B\"][obs])\n",
|
73
|
+
" b_new = max(a * transition[\"AtoB\"] * emission[\"A\"][obs],\n",
|
74
|
+
" b * transition[\"BtoB\"] * emission[\"B\"][obs])\n",
|
75
|
+
" \n",
|
76
|
+
" chi.append('A' if a_new > b_new else 'B')\n",
|
77
|
+
" a, b = a_new, b_new\n",
|
78
|
+
" delta1.append(a)\n",
|
79
|
+
" delta2.append(b)\n",
|
80
|
+
"\n",
|
81
|
+
" if obs == \"S1\":\n",
|
82
|
+
" print(\"Using Viterbi Algorithm:\", round(max(a, b), 4))\n",
|
83
|
+
" print(\"Best Sequence:\")\n",
|
84
|
+
" print('->'.join(chi + [chi[-1]])) # printing path\n",
|
85
|
+
"\n",
|
86
|
+
"print(delta1)\n",
|
87
|
+
"print(delta2)"
|
88
|
+
]
|
89
|
+
},
|
90
|
+
{
|
91
|
+
"cell_type": "code",
|
92
|
+
"execution_count": null,
|
93
|
+
"metadata": {
|
94
|
+
"colab": {
|
95
|
+
"base_uri": "https://localhost:8080/",
|
96
|
+
"height": 513
|
97
|
+
},
|
98
|
+
"id": "qBU_NokB_679",
|
99
|
+
"outputId": "d3b086cb-92d6-4941-de26-137888b1a2d1"
|
100
|
+
},
|
101
|
+
"outputs": [],
|
102
|
+
"source": [
|
103
|
+
"import networkx as nx\n",
|
104
|
+
"import matplotlib.pyplot as plt\n",
|
105
|
+
"\n",
|
106
|
+
"states = ['A', 'B']\n",
|
107
|
+
"observations = ['S1', 'S2', 'S3']\n",
|
108
|
+
"\n",
|
109
|
+
"graph = nx.DiGraph()\n",
|
110
|
+
"\n",
|
111
|
+
"for from_state in states:\n",
|
112
|
+
" for to_state in states:\n",
|
113
|
+
" graph.add_edge(from_state, to_state, weight=transition[from_state + 'to' + to_state],\n",
|
114
|
+
" label=str(transition[from_state + 'to' + to_state]))\n",
|
115
|
+
" for obs in observations:\n",
|
116
|
+
" graph.add_edge(from_state, obs, weight=emission[from_state][obs],\n",
|
117
|
+
" label=str(emission[from_state][obs]), style='dashed')\n",
|
118
|
+
"\n",
|
119
|
+
"pos = nx.spring_layout(graph)\n",
|
120
|
+
"nx.draw(graph, pos, with_labels=True, node_size=1500, node_color=\"skyblue\", font_size=12, font_weight='bold')\n",
|
121
|
+
"nx.draw_networkx_edge_labels(graph, pos, edge_labels=nx.get_edge_attributes(graph, 'label'), font_size=10)\n",
|
122
|
+
"\n",
|
123
|
+
"plt.title(\"Hidden Markov Model\")\n",
|
124
|
+
"plt.show()"
|
125
|
+
]
|
126
|
+
}
|
127
|
+
],
|
128
|
+
"metadata": {
|
129
|
+
"colab": {
|
130
|
+
"provenance": []
|
131
|
+
},
|
132
|
+
"kernelspec": {
|
133
|
+
"display_name": "Python 3 (ipykernel)",
|
134
|
+
"language": "python",
|
135
|
+
"name": "python3"
|
136
|
+
},
|
137
|
+
"language_info": {
|
138
|
+
"codemirror_mode": {
|
139
|
+
"name": "ipython",
|
140
|
+
"version": 3
|
141
|
+
},
|
142
|
+
"file_extension": ".py",
|
143
|
+
"mimetype": "text/x-python",
|
144
|
+
"name": "python",
|
145
|
+
"nbconvert_exporter": "python",
|
146
|
+
"pygments_lexer": "ipython3",
|
147
|
+
"version": "3.12.4"
|
148
|
+
}
|
149
|
+
},
|
150
|
+
"nbformat": 4,
|
151
|
+
"nbformat_minor": 4
|
152
|
+
}
|
@@ -0,0 +1,117 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": null,
|
6
|
+
"id": "b4a8b5dc",
|
7
|
+
"metadata": {},
|
8
|
+
"outputs": [],
|
9
|
+
"source": [
|
10
|
+
"import numpy as np\n",
|
11
|
+
"import pandas as pd\n",
|
12
|
+
"import matplotlib.pyplot as plt\n",
|
13
|
+
"import sklearn\n",
|
14
|
+
"from sklearn.neighbors import KNeighborsClassifier\n",
|
15
|
+
"from sklearn.model_selection import train_test_split\n",
|
16
|
+
"from sklearn import metrics\n",
|
17
|
+
"from sklearn.preprocessing import StandardScaler"
|
18
|
+
]
|
19
|
+
},
|
20
|
+
{
|
21
|
+
"cell_type": "code",
|
22
|
+
"execution_count": null,
|
23
|
+
"id": "1c308767",
|
24
|
+
"metadata": {},
|
25
|
+
"outputs": [],
|
26
|
+
"source": [
|
27
|
+
"cols = ['class name','left-weight','left-distance','right-weight','right-distance']\n",
|
28
|
+
"df = pd.read_csv('data/balance-scale.txt', delimiter=',', names=cols)\n",
|
29
|
+
"print(\"Shape:\", df.shape)\n",
|
30
|
+
"df.head()"
|
31
|
+
]
|
32
|
+
},
|
33
|
+
{
|
34
|
+
"cell_type": "code",
|
35
|
+
"execution_count": null,
|
36
|
+
"id": "c5dc6788-f131-4b3e-8b39-0f83c117e2cd",
|
37
|
+
"metadata": {},
|
38
|
+
"outputs": [],
|
39
|
+
"source": [
|
40
|
+
"features = ['left-weight', 'left-distance', 'right-weight', 'right-distance']\n",
|
41
|
+
"x = df.loc[:, features]\n",
|
42
|
+
"y = df.loc[:, 'class name']"
|
43
|
+
]
|
44
|
+
},
|
45
|
+
{
|
46
|
+
"cell_type": "code",
|
47
|
+
"execution_count": null,
|
48
|
+
"id": "59450a6f-db40-4b8c-b294-d427f0792c26",
|
49
|
+
"metadata": {},
|
50
|
+
"outputs": [],
|
51
|
+
"source": [
|
52
|
+
"x = StandardScaler().fit_transform(x)\n",
|
53
|
+
"X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.4, \n",
|
54
|
+
" random_state=4)\n",
|
55
|
+
"print (X_train.shape)\n",
|
56
|
+
"print (X_test.shape)\n",
|
57
|
+
"knn = KNeighborsClassifier(n_neighbors=15)\n",
|
58
|
+
"knn.fit(X_train, y_train) "
|
59
|
+
]
|
60
|
+
},
|
61
|
+
{
|
62
|
+
"cell_type": "code",
|
63
|
+
"execution_count": null,
|
64
|
+
"id": "22e96c2a",
|
65
|
+
"metadata": {},
|
66
|
+
"outputs": [],
|
67
|
+
"source": [
|
68
|
+
"y_pred = knn.predict(np.array([1,1,1,1]).reshape(1, -1))[0]\n",
|
69
|
+
"print(\"Class Predicted [1,1,1,1]:\", y_pred)"
|
70
|
+
]
|
71
|
+
},
|
72
|
+
{
|
73
|
+
"cell_type": "code",
|
74
|
+
"execution_count": null,
|
75
|
+
"id": "366c003d",
|
76
|
+
"metadata": {},
|
77
|
+
"outputs": [],
|
78
|
+
"source": [
|
79
|
+
"y_pred = knn.predict(X_test)\n",
|
80
|
+
"print(\"Accuracy:\", metrics.accuracy_score(y_test, y_pred))"
|
81
|
+
]
|
82
|
+
},
|
83
|
+
{
|
84
|
+
"cell_type": "code",
|
85
|
+
"execution_count": null,
|
86
|
+
"id": "517c6e56-afc6-481c-a48c-b1a0435537bd",
|
87
|
+
"metadata": {},
|
88
|
+
"outputs": [],
|
89
|
+
"source": [
|
90
|
+
"cm = metrics.confusion_matrix(y_test, knn.predict(X_test))\n",
|
91
|
+
"metrics.ConfusionMatrixDisplay(cm, display_labels=['B','L','R']).plot()\n",
|
92
|
+
"plt.show()"
|
93
|
+
]
|
94
|
+
}
|
95
|
+
],
|
96
|
+
"metadata": {
|
97
|
+
"kernelspec": {
|
98
|
+
"display_name": "Python 3 (ipykernel)",
|
99
|
+
"language": "python",
|
100
|
+
"name": "python3"
|
101
|
+
},
|
102
|
+
"language_info": {
|
103
|
+
"codemirror_mode": {
|
104
|
+
"name": "ipython",
|
105
|
+
"version": 3
|
106
|
+
},
|
107
|
+
"file_extension": ".py",
|
108
|
+
"mimetype": "text/x-python",
|
109
|
+
"name": "python",
|
110
|
+
"nbconvert_exporter": "python",
|
111
|
+
"pygments_lexer": "ipython3",
|
112
|
+
"version": "3.12.4"
|
113
|
+
}
|
114
|
+
},
|
115
|
+
"nbformat": 4,
|
116
|
+
"nbformat_minor": 5
|
117
|
+
}
|
@@ -0,0 +1,156 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": null,
|
6
|
+
"id": "f8872206-2c4c-4b9c-a146-4874bed47d58",
|
7
|
+
"metadata": {},
|
8
|
+
"outputs": [],
|
9
|
+
"source": [
|
10
|
+
"import numpy as np\n",
|
11
|
+
"import pandas as pd\n",
|
12
|
+
"import matplotlib.pyplot as plt\n",
|
13
|
+
"import sklearn\n",
|
14
|
+
"from sklearn import svm\n",
|
15
|
+
"from sklearn.neighbors import KNeighborsClassifier\n",
|
16
|
+
"from sklearn.model_selection import train_test_split\n",
|
17
|
+
"from sklearn import metrics\n",
|
18
|
+
"from sklearn.preprocessing import StandardScaler"
|
19
|
+
]
|
20
|
+
},
|
21
|
+
{
|
22
|
+
"cell_type": "code",
|
23
|
+
"execution_count": null,
|
24
|
+
"id": "6784e6b1-ab93-4529-85a0-8be414f00283",
|
25
|
+
"metadata": {},
|
26
|
+
"outputs": [],
|
27
|
+
"source": [
|
28
|
+
"df = pd.read_csv('data/iris.csv')\n",
|
29
|
+
"df.head()"
|
30
|
+
]
|
31
|
+
},
|
32
|
+
{
|
33
|
+
"cell_type": "code",
|
34
|
+
"execution_count": null,
|
35
|
+
"id": "7d9be320-b960-49d2-bf0b-d6193332b1f8",
|
36
|
+
"metadata": {},
|
37
|
+
"outputs": [],
|
38
|
+
"source": [
|
39
|
+
"class_mapper = {0: \"setosa\", 1: \"versicolor\", 2: \"virginica\"}\n",
|
40
|
+
"df['target'] = df['class'].map(class_mapper)\n",
|
41
|
+
"df.head()"
|
42
|
+
]
|
43
|
+
},
|
44
|
+
{
|
45
|
+
"cell_type": "code",
|
46
|
+
"execution_count": null,
|
47
|
+
"id": "d523f9bd-14d5-42f6-80cc-fe8a8210fa1f",
|
48
|
+
"metadata": {},
|
49
|
+
"outputs": [],
|
50
|
+
"source": [
|
51
|
+
"feature = ['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']\n",
|
52
|
+
"x = df.loc[:, feature]\n",
|
53
|
+
"y = df.loc[:, 'target']"
|
54
|
+
]
|
55
|
+
},
|
56
|
+
{
|
57
|
+
"cell_type": "code",
|
58
|
+
"execution_count": null,
|
59
|
+
"id": "f852a9d8-bb7a-49d3-a199-e47738c05a04",
|
60
|
+
"metadata": {},
|
61
|
+
"outputs": [],
|
62
|
+
"source": [
|
63
|
+
"x = StandardScaler().fit_transform(x)\n",
|
64
|
+
"X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.4,\n",
|
65
|
+
" random_state=4)\n",
|
66
|
+
"print (X_train.shape)\n",
|
67
|
+
"print (X_test.shape)\n",
|
68
|
+
"knn = KNeighborsClassifier(n_neighbors=15)\n",
|
69
|
+
"knn.fit(X_train, y_train) "
|
70
|
+
]
|
71
|
+
},
|
72
|
+
{
|
73
|
+
"cell_type": "code",
|
74
|
+
"execution_count": null,
|
75
|
+
"id": "e57d7cdd-29b8-4235-aea7-9f904a70e9fd",
|
76
|
+
"metadata": {},
|
77
|
+
"outputs": [],
|
78
|
+
"source": [
|
79
|
+
"y_pred = knn.predict(np.array([5.2, 3.5, 1.1, 0.2]).reshape(1, -1))[0]\n",
|
80
|
+
"print(\"Class Predicted [5.2, 3.5, 1.1, 0.2]:\", y_pred)"
|
81
|
+
]
|
82
|
+
},
|
83
|
+
{
|
84
|
+
"cell_type": "code",
|
85
|
+
"execution_count": null,
|
86
|
+
"id": "b48aeb20-c569-4d0f-900c-82cd9ad92098",
|
87
|
+
"metadata": {},
|
88
|
+
"outputs": [],
|
89
|
+
"source": [
|
90
|
+
"y_pred = knn.predict(X_test)\n",
|
91
|
+
"print(\"Accuracy:\", metrics.accuracy_score(y_test, y_pred))"
|
92
|
+
]
|
93
|
+
},
|
94
|
+
{
|
95
|
+
"cell_type": "code",
|
96
|
+
"execution_count": null,
|
97
|
+
"id": "f72df1f8-5075-4d23-b93f-18a91e8fd1e0",
|
98
|
+
"metadata": {},
|
99
|
+
"outputs": [],
|
100
|
+
"source": [
|
101
|
+
"cm = metrics.confusion_matrix(y_test, knn.predict(X_test))\n",
|
102
|
+
"metrics.ConfusionMatrixDisplay(cm, display_labels=['setosa','versicolor','virginica']).plot()\n",
|
103
|
+
"plt.show()"
|
104
|
+
]
|
105
|
+
},
|
106
|
+
{
|
107
|
+
"cell_type": "code",
|
108
|
+
"execution_count": null,
|
109
|
+
"id": "4c1df2d9-4e4b-43a1-aaec-d7eee15b716b",
|
110
|
+
"metadata": {},
|
111
|
+
"outputs": [],
|
112
|
+
"source": [
|
113
|
+
"from sklearn import svm\n",
|
114
|
+
"import numpy as np\n",
|
115
|
+
"import matplotlib.pyplot as plt\n",
|
116
|
+
"\n",
|
117
|
+
"X, y = df.iloc[:, :2].values, df['class'].values\n",
|
118
|
+
"\n",
|
119
|
+
"clf = svm.SVC(kernel='linear').fit(X, y)\n",
|
120
|
+
"\n",
|
121
|
+
"x_min, x_max = X[:,0].min() - 1, X[:,0].max() + 1\n",
|
122
|
+
"y_min, y_max = X[:,1].min() - 1, X[:,1].max() + 1\n",
|
123
|
+
"xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))\n",
|
124
|
+
"\n",
|
125
|
+
"Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)\n",
|
126
|
+
"\n",
|
127
|
+
"plt.figure()\n",
|
128
|
+
"plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)\n",
|
129
|
+
"plt.scatter(X[:,0], X[:,1], c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')\n",
|
130
|
+
"plt.title('Decision surface of linear SVC')\n",
|
131
|
+
"plt.show()"
|
132
|
+
]
|
133
|
+
}
|
134
|
+
],
|
135
|
+
"metadata": {
|
136
|
+
"kernelspec": {
|
137
|
+
"display_name": "Python 3 (ipykernel)",
|
138
|
+
"language": "python",
|
139
|
+
"name": "python3"
|
140
|
+
},
|
141
|
+
"language_info": {
|
142
|
+
"codemirror_mode": {
|
143
|
+
"name": "ipython",
|
144
|
+
"version": 3
|
145
|
+
},
|
146
|
+
"file_extension": ".py",
|
147
|
+
"mimetype": "text/x-python",
|
148
|
+
"name": "python",
|
149
|
+
"nbconvert_exporter": "python",
|
150
|
+
"pygments_lexer": "ipython3",
|
151
|
+
"version": "3.12.4"
|
152
|
+
}
|
153
|
+
},
|
154
|
+
"nbformat": 4,
|
155
|
+
"nbformat_minor": 5
|
156
|
+
}
|