File size: 19,567 Bytes
75022ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Hpi-lEQsxEyJ"
      },
      "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": "vZYY_LWRxEyN"
      },
      "source": [
        "### Question 1 - Feature Engineering (5 points)\n",
        "\n",
        "Write the posterior probability expressions for logistic regression for the problem you are given to solve."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_Pzq9mohxEyO"
      },
      "source": [
        "$$p(y=1| \\mathbf{x}, \\mathbf w)=\\frac{1}{1+e^{-w^{T}x}}=\\frac{e^{w^{T}x}}{1+e^{w^{T}x}}$$ \n",
        "\n",
        "$$p(y=0| \\mathbf{x}, \\mathbf w)=1-\\frac{1}{1+e^{-w^{T}x}}=1-\\frac{e^{w^{T}x}}{1+e^{w^{T}x}}$$ "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nwdYXqk9xEyP"
      },
      "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": "VuOrNLFTxEyQ"
      },
      "source": [
        "$$w_{0}+w_{1}x_{1}+w_{2}x_{2}+...+w_{d}x_{d}=0$$ Where $d$ is the number of features."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QSL1VoIkxEyR"
      },
      "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": "Zd-TyoPGxEyR"
      },
      "source": [
        "$$L_{CE} = \\ln(\\prod_{i=1}^{N}\\sigma(w^{T}x)^{y_i}(1-\\sigma(w^{T}x))^{1-y_i})=\\sum_{i=1}^{N}\\ln(\\sigma(w^{T}x)^{y_i}(1-\\sigma(w^{T}x))^{1-y_i})$$ I'm using the log function for mathmatical convenience when taking the gradiet in the following step. Since the log function is always increasing, a function and the logarithm of that function will have the same optimal values for $w$."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BIvuYZfgxEyS"
      },
      "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": "0ORR1odKxEyT"
      },
      "source": [
        "$$ \\nabla_\\mathbf w L_{CE} = \\nabla_{w} \\sum_{i=1}^{N}\\ln(\\sigma(w^{T}x)^{y_i}(1-\\sigma(w^{T}x))^{1-y_i})=\\sum_{i=1}^{N}y_i\\ln(\\sigma(w^{T}x)+\\sum_{i=1}^{N}(1-y_i)\\ln(1-\\sigma(w^{T}x))$$\n",
        "Since the derivative of the sigmoid function is as follows:\n",
        "$$\\frac{d\\sigma(z)}{dz}=\\frac{1}{1+e^{-z}}(1-\\frac{1}{1+e^{-z}})= \\sigma(z)(1-\\sigma(z))$$\n",
        "Then the gradient of the loss with respect to $w_j$ are as follows:\n",
        "$$\\sum_{i=1}^{N}(y^{(i)}\\cdot1-y^{(i)}\\cdot\\sigma(w^{T}x^{(i)})-1\\cdot\\sigma(w^{T}x^{(i)})+y^{(i)}\\cdot\\sigma(w^{T}x^{(i)}))x_{j}^{(i)}=\\sum_{i=1}^{N}(y^{(i)}-\\sigma(w^{T}x^{(i)}))x_{j}^{(i)}$$\n",
        "The gradient of the loss with respect to $w_j$ is:\n",
        "$$\\sum_{i=1}^{N}(y^{(i)}-\\sigma(w^{T}x^{(i)}))x_{j}^{(i)}$$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KQ4QJXrTxEyT"
      },
      "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": "sZQzEx5txEyU"
      },
      "source": [
        "If a dataset used to train a logistic regression model is imbalanced, it can cause the model to pickup and expect patterns that are true in the training data, but may not be accurate of the true relationship between features. For example, if a test set contains samples that are 98% negative in the 'parasite_status' column, then a model that classifies every sample as negative reagrdless of the other features will perform with 98% accuracy, which is misleadingly good accuracy for such a poor model. It can also lead to the model classifying more future examples as false negatives, given more of the training data is saw was negative. Depending on the use case in the real world, Type I or Type II error may be favored, but in any case, a model should not have to favor one over the other simply because the data is was trained on was imbalanced. The true effects of this would be evident when the model is tested on validation sets and new test data after it is trained."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zRtEkkx5xEyU"
      },
      "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": 6,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 575
        },
        "id": "kPur3Q4CxEyV",
        "outputId": "1b9c4dc9-179b-4d13-83c5-79a38b47ef58"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 92.3841% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 92.7152% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 93.2119% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 94.0397% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 91.8874% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 93.0464% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 92.053% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 92.053% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 94.8675% of the time, with an F1 score of 0.0\n",
            "Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted 93.0464% of the time, with an F1 score of 0.0\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.9/dist-packages/sklearn/linear_model/_logistic.py:458: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
            "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
            "\n",
            "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
            "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
            "Please also refer to the documentation for alternative solver options:\n",
            "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
            "  n_iter_i = _check_optimize_result(\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEGCAYAAABLgMOSAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAAsTAAALEwEAmpwYAAASFElEQVR4nO3dfZBddX3H8fenRKmPPEaKBAzWVBq1PswVfGrLKCLWh9CWGaEPZiw1tRW1WkdjnRGlTgdaq9VqnaaiUmVEB6um2oqIUq1aZBMRiZQmRZEgSJQURUcR/PaPeyI362az/LJ3z97u+zVz557f7/zuOd89M7ufPefcc06qCkmS7q6f67sASdJkMkAkSU0MEElSE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JQNE2jdnA7/aXdz50u4ZJX+d5PIkVyb5I4Akxyf5bJKNdFeJJ/lwkk3dsyvWdX1nM7yT7BVJzu/6du3tpFv2VUm+kuQ5I8u+dOR5IOd3V2FLY7W3/4QkzW498PKqeiZAFwS3VtVjk+wPfC7JrrvBPgZ4eFV9rWv/QVXdkuRewOVJPlhV65OcUVWPmmFdv8XwKvNHAod2n/lMN+/RwMMY3rr8cwzvXvAf8/3DSqPcA5Hm14nAc5NcwfCW+4cAq7p5XxwJD4AXJ/ky8J/AkSPj9uRJwPuq6s6q+hbw78BjR5a9vap+AlzB8NCaNFbugUjzK8CLquqi3TqT4xneZn20fQLw+Kr6QZJLgX159OqPRqbvxN9tLQD3QKR98z3gfiPti4A/7m6/T5Jf2sPDnQ4AdnbhcQzDRxXv8uNdn5/ms8BzuvMsyxk+hfCL8/JTSA38L0XaN1cCd3aHot7N8NnkK4HN3YnsHcz8+NCPAy9IcjVwDcPDWLtsAK5Msrmqfnek/0PA44EvM3wg0Cuq6qYugKQF5914JUlNPIQlSWpigEiSmhggkqQmBogkqYkBIklqYoBIkpoYIJKkJv8H7O99GVpZGGUAAAAASUVORK5CYII=\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "source": [
        "import numpy as np\n",
        "from sklearn import preprocessing\n",
        "import pandas as pd\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.linear_model import LogisticRegression\n",
        "from sklearn.metrics import f1_score\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "df = pd.read_csv('CBC_data.csv')\n",
        "\n",
        "# preprocessing via one hot encoding\n",
        "df_obj = df.select_dtypes(include=[object])\n",
        "pre = preprocessing.LabelEncoder()\n",
        "df_obj = df_obj.apply(pre.fit_transform)\n",
        "df['ID'] = df_obj['ID']\n",
        "df['SEX']= df_obj['SEX']\n",
        "df['TYPEAREA']= df_obj['TYPEAREA']\n",
        "df['SEX.REPRO']= df_obj['SEX.REPRO']\n",
        "df['REPRO.STATUS']= df_obj['REPRO.STATUS']\n",
        "df['PARASITE_STATUS']= df_obj['PARASITE_STATUS']\n",
        "\n",
        "\n",
        "# features used: ID, SEX, TYPEAREA, REPRO.STATE, AGE\n",
        "X = df.iloc[:, [0,1,2,4,5]]\n",
        "# target variable\n",
        "y = df['PARASITE_STATUS']\n",
        "\n",
        "# ITERATIONS \n",
        "t = 10\n",
        "x = [1,2,3,4,5,6,7,8,9,10]\n",
        "f1 = []\n",
        "\n",
        "for i in range(t):\n",
        "  # trains on 80% of data, tests of remaining 20%\n",
        "  x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
        "  lr = LogisticRegression()\n",
        "  lr.fit(x_train, y_train)\n",
        "  predictions = lr.predict(x_test)\n",
        "  score = lr.score(x_test, y_test)\n",
        "  f1.append(f1_score(y_test.tolist(), predictions))\n",
        "  print('Based on ID, SEX, TYPEAREA, REPRO.STATE, & AGE, PARASITE_STATUS was correctly predicted {}% of the time, with an F1 score of {}'.format(round(score*100, 4), f1_score(y_test.tolist(), predictions)))\n",
        "\n",
        "\n",
        "plt.xlabel('Iteration')\n",
        "plt.ylabel('F1 Score')\n",
        "plt.plot(x,f1)\n",
        "plt.show()\n",
        "\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.6"
    },
    "orig_nbformat": 4,
    "vscode": {
      "interpreter": {
        "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
      }
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}