<jupyter_start><jupyter_text>Model Training<jupyter_code>df.head()
fig,ax = plt.subplots(nrows=1,ncols= 3,figsize=(12,4),dpi=200)

ax[0].plot(df['TV'],df['sales'],'o')
ax[0].set_xlabel('TV')
ax[0].set_ylabel('Sales')

ax[1].plot(df['radio'],df['sales'],'o')
ax[1].set_xlabel('radio')
ax[1].set_ylabel('Sales')

ax[2].plot(df['newspaper'],df['sales'],'o')
ax[2].set_xlabel('newspaper')
ax[2].set_ylabel('Sales')
fig.tight_layout()<jupyter_output><empty_output><jupyter_text>**Beta value will tell us about which feature is more important and how**task 1 first seperate feature and labeltask 2 train test split<jupyter_code>X = df.drop(['sales','Total_spend'],axis=1)
X
y = df['sales']
y<jupyter_output><empty_output><jupyter_text>**Train test split**<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.3, random_state=101)
len(X_train)
len(X_test)<jupyter_output><empty_output><jupyter_text>**Model call and Train**<jupyter_code>from sklearn.linear_model import LinearRegression
# hyperparameters are the constants within the model we edit them to adjust performance
# things you can edit
#help(LinearRegression)
model = LinearRegression()
model.fit(X_train,y_train)
predict = model.predict(X_test)<jupyter_output><empty_output><jupyter_text>**Performace Evaluation**<jupyter_code>from sklearn.metrics import mean_absolute_error,mean_squared_error
df['sales'].mean()
y_test.mean()
y_train.mean()
mean_absolute_error(y_test,predict) #MAE
mean_squared_error(y_test,predict) #MSE
np.sqrt(mean_squared_error(y_test,predict)) #RMSE
sns.histplot(data = df,x='sales')<jupyter_output><empty_output><jupyter_text>**Evaluating Residuals**<jupyter_code>#predict_for_x_train = model.predict(X_train)
sns.scatterplot(data=df,x=y_train,y=y_train-predict_for_x_train)
plt.axhline(y=0,ls='--',color = 'r')
sns.scatterplot(data=df,x=y_test,y=y_test-predict)
plt.axhline(y=0,ls='--',color = 'r')
sns.kdeplot(data=df,x=y_test-predict)
len(y_test)
len(predict)
sns.scatterplot(x=predict,y=y_test-predict)
plt.axhline(y=0,ls='--',color = 'r')
sns.scatterplot(data=df,x=y_test,y=y_test-predict,color='b')
plt.axhline(y=0,ls='--',color = 'r')<jupyter_output><empty_output><jupyter_text>Deployment<jupyter_code>final_model = LinearRegression()
final_model.fit(X,y)
len(X),len(y)
final_model.coef_
predicted_final = final_model.predict(X)
residuals = y- predicted_final 
plt.plot(predicted_final,residuals,'o')
plt.axhline(ls='--')
from joblib import dump,load
dump(final_model,'final_sales_model.joblib')
sales_model = load('final_sales_model.joblib')
sales_model.coef_
campaign = [[400,140,50]]
sales_model.predict(campaign)<jupyter_output><empty_output>