Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# ASSIGNMENT - 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "c:\\Users\\soniv\\anaconda3\\lib\\site-packages\\numpy\\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:\n",
- "c:\\Users\\soniv\\anaconda3\\lib\\site-packages\\numpy\\.libs\\libopenblas.FB5AE2TYXYH2IJRDKGDGQ3XBKLKTF43H.gfortran-win_amd64.dll\n",
- "c:\\Users\\soniv\\anaconda3\\lib\\site-packages\\numpy\\.libs\\libopenblas64__v0.3.21-gcc_10_3_0.dll\n",
- " warnings.warn(\"loaded more than 1 DLL from .libs:\"\n"
- ]
- }
- ],
- "source": [
- "from tensorflow import keras as tf_keras\n",
- "import numpy as np \n",
- "import pandas as pd \n",
- "from keras.layers import Dense\n",
- "from keras.models import Sequential"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/boston_housing.npz\n",
- "57026/57026 [==============================] - 0s 2us/step\n"
- ]
- }
- ],
- "source": [
- "(X_train, y_train), (X_test, y_test) = tf_keras.datasets.boston_housing.load_data(\n",
- " path=\"boston_housing.npz\", test_split=0.2, seed=42\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(404, 13) <class 'numpy.ndarray'>\n",
- "(404,) <class 'numpy.ndarray'>\n",
- "(102, 13) <class 'numpy.ndarray'>\n",
- "(102,) <class 'numpy.ndarray'>\n"
- ]
- }
- ],
- "source": [
- "print(X_train.shape, type(X_train))\n",
- "print(y_train.shape, type(y_train))\n",
- "print(X_test.shape, type(X_test))\n",
- "print(y_test.shape, type(y_test))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "papermill": {
- "duration": 0.052899,
- "end_time": "2020-10-07T20:56:02.160829",
- "exception": false,
- "start_time": "2020-10-07T20:56:02.107930",
- "status": "completed"
- },
- "tags": []
- },
- "source": [
- "<a id = 'NN'></a>\n",
- "## Neural Networks"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "execution": {
- "iopub.execute_input": "2020-10-07T20:56:02.279947Z",
- "iopub.status.busy": "2020-10-07T20:56:02.279001Z",
- "iopub.status.idle": "2020-10-07T20:56:02.291535Z",
- "shell.execute_reply": "2020-10-07T20:56:02.290743Z"
- },
- "papermill": {
- "duration": 0.077294,
- "end_time": "2020-10-07T20:56:02.291688",
- "exception": false,
- "start_time": "2020-10-07T20:56:02.214394",
- "status": "completed"
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "#Scaling the dataset\n",
- "from sklearn.preprocessing import StandardScaler\n",
- "sc = StandardScaler()\n",
- "X_train = sc.fit_transform(X_train)\n",
- "X_test = sc.transform(X_test)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [],
- "source": [
- "X_train = pd.DataFrame(X_train)\n",
- "X_test = pd.DataFrame(X_test)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "execution": {
- "iopub.execute_input": "2020-10-07T20:56:02.521142Z",
- "iopub.status.busy": "2020-10-07T20:56:02.520202Z",
- "iopub.status.idle": "2020-10-07T20:56:02.813516Z",
- "shell.execute_reply": "2020-10-07T20:56:02.812803Z"
- },
- "papermill": {
- "duration": 0.36162,
- "end_time": "2020-10-07T20:56:02.813669",
- "exception": false,
- "start_time": "2020-10-07T20:56:02.452049",
- "status": "completed"
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "model = Sequential()\n",
- "\n",
- "model.add(Dense(128,activation = 'relu',input_dim =13))\n",
- "model.add(Dense(64,activation = 'relu'))\n",
- "model.add(Dense(32,activation = 'relu'))\n",
- "model.add(Dense(16,activation = 'relu'))\n",
- "model.add(Dense(1))\n",
- "model.compile(optimizer = 'adam',loss = 'mean_squared_error')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "execution": {
- "iopub.execute_input": "2020-10-07T20:56:02.934653Z",
- "iopub.status.busy": "2020-10-07T20:56:02.933663Z",
- "iopub.status.idle": "2020-10-07T20:56:07.137189Z",
- "shell.execute_reply": "2020-10-07T20:56:07.137910Z"
- },
- "papermill": {
- "duration": 4.268213,
- "end_time": "2020-10-07T20:56:07.138097",
- "exception": false,
- "start_time": "2020-10-07T20:56:02.869884",
- "status": "completed"
- },
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Epoch 1/100\n",
- "13/13 [==============================] - 2s 3ms/step - loss: 569.1288\n",
- "Epoch 2/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 494.4398\n",
- "Epoch 3/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 319.8408\n",
- "Epoch 4/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 105.5404\n",
- "Epoch 5/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 63.4671\n",
- "Epoch 6/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 37.5063\n",
- "Epoch 7/100\n",
- "13/13 [==============================] - 0s 8ms/step - loss: 28.0821\n",
- "Epoch 8/100\n",
- "13/13 [==============================] - 0s 8ms/step - loss: 23.9037\n",
- "Epoch 9/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 21.2490\n",
- "Epoch 10/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 19.5451\n",
- "Epoch 11/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 18.2643\n",
- "Epoch 12/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 17.0230\n",
- "Epoch 13/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 16.1230\n",
- "Epoch 14/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 15.3916\n",
- "Epoch 15/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 14.6173\n",
- "Epoch 16/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 13.9202\n",
- "Epoch 17/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 13.5544\n",
- "Epoch 18/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 13.1145\n",
- "Epoch 19/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 12.5996\n",
- "Epoch 20/100\n",
- "13/13 [==============================] - 0s 9ms/step - loss: 12.3431\n",
- "Epoch 21/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 11.8682\n",
- "Epoch 22/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 11.6428\n",
- "Epoch 23/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 11.4789\n",
- "Epoch 24/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 11.1080\n",
- "Epoch 25/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 10.8256\n",
- "Epoch 26/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 10.6871\n",
- "Epoch 27/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 10.6583\n",
- "Epoch 28/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 10.3711\n",
- "Epoch 29/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 10.1583\n",
- "Epoch 30/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 10.0381\n",
- "Epoch 31/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 9.9237\n",
- "Epoch 32/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 9.6011\n",
- "Epoch 33/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 9.4041\n",
- "Epoch 34/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 9.3129\n",
- "Epoch 35/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 9.0364\n",
- "Epoch 36/100\n",
- "13/13 [==============================] - 0s 9ms/step - loss: 9.1486\n",
- "Epoch 37/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 8.8286\n",
- "Epoch 38/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 8.7360\n",
- "Epoch 39/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 8.6275\n",
- "Epoch 40/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 8.4064\n",
- "Epoch 41/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 8.1975\n",
- "Epoch 42/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 8.0858\n",
- "Epoch 43/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 7.9936\n",
- "Epoch 44/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 7.9371\n",
- "Epoch 45/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 7.6954\n",
- "Epoch 46/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 7.6290\n",
- "Epoch 47/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 7.3993\n",
- "Epoch 48/100\n",
- "13/13 [==============================] - 0s 8ms/step - loss: 7.2998\n",
- "Epoch 49/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 7.0291\n",
- "Epoch 50/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 7.1075\n",
- "Epoch 51/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 7.0854\n",
- "Epoch 52/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 7.1318\n",
- "Epoch 53/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 7.5909\n",
- "Epoch 54/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 6.7638\n",
- "Epoch 55/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 6.4285\n",
- "Epoch 56/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 6.2816\n",
- "Epoch 57/100\n",
- "13/13 [==============================] - 0s 10ms/step - loss: 6.2752\n",
- "Epoch 58/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 6.1827\n",
- "Epoch 59/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 6.3984\n",
- "Epoch 60/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 6.1120\n",
- "Epoch 61/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 5.8841\n",
- "Epoch 62/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 5.7373\n",
- "Epoch 63/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 5.7480\n",
- "Epoch 64/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 5.5833\n",
- "Epoch 65/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 5.4510\n",
- "Epoch 66/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 5.5317\n",
- "Epoch 67/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 5.2579\n",
- "Epoch 68/100\n",
- "13/13 [==============================] - 0s 2ms/step - loss: 5.3661\n",
- "Epoch 69/100\n",
- "13/13 [==============================] - 0s 2ms/step - loss: 5.1459\n",
- "Epoch 70/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 5.3962\n",
- "Epoch 71/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 4.9652\n",
- "Epoch 72/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 4.8648\n",
- "Epoch 73/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 4.9541\n",
- "Epoch 74/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 4.6797\n",
- "Epoch 75/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 4.6717\n",
- "Epoch 76/100\n",
- "13/13 [==============================] - 0s 2ms/step - loss: 4.5783\n",
- "Epoch 77/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 4.5508\n",
- "Epoch 78/100\n",
- "13/13 [==============================] - 0s 2ms/step - loss: 4.5492\n",
- "Epoch 79/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 4.5550\n",
- "Epoch 80/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 4.4583\n",
- "Epoch 81/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 4.6703\n",
- "Epoch 82/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 4.9047\n",
- "Epoch 83/100\n",
- "13/13 [==============================] - 0s 8ms/step - loss: 4.1940\n",
- "Epoch 84/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 4.1827\n",
- "Epoch 85/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 4.1972\n",
- "Epoch 86/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 4.1678\n",
- "Epoch 87/100\n",
- "13/13 [==============================] - 0s 11ms/step - loss: 4.0276\n",
- "Epoch 88/100\n",
- "13/13 [==============================] - 0s 12ms/step - loss: 4.2794\n",
- "Epoch 89/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 4.1283\n",
- "Epoch 90/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 4.5400\n",
- "Epoch 91/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 4.0902\n",
- "Epoch 92/100\n",
- "13/13 [==============================] - 0s 7ms/step - loss: 4.1553\n",
- "Epoch 93/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 4.2422\n",
- "Epoch 94/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 3.8847\n",
- "Epoch 95/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 3.8599\n",
- "Epoch 96/100\n",
- "13/13 [==============================] - 0s 8ms/step - loss: 3.7208\n",
- "Epoch 97/100\n",
- "13/13 [==============================] - 0s 4ms/step - loss: 3.8535\n",
- "Epoch 98/100\n",
- "13/13 [==============================] - 0s 5ms/step - loss: 3.6761\n",
- "Epoch 99/100\n",
- "13/13 [==============================] - 0s 3ms/step - loss: 3.4122\n",
- "Epoch 100/100\n",
- "13/13 [==============================] - 0s 6ms/step - loss: 3.5394\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "<keras.callbacks.History at 0x240929ddb80>"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "model.fit(X_train, y_train, epochs = 100)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "papermill": {
- "duration": 0.144695,
- "end_time": "2020-10-07T20:56:07.429767",
- "exception": false,
- "start_time": "2020-10-07T20:56:07.285072",
- "status": "completed"
- },
- "tags": []
- },
- "source": [
- "<a id = 'eval'></a>\n",
- "### Evaluation of the model"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {
- "execution": {
- "iopub.execute_input": "2020-10-07T20:56:07.725859Z",
- "iopub.status.busy": "2020-10-07T20:56:07.724391Z",
- "iopub.status.idle": "2020-10-07T20:56:07.944661Z",
- "shell.execute_reply": "2020-10-07T20:56:07.945236Z"
- },
- "papermill": {
- "duration": 0.370667,
- "end_time": "2020-10-07T20:56:07.945463",
- "exception": false,
- "start_time": "2020-10-07T20:56:07.574796",
- "status": "completed"
- },
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "4/4 [==============================] - 0s 3ms/step\n"
- ]
- }
- ],
- "source": [
- "y_pred = model.predict(X_test)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {
- "execution": {
- "iopub.execute_input": "2020-10-07T20:56:08.252035Z",
- "iopub.status.busy": "2020-10-07T20:56:08.251041Z",
- "iopub.status.idle": "2020-10-07T20:56:08.255851Z",
- "shell.execute_reply": "2020-10-07T20:56:08.255145Z"
- },
- "papermill": {
- "duration": 0.159132,
- "end_time": "2020-10-07T20:56:08.255991",
- "exception": false,
- "start_time": "2020-10-07T20:56:08.096859",
- "status": "completed"
- },
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0.856404959522691\n"
- ]
- }
- ],
- "source": [
- "from sklearn.metrics import r2_score\n",
- "r2 = r2_score(y_test, y_pred)\n",
- "print(r2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "execution": {
- "iopub.execute_input": "2020-10-07T20:56:08.557357Z",
- "iopub.status.busy": "2020-10-07T20:56:08.556409Z",
- "iopub.status.idle": "2020-10-07T20:56:08.560011Z",
- "shell.execute_reply": "2020-10-07T20:56:08.560689Z"
- },
- "papermill": {
- "duration": 0.156747,
- "end_time": "2020-10-07T20:56:08.560855",
- "exception": false,
- "start_time": "2020-10-07T20:56:08.404108",
- "status": "completed"
- },
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "3.1975390155396077\n"
- ]
- }
- ],
- "source": [
- "# Predicting RMSE the Test set results\n",
- "from sklearn.metrics import mean_squared_error\n",
- "rmse = (np.sqrt(mean_squared_error(y_test, y_pred)))\n",
- "print(rmse)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "papermill": {
- "duration": 0.145421,
- "end_time": "2020-10-07T20:56:08.851425",
- "exception": false,
- "start_time": "2020-10-07T20:56:08.706004",
- "status": "completed"
- },
- "tags": []
- },
- "source": [
- "<a id = 'conclude'></a>\n",
- "## Conclusion\n",
- "\n",
- "Using a simple neural network, we were able to improve the model significantly. I encourage you to try alterating the hyperparameters of the model and see if you can get better model"
- ]
- }
- ],
- "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.12"
- },
- "papermill": {
- "duration": 28.602522,
- "end_time": "2020-10-07T20:56:09.403032",
- "environment_variables": {},
- "exception": null,
- "input_path": "__notebook__.ipynb",
- "output_path": "__notebook__.ipynb",
- "parameters": {},
- "start_time": "2020-10-07T20:55:40.800510",
- "version": "2.1.0"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement