<jupyter_start><jupyter_text>Multiple Linear Regression- MLR: y = b0 + b1*x1 + ...... + bn*xn- Assumptions of a Linear Regression;    - Linearity    - Homoscedasticity    - Multivariate Normality    - Independence of Errors    - Lack of Mulitcollinearity **Building a Model**- There are 5 methods of building a model    **1. All-in**        - One is if you have prior knowledge if you know that these exact variavle are the ones are your true           predictors you dont have to build anything you already know that this is the case.        - Other one is you have to.        - Preparing for backward elimination    **2. Backward Elimination**        - Step 1: Select a significaance level to stay in the model(eg Significance Level = 0.05)        - Step 2: Fit the full model with akk possible predictors.        - Step 3: Consider the predictor with the hghest p-value, if p > sl, go to step 4, otherwise go to FIN (Keep                   the previous mode.).        - Step 4: Remove the predictor.        - Step 5: Fit model without this variables.    **3. Forward Selection**        - Step 1: Select a significaance level to enter in the model(eg Significance Level = 0.05)        - Step 2: Fit all the simple regression model Y ~ Xn, then select the one with the lowest p-value.        - Step 3: Keep this variable and fit all possible models with one extra predictor added to the one you already                   have.        - Step 4: Consider the predictor with the lowest p-value. If p < sl, go to step 3, otherwise go to FIN.    **4. Bidirectional Elimination**        - Step 1: Select a significance level to enter and to stay in the model. (eg SLENTER = 0.05, SLSTAY = 0.05)        - Step 2: Perfomr the next step of Forward Selection. (new variable must have p < SLENTER to enter)        - Step 3: Perform ALL step of Backward Elemination. (old variable must have p < SLSTAY to stay)        - Step 4: No new variable can enter and no old variabel can exit.        - Now your model is ready.    **5. Score Comparison/All Possible Models**        - Step 1: Select a criterion of goodness of fit. (eg Akaike criterion)        - Step 2: Construct all possible regression model; 2^n - 1 total combination.        - Step 3: Select the one with the best criterion**Importing Dataset**<jupyter_code># Numpy allows us to work with array.
import numpy as np

# Maptplotlib which allows us to plot some chart.
import matplotlib.pyplot as plt

# Pandas allows us to not only import the datasets but also create the matrix of features(independent) and 
# dependent variable.
import pandas as pd<jupyter_output><empty_output><jupyter_text>**Importing the Dataset**<jupyter_code>dataset = pd.read_csv("50_Startups.csv")<jupyter_output><empty_output><jupyter_text>- The independent variable usally in the first columns of dataset and dependent variable usally in the last columns of the data sets.- X is Independent Variable.- Y is Dependent Variable.<jupyter_code>x = dataset.iloc[:, :-1].values      # iloc is index of location
y = dataset.iloc[:, -1].values

print(x)
print(y)<jupyter_output>[192261.83 191792.06 191050.39 182901.99 166187.94 156991.12 156122.51
 155752.6  152211.77 149759.96 146121.95 144259.4  141585.52 134307.35
 132602.65 129917.04 126992.93 125370.37 124266.9  122776.86 118474.03
 111313.02 110352.25 108733.99 108552.04 107404.34 105733.54 105008.31
 103282.38 101004.64  99937.59  97483.56  97427.84  96778.92  96712.8
  96479.51  90708.19  89949.14  81229.06  81005.76  78239.91  77798.83
  71498.49  69758.98  65200.33  64926.08  49490.75  42559.73  35673.41
  14681.4 ]<jupyter_text>**Encoding Categorical Data**<jupyter_code># One Hot Encoding it helps to change the Category to Binary Vector Number.
# California = 1 0 0
# New York = 0 0 1
# Florida = 0 1 0

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder

# Creating an object of the column transform
ct = ColumnTransformer(transformers =[('encoder', OneHotEncoder(), [3])], remainder = 'passthrough')

# Fit and Transform
x = np.array(ct.fit_transform(x))
print(x)<jupyter_output>[[0.0 0.0 1.0 165349.2 136897.8 471784.1]
 [1.0 0.0 0.0 162597.7 151377.59 443898.53]
 [0.0 1.0 0.0 153441.51 101145.55 407934.54]
 [0.0 0.0 1.0 144372.41 118671.85 383199.62]
 [0.0 1.0 0.0 142107.34 91391.77 366168.42]
 [0.0 0.0 1.0 131876.9 99814.71 362861.36]
 [1.0 0.0 0.0 134615.46 147198.87 127716.82]
 [0.0 1.0 0.0 130298.13 145530.06 323876.68]
 [0.0 0.0 1.0 120542.52 148718.95 311613.29]
 [1.0 0.0 0.0 123334.88 108679.17 304981.62]
 [0.0 1.0 0.0 101913.08 110594.11 229160.95]
 [1.0 0.0 0.0 100671.96 91790.61 249744.55]
 [0.0 1.0 0.0 93863.75 127320.38 249839.44]
 [1.0 0.0 0.0 91992.39 135495.07 252664.93]
 [0.0 1.0 0.0 119943.24 156547.42 256512.92]
 [0.0 0.0 1.0 114523.61 122616.84 261776.23]
 [1.0 0.0 0.0 78013.11 121597.55 264346.06]
 [0.0 0.0 1.0 94657.16 145077.58 282574.31]
 [0.0 1.0 0.0 91749.16 114175.79 294919.57]
 [0.0 0.0 1.0 86419.7 153514.11 0.0]
 [1.0 0.0 0.0 76253.86 113867.3 298664.47]
 [0.0 0.0 1.0 78389.47 153773.43 299737.29]
 [0.0 1.0 0.0 73994.56 122782.75 3[...]<jupyter_text>**Splitting the dataset into Training Set and Test Set**<jupyter_code># Importing Package
from sklearn.model_selection import train_test_split

# Dividing training and test set.
# The best ratio is 80 - 20 for trainging and testing respectively.
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)

print(x_train)
print(y_train)<jupyter_output>[ 96778.92  96479.51 105733.54  96712.8  124266.9  155752.6  132602.65
  64926.08  35673.41 101004.64 129917.04  99937.59  97427.84 126992.93
  71498.49 118474.03  69758.98 152211.77 134307.35 107404.34 156991.12
 125370.37  78239.91  14681.4  191792.06 141585.52  89949.14 108552.04
 156122.51 108733.99  90708.19 111313.02 122776.86 149759.96  81005.76
  49490.75 182901.99 192261.83  42559.73  65200.33]<jupyter_text>**Training the MLR model on the Training Set**<jupyter_code># Importing Package
from sklearn.linear_model import LinearRegression

reg = LinearRegression()

# Fitting the model
reg.fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>**Preparing the Test Set results**<jupyter_code># Predicting
y_pred =  reg.predict(x_test)

# Getting only 2 decimal
np.set_printoptions(precision = 2)

# Concatenating and Reshaping
print(np.concatenate((y_pred.reshape(len(y_pred), 1), y_test.reshape(len(y_test), 1)), axis = 1))<jupyter_output>[[103015.2  103282.38]
 [132582.28 144259.4 ]
 [132447.74 146121.95]
 [ 71976.1   77798.83]
 [178537.48 191050.39]
 [116161.24 105008.31]
 [ 67851.69  81229.06]
 [ 98791.73  97483.56]
 [113969.44 110352.25]
 [167921.07 166187.94]]
{'fraction_non_alphanumeric': 0.10387481371087928, 'fraction_numerical': 0.23099850968703428, 'mean_word_length': 3.299167200512492, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '300540', 'license': 'MIT', 'n_tokens_mistral': 3495, 'n_tokens_neox': 2481, 'n_words': 808, 'path': 'Multiple Linear Regression.ipynb', 'repo_name': 'dshah98/Machine_Learning_with_Python', 'size': '6710'}