Spaces:
Runtime error
Runtime error
File size: 19,325 Bytes
61d4541 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "2IdTjyqZHKK5"
},
"source": [
"# Midterm - Spring 2023\n",
"\n",
"## Problem 1: Take-at-home (45 points total)\n",
"\n",
"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",
"\n",
"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",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fp6F7zIuHKK-"
},
"source": [
"### Question 1 - Feature Engineering (5 points)\n",
"\n",
"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",
"\n",
"Write the posterior probability expressions for logistic regression for the problem you are given to solve."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZDRhz6bDHKLA"
},
"source": [
"$$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",
"\n",
"$$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",
"\n",
"the equeation for alpha is shown below. $$\\alpha=ln\\frac{p(x|y=1)p(y=1)}{p(x|y=0)p(y=0)}$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zXYe2qtSHKLB"
},
"source": [
"\n",
"\n",
"### Question 2 - Decision Boundary (5 points)\n",
"\n",
"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",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5sM9vvj3HKLC"
},
"source": [
"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",
"\n",
"$$p(y=1|x,w)=p(y=0|x,w)$$\n",
"\n",
"which can also be written as the following:\n",
"\n",
"$$\\sigma(\\alpha)=1-\\sigma(\\alpha)$$\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uY7lH1geHKLD"
},
"source": [
"\n",
"\n",
"### Question 3 - Loss function (5 points)\n",
"\n",
"Write the expression of the loss as a function of $\\mathbf w$ that makes sense for you to use in this problem. \n",
"\n",
"NOTE: The loss will be a function that will include this function: \n",
"\n",
"$$\\sigma(a) = \\frac{1}{1+e^{-a}}$$\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "or-qkG4MHKLE"
},
"source": [
"$$L_{CE} = -\\Sigma^m_{i=1}[y_i\\ln(σ(w^Τx_i))+(1-y_i)\\ln(1-\\sigma(w^Τx_i)] $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qq1aXGcMHKLF"
},
"source": [
"\n",
"### Question 4 - Gradient (5 points)\n",
"\n",
"Write the expression of the gradient of the loss with respect to the parameters - show all your work.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fZklUA8PHKLH"
},
"source": [
"$$ \\nabla_\\mathbf w L_{CE} =\\Sigma^m_{i=1}[(σ(w^Τx_i)-y_i)x_i]$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WHj0C8T7HKLI"
},
"source": [
"### Question 5 - Imbalanced dataset (10 points)\n",
"\n",
"You are now told that in the dataset \n",
"\n",
"$$p(y=0) >> p(y=1)$$\n",
"\n",
"Can you comment if the accuracy of Logistic Regression will be affected by such imbalance?\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ft7xpMVIHKLK"
},
"source": [
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iE0WczaxHKLL"
},
"source": [
"\n",
"### Question 6 - SGD (15 points)\n",
"\n",
"The interviewer was impressed with your answers and wants to test your programming skills. \n",
"\n",
"1. Use the dataset to train a logistic regressor that will predict the target variable $y$. \n",
"\n",
" 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",
"\n",
"$$F_1 = \\frac{2}{r^{-1} + p^{-1}}$$\n",
"\n",
"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",
"\n",
"You are allowed to use any library you want to code this problem.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 321
},
"id": "keQIm2V6HKLN",
"outputId": "0a4ae008-a656-4b91-c40f-b80653b25364"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"w vector: [[-1.84834009e-03 -4.54038579e-03 7.88831514e-06 1.83512257e-03\n",
" 3.41803018e-03 -2.00747551e-03 -3.22278484e-03 1.61564091e-04]]\n"
]
},
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x7ff0072dc520>"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAAsTAAALEwEAmpwYAAAUkUlEQVR4nO3df2xd533f8fdnlOxwyWYlDltUlFOpsKtBgTNruVVSJMsyB46U/ohU123sdKizGXCLzViHNuqkFVgTD4WTeYhbYP5jRp3NddfagetowtJO8+oAHYLUNWWlUWVXjeK6sei0VmwrXVYttpTv/uCRQzEkdUle6lIP3y+A4D3Pee59vs89h59zcc7lvakqJEnt+lvDLkCStLwMeklqnEEvSY0z6CWpcQa9JDVuzbALmOmNb3xjbdy4cdhlSNJF5eDBg1+rqrHZ1q24oN+4cSMTExPDLkOSLipJ/mKudZ66kaTGGfSS1DiDXpIaZ9BLUuMMeklq3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uBPWMK+3sx/xs72BebDrWj5JPlt4N1MfXTpXwG/DOwDPgW8iamPef7Jqpp5wfaileSdwP8GDvPtc7b/hqnz9C3P+y1MXXgbYeqF2aeq6vYk38fUK903AIeAf1JV3xxepcunO3Xz4ar6kdUw726On+4W1wC/VVW/kuRyFrmvNxP0kqTZtXLqRpI0B4Nekhpn0EtS4wx6SWqcQS9JjTPoJalxBr0kNe7/A9ZCdgVOdEY7AAAAAElFTkSuQmCC",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"from numpy.core.fromnumeric import size\n",
"# write your code here\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn import datasets\n",
"from sklearn import svm\n",
"\n",
"data=pd.read_csv(\"./CBC_data.csv\").dropna()\n",
"y=data[[\"PARASITE_STATUS\"]].values\n",
"X=data[[\"RBC\",\"HGB\",\"WBC\",\"EOS.CNT\",\"MONO.CNT\",\"NUT.CNT\",\"PL.CNT\",\"LYMP.CNT\"]]\n",
"\n",
"n_epochs=50\n",
"eta=3*10**-7\n",
"ones=[1,1,1,1,1,1,1,1]\n",
"w=np.matrix(np.zeros(X.shape[1]))\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)\n",
"\n",
"m=y_train.size\n",
"\n",
"def sig_fun(w, X):\n",
" output = 1.0/(1 + np.exp(-np.dot(w,X.T)))\n",
" #print(np.exp(-np.dot(w,X.T)),\" - \",w,X.T)\n",
" return output\n",
"\n",
"\n",
"def grad_fun(w,X,y):\n",
" #print(np.dot((sig_fun(w,X)-y),X))\n",
" return np.dot((sig_fun(w,X)-y),X)\n",
"\n",
"def pred_values(w, X):\n",
" probabilities = sig_fun(w, X)\n",
" pred_value = np.where(probabilities >= .5, 1, 0) \n",
" return np.squeeze(pred_value)\n",
"\n",
"def f1_score(X_test,y_test,w):\n",
" predictions = pred_values(w,X_test)\n",
" fp=0;tp=0;fn=0\n",
" for i in range(y_test.size):\n",
" yi=1 if (y_train[i])==(\"Positive\") else 0\n",
" #print(yi,predictions[i])\n",
" if(yi==1):\n",
" if(predictions[i]==1):\n",
" tp+=1\n",
" else:\n",
" fn+=1\n",
" else:\n",
" if(predictions[i]==1):\n",
" fp+=1 \n",
" #print(fp,fn,tp)\n",
" if(tp==0):\n",
" return 0\n",
" recall=tp/(tp+fn)\n",
" percision=tp/(tp+fp)\n",
" return 2/(percision**-1+recall**-1)\n",
"\n",
"\n",
"iteration_number=[]\n",
"F_score_v_iteration=[]\n",
"\n",
"\n",
"for epoch in range(n_epochs):\n",
" for iteration in range(m):\n",
" random_index=np.random.randint(m)\n",
" xi=X_train[random_index:random_index+1].values\n",
" #if(type(np.dot(ones,xi.T)[0])!=type(1)):\n",
" # continue\n",
" #print(xi)\n",
" yi=1 if (y_train[random_index:random_index+1][0])==(\"Positive\") else 0\n",
" gradients=grad_fun(w,xi,yi)\n",
" w=w-eta*gradients\n",
" iteration_number.append(epoch)\n",
" F_score_v_iteration.append(f1_score(X_test,y_test,w))\n",
" #print(w,gradients)\n",
" #print(yi,(y_train[random_index:random_index+1][0]))\n",
"\n",
"print(\"w vector: \",w)\n",
"plt.scatter(iteration_number,F_score_v_iteration)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aOh-_F2u-Fvt"
},
"source": [
"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."
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "ai-course",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10.8"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "62556f7a043365a66e0918c892755cfafede529a87e97207556f006a109bade4"
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|