# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="CazISR8X_HUG"
# # Multiple Linear Regression

# + [markdown] id="pOyqYHTk_Q57"
# ## Importing the libraries

# + executionInfo={"elapsed": 2, "status": "ok", "timestamp": 1639670848888, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="pucEdPkxwcmL"
import numpy as np
# no visual graph in this template, plt is commented out as such
# import matplotlib.pyplot as plt
import pandas as pd

# + [markdown] id="vgC61-ah_WIz"
# ## Importing the dataset

# + executionInfo={"elapsed": 121, "status": "ok", "timestamp": 1639670877918, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="8VPGXXaCwe_B"
dataset = pd.read_csv('50_Startups.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"elapsed": 123, "status": "ok", "timestamp": 1639670882036, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="QSFURpWbyfxs" outputId="d7809313-818e-4cc6-ebaa-8843501dc04b"
print(X)

# + [markdown] id="VadrvE7s_lS9"
# ## Encoding categorical data

# + executionInfo={"elapsed": 575, "status": "ok", "timestamp": 1639670884525, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="cJRkYYhqw-ob"
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[("encoder", OneHotEncoder(), [3])], remainder="passthrough")
X = np.array(ct.fit_transform(X))

# + [markdown] id="ae8IKbtZ-c-V"
# ## (Backward Elimination Technique) Avoiding the Dummy Variable Trap

# + executionInfo={"elapsed": 127, "status": "ok", "timestamp": 1639670885507, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="QGBODw5L-sk1"
# SKIP this section if not using Backward Elimination Technique
X = X[:, 1:] # Manually removes the first column (avoids Dummy Variable Trap)

# + [markdown] id="WemVnqgeA70k"
# ## Splitting the dataset into the Training set and Test set

# + executionInfo={"elapsed": 158, "status": "ok", "timestamp": 1639670886209, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="pUTQ02X6wjqM"
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# + [markdown] id="k-McZVsQBINc"
# ## Training the Multiple Linear Regression model on the Training set

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"elapsed": 173, "status": "ok", "timestamp": 1639670887930, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="js4sZKWq05aU" outputId="1d79c422-908f-4eba-f070-68b50001a45e"
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

# + [markdown] id="xNkXL1YQBiBT"
# ## Predicting the Test set results

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"elapsed": 140, "status": "ok", "timestamp": 1639670888887, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="hhN7ziBf3xBa" outputId="02890b58-e47d-47fd-a6f2-45256edba6df"
y_pred = regressor.predict(X_test)
np.set_printoptions(precision=2) # Precision Output to Two Decimal Points

# Concactenate y prediction with y test models
print(np.concatenate((y_pred.reshape(len(y_pred), 1), y_test.reshape(len(y_test), 1)), 1))

# + [markdown] id="imblynqS-0Tr"
# ## (Backward Elimination Technique) Building the optimal model using Backward Elimination

# + colab={"base_uri": "https://localhost:8080/", "height": 542} executionInfo={"elapsed": 1073, "status": "ok", "timestamp": 1639670891443, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GitFXlIXIV85UFNNQ2ZU350icy8WERr7pefHVUQ4w=s64", "userId": "12594929443311132995"}, "user_tz": 300} id="81hHHnvh_Rv7" outputId="9d9f318a-970a-4b44-86af-fd86e760567b"
# SKIP this section if not using Backward Elimination Technique
import statsmodels.api as sm # Import Stats Model API

X = np.append(arr = np.ones((50, 1)).astype(int), values = X, axis = 1)
X_opt = X[:, [0, 1, 2, 3, 4, 5]]
X_opt = X_opt.astype(np.float64)

regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 1, 3, 4, 5]]
X_opt = X_opt.astype(np.float64)

regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 3, 4, 5]]
X_opt = X_opt.astype(np.float64)

regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 3, 5]]
X_opt = X_opt.astype(np.float64)

regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary().X_opt = X[:, [0, 3]]
X_opt = X_opt.astype(np.float64)
regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()

StarcoderdataJupyterScriptsDedupFiltered