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

# + id="tCf8JnDCuVBR" colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": ""}}, "base_uri": "https://localhost:8080/", "height": 73} outputId="22526756-3979-4428-b397-649aaa1b0cf9"
import pandas as pd
from google.colab import files

data_to_load = files.upload()

# + id="T1uOZPtoucBr"
# importing advertising.csv

import io
advertising_multi = pd.read_csv(io.BytesIO(data_to_load['advertising.csv']))

# + [markdown] id="1mR6A3-AuhPy"
# **Step 1: CLEANING DATA AND GETTING IMPORTANT INFO FROM OUR CSV FILE**

# + colab={"base_uri": "https://localhost:8080/", "height": 206} id="6uRiSfvVUb2U" outputId="f0779b66-b174-4f45-ee49-6d2a8f92f6ae"
# Looking at the first five rows
advertising_multi.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 206} id="NV5L1B3_Uljj" outputId="15e97427-518c-487d-e0a7-65e92762cdc8"
# Looking at the last five rows
advertising_multi.tail()

# + colab={"base_uri": "https://localhost:8080/"} id="mAQkgR1EU_-q" outputId="da35ba08-2c4e-4d54-d8e2-84aaf987a031"
# What type of values are stored in the columns?
advertising_multi.info()

# + colab={"base_uri": "https://localhost:8080/", "height": 300} id="Z19Pl9HmVJPy" outputId="8f7379f2-6673-4090-e466-1854a350130f"
# Let's look at some statistical information about our dataframe.
advertising_multi.describe()

# + [markdown] id="zisPS5fHVtFC"
# **Step 2: Visualising Data**

# + id="3PznXQkXVwtJ"
import matplotlib.pyplot as plt
import seaborn as sns

# %matplotlib inline

# + colab={"base_uri": "https://localhost:8080/", "height": 743} id="j_1im3qBWJHK" outputId="d2497024-31e6-4090-cac2-13ed8d66c09a"
# Let's plot a pair plot of all variables in our dataframe
sns.pairplot(advertising_multi)

# + colab={"base_uri": "https://localhost:8080/", "height": 567} id="rtQuSvOHbaNh" outputId="499be2c4-b8e2-499d-a828-081c14ab33df"
# Visualize the relationship between the features and the response using scatterplots
sns.pairplot(advertising_multi, x_vars=['TV', 'Radio', 'Newspaper'], y_vars='Sales', size=7, aspect=0.7, kind='scatter')

# + [markdown] id="4t3sUnlpdGc4"
# **Step 3: Splitting the Data for Training and Testing**

# + id="MZU2raI8dL7w"
# Putting feature variable to X
X = advertising_multi[['TV', 'Radio', 'Newspaper']]

# Putting response variable to y
y =  advertising_multi['Sales']

# + id="gy9TqTBEdtX5"
# random state is the seed used by the random number generator, it can be any integer.
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)

# + [markdown] id="8C_DorQJeUVg"
# **Step 4: Performing Linear Regression**

# + colab={"base_uri": "https://localhost:8080/"} id="-Xnbt8J_eYUA" outputId="c21ea06a-d186-4074-818e-3d3177d40503"
# import LinearRegression from sklearn
from sklearn.linear_model import LinearRegression

# Representing LinearRegression as lr
lr = LinearRegression()

# Fit the model using lr.fit()
lr.fit(X_train, y_train)

# + [markdown] id="Fnc7-GNtenYo"
# **Step 5: Model Evaluation**

# + colab={"base_uri": "https://localhost:8080/"} id="Mmr6fsuLerjQ" outputId="d68de467-d255-47c5-d7c3-c1dd8cea3e8a"
# Print the intercept and coefficients
print(lr.intercept_)

# + colab={"base_uri": "https://localhost:8080/", "height": 143} id="CoLNE5_RfA6I" outputId="cdb598bb-2914-4ed1-f919-9b8cc3e1c7c3"
# Let's see the coefficient
coeff_df = pd.DataFrame(lr.coef_, X_test.columns, columns=['Coefficient'])
coeff_df

# + [markdown] id="bUbMhKa3htqI"
# From the above result we may infern that if TV price increases by 1 unit it will affect sales by 0.045 units

# + [markdown] id="iOtcqPAFh8YP"
# **Step 6: Predictions**

# + id="lC6_LFyHh7D3"
# Making predictions using the model
y_pred = lr.predict(X_test)

# + [markdown] id="eoKsheOxiPiH"
# **Step 7: Calculating Error Terms**

# + id="Bw7MIj0_ieMf"
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, y_pred)
r_squared = r2_score(y_test, y_pred)

# + colab={"base_uri": "https://localhost:8080/"} id="FoR1h692izEg" outputId="e92aee7e-ec6a-4600-ca7a-77551196def1"
print('Mean Squared Error: ', mse)
print('R squared value: ', r_squared)

# + colab={"base_uri": "https://localhost:8080/", "height": 296} id="95L9sDq-jJav" outputId="03da4eaa-1994-4949-a736-8c3e2966c67d"
plt.scatter(y_test, y_pred)
plt.xlabel('Y test')
plt.ylabel('Y Prediction')

# + [markdown] id="bSKfo8PUi9VP"
# **Optional Step: Checking for P-value using STATSMODELS**

# + colab={"base_uri": "https://localhost:8080/"} id="LMsHUNGGjC73" outputId="808c73b8-7cee-49f9-badd-4220c679148a"
import statsmodels.api as sm
X_train_sm = X_train

# Unlike SKLearn, statsmodels don't automatically fit a constant.
# So you need to use the method sm.add_constant(X) in order to add a constant.
X_train_sm = sm.add_constant(X_train_sm)

# Create a fitter model in one line
lr_1 = sm.OLS(y_train, X_train_sm).fit()

# print the coefficients
lr_1.params


# + colab={"base_uri": "https://localhost:8080/"} id="jdfjjj4NkirA" outputId="a3951ee9-3d70-42e0-cb2c-180734f8a446"
print(lr_1.summary())

# + [markdown] id="s2A4F7TElLQX"
# From the above we can see that Newspaper is insignificant

# + colab={"base_uri": "https://localhost:8080/", "height": 341} id="vDKs9kKUlQ9_" outputId="0f96d4c6-2a27-4930-dcc4-b767d9bf5243"
plt.figure(figsize= (5,5))
sns.heatmap(advertising_multi.corr(), annot=True)

# + [markdown] id="KMGeWdrXl-oP"
# **Step 8: Implementing the results and running the model again**

# + [markdown] id="I3YhwJ1CmJ63"
# From the above we can see that Newspaper is insignificant

# + id="PjIPb1Y0mLaG"
# Removing Newspaper from our dataset
X_train_new = X_train[['TV', 'Radio']]
X_test_new = X_test[['TV', 'Radio']]

# + colab={"base_uri": "https://localhost:8080/"} id="ZMmsmFekmgCa" outputId="9337f0af-b720-448b-dd48-7bbdddf9913d"
# Model building
lr.fit(X_train_new, y_train)

# + id="zfTADnIimFlm"
# Making predictions
y_pred_new = lr.predict(X_test_new)

# + colab={"base_uri": "https://localhost:8080/", "height": 333} id="KVJp3BHam7xP" outputId="2cf7a9af-d6d0-4a07-a4c0-88ce842b28a6"
# Actual vs Predicted
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="-")
fig.suptitle('Actual and Predicted', fontsize=20)
plt.xlabel('Index', fontsize=18)
plt.ylabel('Sales', fontsize=16)

# + colab={"base_uri": "https://localhost:8080/", "height": 333} id="IoiF3VrXnNle" outputId="ab05bf10-6118-4847-94e2-723f3f8736e5"
# Error terms
c = [i for i in range(1,61,1)]
fig = plt.figure()
plt.plot(c, y_test-y_pred, color="blue", linewidth=2.5, linestyle="-")
fig.suptitle('Error Terms', fontsize=20)
plt.xlabel('Index', fontsize=18)
plt.ylabel('ytest-ypred', fontsize=16)

# + id="OH26mqcOncet"
mse2 = mean_squared_error(y_test, y_pred_new)
r_squared2 = r2_score(y_test, y_pred_new)

# + colab={"base_uri": "https://localhost:8080/"} id="6iY5FsgJnnLe" outputId="cdb54ef4-7a98-4df8-fbc8-f5bbb8632312"
print('Mean Squared Error: ', mse2)
print('R squared value: ', r_squared2)

# + colab={"base_uri": "https://localhost:8080/"} id="hjJF56kVoi6_" outputId="46a781aa-4d46-426a-e78f-c32eca7f52b0"
X_train_final = X_train_new

# Unlike SKLearn, statsmodels don't automatically fit a constant.
# So you need to use the method sm.add_constant(X) in order to add a constant.
X_train_final = sm.add_constant(X_train_final)

# Create a fitter model in one line
lr_final = sm.OLS(y_train, X_train_final).fit()

# print the summary
print(lr_final.summary())

# + [markdown] id="GXY5vJkzpob2"
# *Haya Ngoja tuone shida ilikuwa nini kwenye Newspaper... (Hii ni optional)*
#
# We're going to use **Simple Linear Regression: Newspaper(X) and Sales(y)**

# + colab={"base_uri": "https://localhost:8080/"} id="J4cvhmATp3F9" outputId="04d59654-5ad5-4549-99dd-8e89c706de3f"
import numpy as np

x_news = advertising_multi['Newspaper']
y_news = advertising_multi['Sales']

# Data Split
X_train, X_test, y_train, y_test = train_test_split(x_news, y_news, train_size=0.7, random_state=110)

# Required only in the case of simple linear regression
X_train = X_train[:, np.newaxis]
X_test = X_test[:, np.newaxis]

# Linear regression from sklearn
lm = LinearRegression()

# Fitting the model
lm.fit(X_train, y_train)

# Making predictions
y_pred_np = lm.predict(X_test)

# Computing mean square error and r square from sklearn library.
mse_np = mean_squared_error(y_test, y_pred_np)
r_sqaured_np = r2_score(y_test, y_pred_np)

# Printing
print('Mean Squared Error: ', mse_np)
print('R squared value: ', r_sqaured_np)

# + [markdown] id="pvxK17sduAIt"
# From above, ukicheck kwenye MSE, unaona kabisa namna kuna values kama 23 zinaaingua but also kwenye RSV, chance ya model kusaidia ni 0.8%...
#
# Ndo maana 'Newspaper' ilikuwa ni insignificant

StarcoderdataJupyterScriptsDedupFiltered