<jupyter_start><jupyter_text>Multiple Linear Regression<jupyter_code>import pandas as pd
df = pd.read_csv("50_Startups.csv")
df.head()
df.info()
df.State.unique()
X = df.iloc[:, :-1].values
y = df.iloc[:, -1].values
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
import numpy as np
ct = ColumnTransformer([("encoder", OneHotEncoder(), [3])], remainder = "passthrough")
X = np.array(ct.fit_transform(X))<jupyter_output><empty_output><jupyter_text>Avoiding the dummy variable trap<jupyter_code>X = X[:, 1:]
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)
X_train[:5]
from sklearn.linear_model import LinearRegression
regressor= LinearRegression()
regressor.fit(X_train, y_train)
y_pred_train = regressor.predict(X_train)
y_pred_test = regressor.predict(X_test)
y_test - y_pred_test #Difference in prediction and real observation
regressor.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Using Backward Elimination<jupyter_code>import statsmodels.formula.api as stats
"""Adding a column for x0 for b0 (constant). So the column will conatain only one's"""
X = np.append(arr = np.ones((50, 1)).astype(float), values = X , axis = 1)
X_opt = X[:, [0, 1, 2, 3, 4, 5]].astype(float)
regressor_OLS = stats.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 1, 3, 4, 5]].astype(float)
regressor_OLS = stats.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 3, 4, 5]].astype(float)
regressor_OLS = stats.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 3, 5]].astype(float)
regressor_OLS = stats.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 3]].astype(float)
regressor_OLS = stats.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15087538619979401, 'fraction_numerical': 0.018537590113285273, 'mean_word_length': 6.560311284046692, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '336517', 'license': 'MIT', 'n_tokens_mistral': 800, 'n_tokens_neox': 760, 'n_words': 169, 'path': 'Regression/MultipleLinearRegression/MultipleLinearRegression.ipynb', 'repo_name': 'danmusetoiu/biblioteca', 'size': '1942'}