<jupyter_start><jupyter_text>Reading the data<jupyter_code>advertising = pd.read_csv('D:/upgrad/ML/advertising.csv')
advertising.head()
advertising.describe()<jupyter_output><empty_output><jupyter_text>Visualising the data<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use('ggplot')
sns.pairplot(advertising)<jupyter_output><empty_output><jupyter_text>Split the dataset between training and test<jupyter_code>X = advertising[['TV','Radio','Newspaper']]
y=advertising['Sales']
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.7, random_state = 100)<jupyter_output>D:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py:2026: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified.
  FutureWarning)<jupyter_text>Perform Linear Regression<jupyter_code>from sklearn.linear_model import LinearRegression

lr = LinearRegression()

lr.fit(X_train, y_train)
lr.coef_<jupyter_output><empty_output><jupyter_text>Model Evaluation<jupyter_code>lr.intercept_<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code>y_pred = lr.predict(X_test)<jupyter_output><empty_output><jupyter_text>Calculate the error terms<jupyter_code>from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, y_pred)
r2s = r2_score(y_test, y_pred)
print(mse)
print(r2s)<jupyter_output>1.8506819941636972
0.9058622107532245<jupyter_text>Checking P value using statsmodels<jupyter_code>import statsmodels.api as sm

X_train_sm = X_train

X_train_sm = sm.add_constant(X_train)

smlr = sm.OLS(y_train,X_train_sm).fit()
smlr.params
smlr.summary()<jupyter_output><empty_output><jupyter_text>Correlation<jupyter_code>sns.heatmap(X.corr(), annot=True)<jupyter_output><empty_output><jupyter_text>Remove the Newspaper and run the model once again<jupyter_code>X = advertising[['TV','Radio','Newspaper']]

y= advertising['Sales']
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.7, random_state = 100)
X_train_new = X_train[['TV','Radio']]
X_test_new = X_test[['TV','Radio']]
lsr = LinearRegression()

lsr.fit(X_train_new, y_train)
y_pred_new = lsr.predict(X_test_new)
c = [ i for i in range(1,61,1)]

fig = plt.figure()
plt.plot(c, y_test, color='blue', linewidth=2.5, linestyle='-')
plt.plot(c, y_pred, color='red', linewidth=2.5, linestyle='-')
plt.plot(c, y_pred_new, color='yellow', linewidth=2.5, linestyle='-')
from sklearn.metrics import mean_squared_error, r2_score

mse = mean_squared_error(y_test, y_pred_new)
r2s = r2_score(y_test, y_pred_new)
print(mse)
print(r2s)
X_train_final = X_train_new
X_train_final = sm.add_constant(X_train_final)
slr_final = sm.OLS(y_train, X_train_final).fit()
slr_final.summary()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12596491228070175, 'fraction_numerical': 0.02456140350877193, 'mean_word_length': 9.52029520295203, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '57198', 'license': 'MIT', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1128, 'n_words': 172, 'path': 'MultivariateLinearRegression.ipynb', 'repo_name': 'aryancodify/LinearRegression', 'size': '2850'}