{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Regression using statsmodels\n", "\n", "Author: Carlos J. Costa, ISEG\n", "\n", "Purpose: Identify the weight of several features in the home prices in Boston.\n", "\n", "**1** import libraries needed: sklearn and pandas\n", "\n", "**2** use boston dataset from https://scikit-learn.org/stable/datasets/index.html and convert into two dataframes: X for the features and Y for the target.\n", "\n", "**3** Verify 5 lines of the features variables\n", "\n", "**4** Veify datatype\n", "\n", "**5** Create new features variables with only 2 variables\n", "\n", "**6** Create and fit the model:model = sm.OLS(target,sm.add_constant(feature)).fit()\n", "\n", "**7** Obtain summary\n", "\n", "**8** What's the differnece between model.summary2().tables[2] and model.summary().tables[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import statsmodels.api as sm\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import load_boston\n", "boston =load_boston()\n", "X = pd.DataFrame(boston.data, columns=boston.feature_names)\n", "Y = pd.DataFrame(boston.target)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X.dtypes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X1 = X.iloc[:,0:2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X1=sm.add_constant(X1)\n", "model = sm.OLS(Y,X1).fit()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.summary()" ] } ], "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }