<jupyter_start><jupyter_text>Problem Statement: The Advertising dataset captures sales revenue generated with respect to advertisement spendsacross multiple channels like radio, tv, and newspaper.  Objective:    Build a linear regression model to:• Interpret the coefficients of the model• Make predictions• Find and analyze model residuals• Evaluate model efficiency using RMSE and R-Square values<jupyter_code>#import libraries
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score, mean_squared_error
from math import sqrt

%matplotlib inline
#load the data
df = pd.read_csv('C:/data/Advertising.csv', index_col=0)
df.head()
df.columns = ['TV','Radio','Newspaper','Sales']
df.shape
df.describe()
#check the missing value
df.isna().any()
#ploting data using scatter plot
fig,axs = plt.subplots(1,3,sharey=True)
df.plot(kind='scatter',x='TV',y='Sales',ax=axs[0], figsize=(16,8))
df.plot(kind='scatter',x='Radio',y='Sales',ax=axs[1])
df.plot(kind='scatter',x='Newspaper',y='Sales',ax=axs[2])
#devide data into feature and target
feature_cols =['TV']
x = df[feature_cols]
y = df.Sales
from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(x,y)
print(lm.intercept_)
print(lm.coef_)
7.032594+0.047537*50
#predict using new tv data
x_new = pd.DataFrame({'TV':[50]})
x_new.head()
lm.predict(x_new)
#use the smallest and largest value and predict the value
x_new = pd.DataFrame({'TV':[df.TV.min(),df.TV.max()]})
x_new.head()
preds = lm.predict(x_new)
preds
df.plot(kind='scatter', x='TV', y = 'Sales')
plt.plot(x_new,preds,c='red',linewidth=2)
import statsmodels.formula.api as smf
lm = smf.ols(formula='Sales ~ TV', data=df).fit()
lm.conf_int()
lm.pvalues
lm.rsquared
feature_cols = ['TV','Radio','Newspaper']
x=df[feature_cols]
y=df.Sales

from sklearn import model_selection
xtrain,xtest,ytrain,ytest = model_selection.train_test_split(x,y,test_size=0.3, random_state=42)
lm = LinearRegression()
lm.fit(x,y)
print(lm.intercept_)
print(lm.coef_)
lm = LinearRegression()
lm.fit(xtrain,ytrain)
# Evaluate model efficiency using RMSE and R-Square values
print(lm.intercept_)
print(lm.coef_)

prediction = lm.predict(xtest)
print(sqrt(mean_squared_error(ytest,prediction)))
#check the summery output
lm = smf.ols(formula='Sales~TV + Radio + Newspaper', data=df).fit()
lm.conf_int()
lm.summary()
lm = smf.ols(formula='Sales~TV+Radio',data=df).fit()
lm.rsquared
lm = smf.ols(formula='Sales~TV+Radio+Newspaper',data=df).fit()
lm.rsquared
import numpy as np
np.random.seed(12345)

nums = np.random.rand(len(df))
mask_large = nums>0.5

df['Size'] ='small'
df.loc[mask_large,'Size']='large'
df.head()
df['IsLarge'] = df.Size.map({'small':0,'large':1})
df.head()
feature_cols =['TV','Radio','Newspaper','IsLarge']
x = df[feature_cols]
y = df.Sales

lm=LinearRegression()
lm.fit(x,y)

zip(feature_cols,lm.coef_)
np.random.seed(123456)

nums = np.random.rand(len(df))
mask_suburban = (nums>0.33) & (nums<0.66)
mask_urban = nums>0.66
df['Area']='rural'
df.loc[mask_suburban,'Area']='suburban'
df.loc[mask_urban,'Area']='urban'
df.head()
area_dummies = pd.get_dummies(df.Area,prefix='Area').iloc[:,1:]

df=pd.concat([df,area_dummies],axis=1)
df.head()
feature_cols =['TV','Radio','Newspaper','IsLarge','Area_suburban','Area_urban']
x = df[feature_cols]
y = df.Sales

lm=LinearRegression()
lm.fit(x,y)

print(feature_cols,lm.coef_)<jupyter_output>['TV', 'Radio', 'Newspaper', 'IsLarge', 'Area_suburban', 'Area_urban'] [ 0.04574401  0.1878667  -0.0010877   0.07739661 -0.10656299  0.26813802]
{'fraction_non_alphanumeric': 0.17039343334276819, 'fraction_numerical': 0.03141805830738749, 'mean_word_length': 7.2377622377622375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '261333', 'license': 'MIT', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1361, 'n_words': 269, 'path': 'Machine Learning/06_Regression_Analysis_Advertising_data.ipynb', 'repo_name': 'MukundAabha/machine_learning_projects', 'size': '3533'}