{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "AbsjF_-0Lort" }, "source": [ "**Import the necessary modules and prepare the data**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "k5QR71xFLGkh" }, "outputs": [], "source": [ "import statsmodels.formula.api as smf\n", "import pandas as pd\n", "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "RpkR60AxLNkl" }, "outputs": [], "source": [ "rawBostonData = pd.read_csv('../Dataset/Boston.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "2Qwjq3ODLWUF" }, "outputs": [], "source": [ "rawBostonData = rawBostonData.dropna()\n", "rawBostonData = rawBostonData.drop_duplicates() \n", "renamedBostonData = rawBostonData.rename(columns = {'CRIM':'crimeRatePerCapita',\n", " ' ZN ':'landOver25K_sqft',\n", " 'INDUS ':'non-retailLandProptn',\n", " 'CHAS':'riverDummy',\n", " 'NOX':'nitrixOxide_pp10m',\n", " 'RM':'AvgNo.RoomsPerDwelling',\n", " 'AGE':'ProptnOwnerOccupied',\n", " 'DIS':'weightedDist',\n", " 'RAD':'radialHighwaysAccess',\n", " 'TAX':'propTaxRate_per10K',\n", " 'PTRATIO':'pupilTeacherRatio',\n", " 'LSTAT':'pctLowerStatus',\n", " 'MEDV':'medianValue_Ks'})\n", "X = renamedBostonData.drop('crimeRatePerCapita', axis = 1)\n", "y = renamedBostonData[['crimeRatePerCapita']]\n", "seed = 10 \n", "test_data_size = 0.3 \n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = test_data_size, random_state = seed)\n", "train_data = pd.concat([X_train, y_train], axis = 1)\n", "test_data = pd.concat([X_test, y_test], axis = 1)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3B9t1_oXO6GB" }, "source": [ "**Exercise 2.05: Fit a multiple linear regression model using the Statsmodels formula API** " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 493 }, "colab_type": "code", "id": "o_CbjkexLfyy", "outputId": "bb48461b-d6aa-43f3-d810-bc3d520928ad" }, "outputs": [], "source": [ "multiLinearModel = smf.ols(formula=\\\n", "'crimeRatePerCapita ~ pctLowerStatus + radialHighwaysAccess +\\\n", "medianValue_Ks + nitrixOxide_pp10m', data=train_data)\n", "multiLinearModResult = multiLinearModel.fit()\n", "print(multiLinearModResult.summary())" ] } ], "metadata": { "colab": { "name": "Exercise2_05.ipynb", "provenance": [] }, "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.8.6" } }, "nbformat": 4, "nbformat_minor": 1 }