<jupyter_start><jupyter_text>Import<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('50_Startups.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 4].values
X<jupyter_output><empty_output><jupyter_text>Preparing Data<jupyter_code>from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lbencoder = LabelEncoder()
ohencoder = OneHotEncoder(categorical_features=[3])

X[:, 3] = lbencoder.fit_transform(X[:, 3])
X = ohencoder.fit_transform(X).toarray()
X<jupyter_output><empty_output><jupyter_text>Avoiding Dummy Data Trap<jupyter_code>X = X[:, 1:]<jupyter_output><empty_output><jupyter_text>Splitting into test and training<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
y_pred
y_test<jupyter_output><empty_output><jupyter_text>Backward Elimination   Handling the constant term<jupyter_code>X = np.append(np.ones((50,1)).astype(int), values=X, axis=1)
X<jupyter_output><empty_output><jupyter_text>Backward Elimination<jupyter_code>import statsmodels.formula.api as sm
significance_level = 0.05
lst = [0,1,2,3,4,5]
while len(lst)>0:
    X_opt = X[:, lst]
    regressor_OLS = sm.OLS(y,X_opt).fit()
    print(regressor_OLS.summary())
    i = regressor_OLS.pvalues.argmax()
    if regressor_OLS.pvalues[i] <= significance_level:
        break
    else:
        print('\nRemoved column -->',lst[i])
        del lst[i]
regressor_OLS.summary()
X_opt = X_opt[:, 1:]
X_train_OLS, X_test_OLS, y_train, y_test = train_test_split(X_opt, y, test_size = 0.2, random_state = 0)
regressor.fit(X_train_OLS, y_train)
y_pred_OLS = regressor.predict(X_test_OLS)
for a,b,c in zip(y_pred_OLS,y_test,y_pred):
    print(a,'  ',b,'  ',c)
from sklearn.metrics import r2_score
print(r2_score(y_test,y_pred_OLS))
print(r2_score(y_test,y_pred))<jupyter_output>0.9464587607787219
0.9347068473282446
{'fraction_non_alphanumeric': 0.12695492180312787, 'fraction_numerical': 0.03035878564857406, 'mean_word_length': 7.269961977186312, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, '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': '348470', 'license': 'MIT', 'n_tokens_mistral': 922, 'n_tokens_neox': 880, 'n_words': 140, 'path': 'Implementations/Part 2 - Regression/Multiple_Linear_Regression/Multiple Linear Regression.ipynb', 'repo_name': 'InternityFoundation/ML-Basics-Vibhu', 'size': '2174'}