File size: 6,247 Bytes
3a794b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "Using regular logistic Regression (Highest Accuracy estimate(so far): 83%"
      ],
      "metadata": {
        "id": "MzqDlYenTCb_"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "VycaGV_zrYz7",
        "collapsed": true
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from tensorflow import keras\n",
        "from keras import layers\n",
        "from sklearn.model_selection import train_test_split\n",
        "import matplotlib.pyplot as plt\n",
        "from tensorflow.keras.utils import to_categorical\n",
        "from sklearn.preprocessing import MinMaxScaler\n",
        "d = pd.read_csv(\"student_lifestyle_dataset.csv\")\n",
        "Y = d[\"Stress_Level\"]\n",
        "dic = {\"Moderate\": 1, \"High\": 2, \"Low\": 0}\n",
        "for i, element in np.ndenumerate(Y):\n",
        "  Y[i,] = dic[element]\n",
        "X = d.drop(\"Stress_Level\", axis=1)\n",
        "scaler = MinMaxScaler()\n",
        "X = scaler.fit_transform(X)\n",
        "x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, train_size=0.8)\n",
        "y_train_one_hot = to_categorical(y_train, num_classes=3)\n",
        "y_test_one_hot = to_categorical(y_test, num_classes=3)\n",
        "w = [np.zeros(x_train.shape[1]) for i in range(3)]\n",
        "b = np.array([0.0 for i in range(3)])\n",
        "\n",
        "def softmax(z):\n",
        "  e_z = np.exp(z - np.max(z))\n",
        "  return e_z/sum(e_z)\n",
        "\n",
        "def forwardPass(weights, x, biases):\n",
        "  z = np.array([np.dot(w, x) + b for w, b in zip(weights, biases)])\n",
        "  a = softmax(z)\n",
        "  return z, a\n",
        "\n",
        "def cross_entropy(y, y_pred):\n",
        "  return -np.sum(y * np.log(y_pred + 1e-4))\n",
        "\n",
        "def backprop(x, y, a):\n",
        "  dz = a-y\n",
        "  dw = [dz[i] * x for i in range(len(dz))]\n",
        "  db = dz\n",
        "  return dw, db\n",
        "\n",
        "epochs = 2000\n",
        "alpha = 0.01\n",
        "for epoch in range(epochs):\n",
        "  total_loss = 0\n",
        "  for x, y in zip(x_train, y_train_one_hot):\n",
        "    z, a = forwardPass(w, x, b)\n",
        "    loss = cross_entropy(y, a)\n",
        "    total_loss += loss\n",
        "    dw, db = backprop(x, y, a)\n",
        "    for i in range(3):\n",
        "      w[i] -= alpha * dw[i]\n",
        "      b[i] -= alpha * db[i]\n",
        "  print(\"Loss at Epoch\" + str(epoch +1)+ \": \" + str(total_loss/len(x_train)))"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def predict(x, w, b):\n",
        "  _, a = forwardPass(w, x, b)\n",
        "  return np.argmax(a)\n",
        "\n",
        "predictions = [predict(x, w, b) for x in x_test]\n",
        "from sklearn.metrics import accuracy_score\n",
        "y_test = np.array(y_test).astype(np.int64)\n",
        "predictions = np.array(predictions).astype(np.int64)\n",
        "accuracy = accuracy_score(y_test, predictions)\n",
        "print(\"Accuracy on test set:\", accuracy)"
      ],
      "metadata": {
        "id": "90O27eYWOBeB"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Using Neural Network Model Highest Accuracy estimate (so far): 98%-97%"
      ],
      "metadata": {
        "id": "zbK7PhT-TZXw"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from tensorflow import keras\n",
        "from keras import layers\n",
        "from sklearn.model_selection import train_test_split\n",
        "import matplotlib.pyplot as plt\n",
        "from tensorflow.keras.utils import to_categorical\n",
        "d = pd.read_csv(\"student_lifestyle_dataset.csv\")\n",
        "from sklearn.preprocessing import MinMaxScaler\n",
        "from tensorflow.keras.optimizers.schedules import ExponentialDecay\n",
        "from tensorflow.keras.optimizers import Adam\n",
        "Y = d[\"Stress_Level\"]\n",
        "X = d.drop(\"Stress_Level\", axis=1)\n",
        "scaler = MinMaxScaler()\n",
        "X = scaler.fit_transform(X)\n",
        "\n",
        "dic = {\"Moderate\": 1, \"High\": 2, \"Low\": 0}\n",
        "print(Y.shape)\n",
        "for i, element in np.ndenumerate(Y):\n",
        "  Y[i,] = dic[element]\n",
        "print(Y)\n",
        "x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, train_size=0.8)\n",
        "\n",
        "y_train = y_train.astype(np.int64)\n",
        "y_test = y_test.astype(np.int64)\n",
        "model = keras.Sequential(\n",
        "    [\n",
        "        layers.Dense(activation=\"relu\", units=125, kernel_regularizer=keras.regularizers.l2(0.01)),\n",
        "        layers.Dense(activation=\"relu\", units=75, kernel_regularizer=keras.regularizers.l2(0.01)),\n",
        "        layers.Dense(activation=\"relu\", units=15, kernel_regularizer=keras.regularizers.l2(0.01)),\n",
        "        layers.Dense(activation=\"softmax\", units=3)\n",
        "    ]\n",
        ")\n",
        "\n",
        "\n",
        "\n",
        "optimizer = Adam(learning_rate=0.001)\n",
        "\n",
        "model.compile(\n",
        "    loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n",
        "    optimizer=optimizer,\n",
        "    metrics=['accuracy']\n",
        ")\n",
        "\n",
        "model.fit(\n",
        "    x_train,\n",
        "    y_train,\n",
        "    validation_data=(x_test, y_test),\n",
        "    epochs=200\n",
        ")\n"
      ],
      "metadata": {
        "id": "A1mAPsTfO-v3"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}