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

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# %matplotlib inline

m,n =200,3

adv = (100+100*np.random.rand(m,n)).round(2)
adv_df = pd.DataFrame(adv,columns=['TV','Radio','Newspaper'])
adv_df.head()

sales = (1 + 2*adv[:,0:1] + 3*adv[:,1:2] + 4*adv[:,2:3] + np.random.randn(m,1)).round(2)
# np.concatenate((adv,sales))
sales[:5]

plt.plot(adv[:, 2:3], sales, 'b.')
plt.xlabel('Newspaper')
plt.ylabel('Sales')
plt.show()

adv

X = np.c_[np.ones((m,1)),adv]
X[:5]

sales.shape

y = sales

theta_hat = (np.linalg.inv(X.T.dot(X))).dot(X.T.dot(y))
theta_hat

# ## Predicted value

example = X[100].dot(theta_hat)
example

# ## Actual value

sales[100]

from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(adv,y)

lin_reg.coef_

lin_reg.intercept_

lin_reg.predict([adv[100]])

StarcoderdataJupyterScriptsDedupFiltered