Spaces:
Sleeping
Sleeping
JKJanosko
commited on
Commit
•
61d4541
1
Parent(s):
2829e3a
Add files via upload
Browse files- MidtermTakehome/CBC_data.csv +0 -0
- MidtermTakehome/index.ipynb +336 -0
MidtermTakehome/CBC_data.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
MidtermTakehome/index.ipynb
ADDED
@@ -0,0 +1,336 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "2IdTjyqZHKK5"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# Midterm - Spring 2023\n",
|
10 |
+
"\n",
|
11 |
+
"## Problem 1: Take-at-home (45 points total)\n",
|
12 |
+
"\n",
|
13 |
+
"You are applying for a position at the data science team of USDA and you are given data associated with determining appropriate parasite treatment of canines. The suggested treatment options are determined based on a **logistic regression** model that predicts if the canine is infected with a parasite. \n",
|
14 |
+
"\n",
|
15 |
+
"The data is given in the site: https://data.world/ehales/grls-parasite-study/workspace/file?filename=CBC_data.csv and more specifically in the CBC_data.csv file. Login using you University Google account to access the data and the description that includes a paper on the study (**you dont need to read the paper to solve this problem**). Your target variable $y$ column is titled `parasite_status`. \n",
|
16 |
+
"\n",
|
17 |
+
"\n"
|
18 |
+
]
|
19 |
+
},
|
20 |
+
{
|
21 |
+
"cell_type": "markdown",
|
22 |
+
"metadata": {
|
23 |
+
"id": "fp6F7zIuHKK-"
|
24 |
+
},
|
25 |
+
"source": [
|
26 |
+
"### Question 1 - Feature Engineering (5 points)\n",
|
27 |
+
"\n",
|
28 |
+
"In this step you outline the following as potential features (this is a limited example - we can have many features as in your programming exercise below). \n",
|
29 |
+
"\n",
|
30 |
+
"Write the posterior probability expressions for logistic regression for the problem you are given to solve."
|
31 |
+
]
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"cell_type": "markdown",
|
35 |
+
"metadata": {
|
36 |
+
"id": "ZDRhz6bDHKLA"
|
37 |
+
},
|
38 |
+
"source": [
|
39 |
+
"$$p(y=1| \\mathbf{x}, \\mathbf w)=\\frac{p(x | y=1)p(y=1)}{p(x | y=1)p(y=1)+p(x|y=0)p(y=0)} = \\frac{1}{1+e^{-\\alpha}}=\\sigma(\\alpha)$$ \n",
|
40 |
+
"\n",
|
41 |
+
"$$p(y=0| \\mathbf{x}, \\mathbf w)=1-\\frac{p(x | y=1)p(y=1)}{p(x | y=1)p(y=1)+p(x|y=0)p(y=0)} = 1-\\sigma(\\alpha)$$\n",
|
42 |
+
"\n",
|
43 |
+
"the equeation for alpha is shown below. $$\\alpha=ln\\frac{p(x|y=1)p(y=1)}{p(x|y=0)p(y=0)}$$"
|
44 |
+
]
|
45 |
+
},
|
46 |
+
{
|
47 |
+
"cell_type": "markdown",
|
48 |
+
"metadata": {
|
49 |
+
"id": "zXYe2qtSHKLB"
|
50 |
+
},
|
51 |
+
"source": [
|
52 |
+
"\n",
|
53 |
+
"\n",
|
54 |
+
"### Question 2 - Decision Boundary (5 points)\n",
|
55 |
+
"\n",
|
56 |
+
"Write the expression for the decision boundary assuming that $p(y=1)=p(y=0)$. The decision boundary is the line that separates the two classes.\n",
|
57 |
+
"\n",
|
58 |
+
"\n",
|
59 |
+
"\n"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "markdown",
|
64 |
+
"metadata": {
|
65 |
+
"id": "5sM9vvj3HKLC"
|
66 |
+
},
|
67 |
+
"source": [
|
68 |
+
"The decision boundary should be the intersection of the probability mass functions, so as to minimize false positives and false negatives. this can be written via the following equality:\n",
|
69 |
+
"\n",
|
70 |
+
"$$p(y=1|x,w)=p(y=0|x,w)$$\n",
|
71 |
+
"\n",
|
72 |
+
"which can also be written as the following:\n",
|
73 |
+
"\n",
|
74 |
+
"$$\\sigma(\\alpha)=1-\\sigma(\\alpha)$$\n",
|
75 |
+
"\n",
|
76 |
+
"visually, the decision boundary should sit at the intersection of the sigmoid line and the inverse simga line, representing the conditions where there is an equal probability that y=1 and y=0."
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "markdown",
|
81 |
+
"metadata": {
|
82 |
+
"id": "uY7lH1geHKLD"
|
83 |
+
},
|
84 |
+
"source": [
|
85 |
+
"\n",
|
86 |
+
"\n",
|
87 |
+
"### Question 3 - Loss function (5 points)\n",
|
88 |
+
"\n",
|
89 |
+
"Write the expression of the loss as a function of $\\mathbf w$ that makes sense for you to use in this problem. \n",
|
90 |
+
"\n",
|
91 |
+
"NOTE: The loss will be a function that will include this function: \n",
|
92 |
+
"\n",
|
93 |
+
"$$\\sigma(a) = \\frac{1}{1+e^{-a}}$$\n",
|
94 |
+
"\n"
|
95 |
+
]
|
96 |
+
},
|
97 |
+
{
|
98 |
+
"cell_type": "markdown",
|
99 |
+
"metadata": {
|
100 |
+
"id": "or-qkG4MHKLE"
|
101 |
+
},
|
102 |
+
"source": [
|
103 |
+
"$$L_{CE} = -\\Sigma^m_{i=1}[y_i\\ln(σ(w^Τx_i))+(1-y_i)\\ln(1-\\sigma(w^Τx_i)] $$"
|
104 |
+
]
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"cell_type": "markdown",
|
108 |
+
"metadata": {
|
109 |
+
"id": "qq1aXGcMHKLF"
|
110 |
+
},
|
111 |
+
"source": [
|
112 |
+
"\n",
|
113 |
+
"### Question 4 - Gradient (5 points)\n",
|
114 |
+
"\n",
|
115 |
+
"Write the expression of the gradient of the loss with respect to the parameters - show all your work.\n",
|
116 |
+
"\n"
|
117 |
+
]
|
118 |
+
},
|
119 |
+
{
|
120 |
+
"cell_type": "markdown",
|
121 |
+
"metadata": {
|
122 |
+
"id": "fZklUA8PHKLH"
|
123 |
+
},
|
124 |
+
"source": [
|
125 |
+
"$$ \\nabla_\\mathbf w L_{CE} =\\Sigma^m_{i=1}[(σ(w^Τx_i)-y_i)x_i]$$"
|
126 |
+
]
|
127 |
+
},
|
128 |
+
{
|
129 |
+
"cell_type": "markdown",
|
130 |
+
"metadata": {
|
131 |
+
"id": "WHj0C8T7HKLI"
|
132 |
+
},
|
133 |
+
"source": [
|
134 |
+
"### Question 5 - Imbalanced dataset (10 points)\n",
|
135 |
+
"\n",
|
136 |
+
"You are now told that in the dataset \n",
|
137 |
+
"\n",
|
138 |
+
"$$p(y=0) >> p(y=1)$$\n",
|
139 |
+
"\n",
|
140 |
+
"Can you comment if the accuracy of Logistic Regression will be affected by such imbalance?\n",
|
141 |
+
"\n"
|
142 |
+
]
|
143 |
+
},
|
144 |
+
{
|
145 |
+
"cell_type": "markdown",
|
146 |
+
"metadata": {
|
147 |
+
"id": "Ft7xpMVIHKLK"
|
148 |
+
},
|
149 |
+
"source": [
|
150 |
+
"If the p(y=0) is much larger than p(y=1), this will lead to the regression being much more likely to return ŷ=0. This will lead to a higher rate of false negatives, thereby effecting the accuracy of the Logisitic Regression. The model will have a low rate of false positives, however, as it will be much less likely to predict a positive in the first place. "
|
151 |
+
]
|
152 |
+
},
|
153 |
+
{
|
154 |
+
"cell_type": "markdown",
|
155 |
+
"metadata": {
|
156 |
+
"id": "iE0WczaxHKLL"
|
157 |
+
},
|
158 |
+
"source": [
|
159 |
+
"\n",
|
160 |
+
"### Question 6 - SGD (15 points)\n",
|
161 |
+
"\n",
|
162 |
+
"The interviewer was impressed with your answers and wants to test your programming skills. \n",
|
163 |
+
"\n",
|
164 |
+
"1. Use the dataset to train a logistic regressor that will predict the target variable $y$. \n",
|
165 |
+
"\n",
|
166 |
+
" 2. Report the harmonic mean of precision (p) and recall (r) i.e the [metric called $F_1$ score](https://en.wikipedia.org/wiki/F-score) that is calculated as shown below using a test dataset that is 20% of each group. Plot the $F_1$ score vs the iteration number $t$. \n",
|
167 |
+
"\n",
|
168 |
+
"$$F_1 = \\frac{2}{r^{-1} + p^{-1}}$$\n",
|
169 |
+
"\n",
|
170 |
+
"Your code includes hyperparameter optimization of the learning rate and mini batch size. Please learn about cross validation which is a splitting strategy for tuning models [here](https://scikit-learn.org/stable/modules/cross_validation.html).\n",
|
171 |
+
"\n",
|
172 |
+
"You are allowed to use any library you want to code this problem.\n",
|
173 |
+
"\n"
|
174 |
+
]
|
175 |
+
},
|
176 |
+
{
|
177 |
+
"cell_type": "code",
|
178 |
+
"execution_count": 99,
|
179 |
+
"metadata": {
|
180 |
+
"colab": {
|
181 |
+
"base_uri": "https://localhost:8080/",
|
182 |
+
"height": 321
|
183 |
+
},
|
184 |
+
"id": "keQIm2V6HKLN",
|
185 |
+
"outputId": "0a4ae008-a656-4b91-c40f-b80653b25364"
|
186 |
+
},
|
187 |
+
"outputs": [
|
188 |
+
{
|
189 |
+
"name": "stdout",
|
190 |
+
"output_type": "stream",
|
191 |
+
"text": [
|
192 |
+
"w vector: [[-1.84834009e-03 -4.54038579e-03 7.88831514e-06 1.83512257e-03\n",
|
193 |
+
" 3.41803018e-03 -2.00747551e-03 -3.22278484e-03 1.61564091e-04]]\n"
|
194 |
+
]
|
195 |
+
},
|
196 |
+
{
|
197 |
+
"data": {
|
198 |
+
"text/plain": [
|
199 |
+
"<matplotlib.collections.PathCollection at 0x7ff0072dc520>"
|
200 |
+
]
|
201 |
+
},
|
202 |
+
"execution_count": 99,
|
203 |
+
"metadata": {},
|
204 |
+
"output_type": "execute_result"
|
205 |
+
},
|
206 |
+
{
|
207 |
+
"data": {
|
208 |
+
"image/png": "iVBORw0KGgoAAAANSUhEUg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uBh7r25uYNkGQD8MPAr3fLYRXMex6L3tdbCfrZvsB8cF/PcnH47qr6anf7L4HvHmYxyynJRqa+5OYxVsG8u9MXXwCeBx4BvgycrKrTXZdW9/dfBX4R+Fa3fDmrY94wdTD/n0kOJrm1a1v0vt7PVwnqItN9jWOT75tN8jrgd5j6trK/zrQvVm513lV1BrgmyTrg08DfG25Fyy/JjwDPV9XBJO8ecjnD8M6qmkzyXcAjSf50+sqF7uutvKK/IF9CvsL9VZLvAeh+Pz/kegYuyVqmQv6/VtXDXXPz8z6rqk4CnwV+EFiX5OwLtRb393cA70/yDFOnYq8Ffo325w1AVU12v59n6uC+jSXs660E/atfYN5dhb+RqS8sX03OfkE73e//NsRaBq47P3sv8FRVfWLaqtbnPda9kifJKHAdU9cnPgvc0HVrbt5VtbeqNlTVRqb+nh+tqp+i8XkDJHltkr9z9jbwXuBPWMK+3sx/xs72BebDrWj5JPlt4N1MfXTpXwG/DOwDPgW8iamPef7Jqpp5wfaileSdwP8GDvPtc7b/hqnz9C3P+y1MXXgbYeqF2aeq6vYk38fUK903AIeAf1JV3xxepcunO3Xz4ar6kdUw726On+4W1wC/VVW/kuRyFrmvNxP0kqTZtXLqRpI0B4Nekhpn0EtS4wx6SWqcQS9JjTPoJalxBr0kNe7/A9ZCdgVOdEY7AAAAAElFTkSuQmCC",
|
209 |
+
"text/plain": [
|
210 |
+
"<Figure size 432x288 with 1 Axes>"
|
211 |
+
]
|
212 |
+
},
|
213 |
+
"metadata": {
|
214 |
+
"needs_background": "light"
|
215 |
+
},
|
216 |
+
"output_type": "display_data"
|
217 |
+
}
|
218 |
+
],
|
219 |
+
"source": [
|
220 |
+
"from numpy.core.fromnumeric import size\n",
|
221 |
+
"# write your code here\n",
|
222 |
+
"\n",
|
223 |
+
"import pandas as pd\n",
|
224 |
+
"import numpy as np\n",
|
225 |
+
"import matplotlib.pyplot as plt\n",
|
226 |
+
"from sklearn.linear_model import LogisticRegression\n",
|
227 |
+
"from sklearn.model_selection import train_test_split\n",
|
228 |
+
"from sklearn import datasets\n",
|
229 |
+
"from sklearn import svm\n",
|
230 |
+
"\n",
|
231 |
+
"data=pd.read_csv(\"./CBC_data.csv\").dropna()\n",
|
232 |
+
"y=data[[\"PARASITE_STATUS\"]].values\n",
|
233 |
+
"X=data[[\"RBC\",\"HGB\",\"WBC\",\"EOS.CNT\",\"MONO.CNT\",\"NUT.CNT\",\"PL.CNT\",\"LYMP.CNT\"]]\n",
|
234 |
+
"\n",
|
235 |
+
"n_epochs=50\n",
|
236 |
+
"eta=3*10**-7\n",
|
237 |
+
"ones=[1,1,1,1,1,1,1,1]\n",
|
238 |
+
"w=np.matrix(np.zeros(X.shape[1]))\n",
|
239 |
+
"\n",
|
240 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)\n",
|
241 |
+
"\n",
|
242 |
+
"m=y_train.size\n",
|
243 |
+
"\n",
|
244 |
+
"def sig_fun(w, X):\n",
|
245 |
+
" output = 1.0/(1 + np.exp(-np.dot(w,X.T)))\n",
|
246 |
+
" #print(np.exp(-np.dot(w,X.T)),\" - \",w,X.T)\n",
|
247 |
+
" return output\n",
|
248 |
+
"\n",
|
249 |
+
"\n",
|
250 |
+
"def grad_fun(w,X,y):\n",
|
251 |
+
" #print(np.dot((sig_fun(w,X)-y),X))\n",
|
252 |
+
" return np.dot((sig_fun(w,X)-y),X)\n",
|
253 |
+
"\n",
|
254 |
+
"def pred_values(w, X):\n",
|
255 |
+
" probabilities = sig_fun(w, X)\n",
|
256 |
+
" pred_value = np.where(probabilities >= .5, 1, 0) \n",
|
257 |
+
" return np.squeeze(pred_value)\n",
|
258 |
+
"\n",
|
259 |
+
"def f1_score(X_test,y_test,w):\n",
|
260 |
+
" predictions = pred_values(w,X_test)\n",
|
261 |
+
" fp=0;tp=0;fn=0\n",
|
262 |
+
" for i in range(y_test.size):\n",
|
263 |
+
" yi=1 if (y_train[i])==(\"Positive\") else 0\n",
|
264 |
+
" #print(yi,predictions[i])\n",
|
265 |
+
" if(yi==1):\n",
|
266 |
+
" if(predictions[i]==1):\n",
|
267 |
+
" tp+=1\n",
|
268 |
+
" else:\n",
|
269 |
+
" fn+=1\n",
|
270 |
+
" else:\n",
|
271 |
+
" if(predictions[i]==1):\n",
|
272 |
+
" fp+=1 \n",
|
273 |
+
" #print(fp,fn,tp)\n",
|
274 |
+
" if(tp==0):\n",
|
275 |
+
" return 0\n",
|
276 |
+
" recall=tp/(tp+fn)\n",
|
277 |
+
" percision=tp/(tp+fp)\n",
|
278 |
+
" return 2/(percision**-1+recall**-1)\n",
|
279 |
+
"\n",
|
280 |
+
"\n",
|
281 |
+
"iteration_number=[]\n",
|
282 |
+
"F_score_v_iteration=[]\n",
|
283 |
+
"\n",
|
284 |
+
"\n",
|
285 |
+
"for epoch in range(n_epochs):\n",
|
286 |
+
" for iteration in range(m):\n",
|
287 |
+
" random_index=np.random.randint(m)\n",
|
288 |
+
" xi=X_train[random_index:random_index+1].values\n",
|
289 |
+
" #if(type(np.dot(ones,xi.T)[0])!=type(1)):\n",
|
290 |
+
" # continue\n",
|
291 |
+
" #print(xi)\n",
|
292 |
+
" yi=1 if (y_train[random_index:random_index+1][0])==(\"Positive\") else 0\n",
|
293 |
+
" gradients=grad_fun(w,xi,yi)\n",
|
294 |
+
" w=w-eta*gradients\n",
|
295 |
+
" iteration_number.append(epoch)\n",
|
296 |
+
" F_score_v_iteration.append(f1_score(X_test,y_test,w))\n",
|
297 |
+
" #print(w,gradients)\n",
|
298 |
+
" #print(yi,(y_train[random_index:random_index+1][0]))\n",
|
299 |
+
"\n",
|
300 |
+
"print(\"w vector: \",w)\n",
|
301 |
+
"plt.scatter(iteration_number,F_score_v_iteration)\n"
|
302 |
+
]
|
303 |
+
},
|
304 |
+
{
|
305 |
+
"cell_type": "markdown",
|
306 |
+
"metadata": {
|
307 |
+
"id": "aOh-_F2u-Fvt"
|
308 |
+
},
|
309 |
+
"source": [
|
310 |
+
"Most of the items in the data set are cases where the animal did not have a parasite. This means that the combined probability of an animal having a parasite, which was our positive case, is very low. Our model does not handle this very well, and tends to always predict no parasite. In this case, when the number of true positives is 0, we set the f1 score equal to zero to avoid a division by zero error during the runtime. This is shown on the graph above."
|
311 |
+
]
|
312 |
+
}
|
313 |
+
],
|
314 |
+
"metadata": {
|
315 |
+
"colab": {
|
316 |
+
"provenance": []
|
317 |
+
},
|
318 |
+
"kernelspec": {
|
319 |
+
"display_name": "ai-course",
|
320 |
+
"language": "python",
|
321 |
+
"name": "python3"
|
322 |
+
},
|
323 |
+
"language_info": {
|
324 |
+
"name": "python",
|
325 |
+
"version": "3.10.8"
|
326 |
+
},
|
327 |
+
"orig_nbformat": 4,
|
328 |
+
"vscode": {
|
329 |
+
"interpreter": {
|
330 |
+
"hash": "62556f7a043365a66e0918c892755cfafede529a87e97207556f006a109bade4"
|
331 |
+
}
|
332 |
+
}
|
333 |
+
},
|
334 |
+
"nbformat": 4,
|
335 |
+
"nbformat_minor": 0
|
336 |
+
}
|