# ---
# 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
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/queenish001/MLnow_2.0/blob/main/3_1_Univariate.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="q_569ugy6lyY"
# > This notebook was created for code illustration of the `ML.now()` course
#
#
# `Univariate Linear Regression`
#
# **Date Created**: June 6, 2021
#
#
# **Author**:
# [Nishi](https://github.com/queenish001/)
#
# [[Course Repository](https://github.com/shivanishimpi/MLnow_2.0)]
#

# + [markdown] id="KmW9S6zjaauQ"
# ## Setup

# + colab={"base_uri": "https://localhost:8080/"} id="oX_QibhWsnnX" outputId="69493bd4-6429-4d57-f2a5-ed9cc8330fe9"
from google.colab import drive
drive.mount('/content/drive')

# + colab={"base_uri": "https://localhost:8080/"} id="wYuNn17Isvml" outputId="d36cb0d0-8ade-42ab-ae5c-1fc870b47a52"
# cd '/content/drive/MyDrive/GS_ML_WORKSHOP/02_EDA/Student'

# + colab={"base_uri": "https://localhost:8080/"} id="oR69jwALs-uP" outputId="4ba195b0-7b79-4426-a235-5ab8b583cd22"
# ls

# + id="0rYwvbeMtPJg"
import os

# + id="cnGQRbmes_tZ"
import pandas as pd #working with csv or excel files
import numpy as np #working with numbers/ arrays/ tensors
import tensorflow as tf #framework
from tensorflow import keras #API / library 
import os #using os commands between the python language

# + id="VV-Ps6dJtX6B"
mathData = pd.read_csv('student-mat.csv', sep=';') #load the csv file as dataframe

# + colab={"base_uri": "https://localhost:8080/", "height": 379} id="2_KId_bptr05" outputId="4d709640-35a0-4a5a-a2ca-330fa7fcb842"
mathData.head(10) #print the first ten rows of the dataframe

# + [markdown] id="i2bal50yG_PZ"
# We are just going to consider the columns `G1` and `G3` for univariate linear regression
#
# $G3_i = G1_i  \cdot \theta_1 + \theta_0$

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="Yw0PNCuTSEDC" outputId="30b9d374-35f0-4d3a-b199-280c3e9a3d88"
uniMathData = mathData[['G1', 'G3']]
uniMathData.head(5)

# + id="eyzP9mL4Hmb5"
uniMathData.to_csv('univariate_MathData_2.csv')

# + colab={"base_uri": "https://localhost:8080/"} id="I-3qK_nNxnTA" outputId="f449ca74-6d40-41dd-bc42-dfcc4e5d2350"
# ls

# + [markdown] id="ImV3p6AKxszU"
# ## Data visualization

# + colab={"base_uri": "https://localhost:8080/", "height": 157} id="RqumcsZLxq4v" outputId="7e172d4b-5db9-4e06-c7c1-14357188e15e"
import seaborn as sns
sns.palplot(sns.color_palette('PuOr')) #Purple to Orange colors
pal = sns.color_palette('PuOr', 6) #print 6 color shades from Purple to Orange
pal.as_hex() #set hex code values for colors

import matplotlib.pyplot as plt
plt.style.use(['seaborn'])
sns_colors = ['#c6690c', '#664697'] #orange Purple hex codes
sns.set_palette(sns_colors) #set the palette as sns_colors 
sns.palplot(sns.color_palette(sns_colors)) #plot the color codes

# + colab={"base_uri": "https://localhost:8080/"} id="A6MT5E8Fy7MZ" outputId="50c8ed17-a366-4f9a-a0bf-530f3b5e3ab5"
uniMathData.columns #columns in the dataframe

# + colab={"base_uri": "https://localhost:8080/", "height": 391} id="PrsTwdHHzBbk" outputId="ea99d5af-7339-40d9-9d6a-695359775b99"
#pairplot for all the values

sns.pairplot(uniMathData,
             x_vars = ['G1', 'G3'],
             y_vars =  ['G1', 'G3'],
       diag_kind='kde'
             )

# + [markdown] id="Klm33YcpKZkl"
# ## Data Splits

# + colab={"base_uri": "https://localhost:8080/"} id="UwrSOCKS1NhN" outputId="2cc691a0-33f6-41a7-e004-d2f60ee0eb0b"
#80-20 train-test percent split

trainDataset = uniMathData.sample(frac=0.8, random_state=0) 
testDataset = uniMathData.drop(trainDataset.index)

print(trainDataset.head())
print(testDataset.head())

# + colab={"base_uri": "https://localhost:8080/"} id="YWO2xRV53wV-" outputId="2faeaa06-5618-48cc-dc15-bd7e78d87212"
print(trainDataset.shape)
print(testDataset.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="OmDTbvMC32hg" outputId="169f273e-319d-480c-cc37-51a4d21acdb8"
# #copy the trainDataset dataframe for getting the features

trainFeatures = trainDataset.copy()
testFeatures = testDataset.copy()

print(trainFeatures.head())
print(testFeatures.head())

# + colab={"base_uri": "https://localhost:8080/"} id="jSR0_uni4ayh" outputId="862cc218-97dd-49b4-9a05-123317c7c5c8"
#removing the G3 column and saving it into the labels variable

trainLabels = trainFeatures.pop('G3')
testLabels = testFeatures.pop('G3')

print(trainLabels.head())
print(testLabels.head())

# + colab={"base_uri": "https://localhost:8080/"} id="HnGSqb-q4wk8" outputId="1f8691f8-62c2-4c99-8f7f-17a6c6b02b8b"
print(trainFeatures.head())
print(testFeatures.head())

# + id="XPTSq34jk4o_"
# univariate -> num(features) = 1
# multvariate -> num(features) > 1 = 11


model = tf.keras.Sequential([
                             tf.keras.layers.Dense(1)

])

# + id="4C4GKBdE6iCE"
model.compile(
    loss = 'mean_absolute_error', #minimizing the MAE loss
    optimizer = tf.keras.optimizers.Adam(0.001), #learning rate specified as 0.001
    # optimizer = 'adam', #takes the default learning rate
    metrics = ['mae', 'mse'] #meanSquare and meanAbsolute error metrics
)

# + colab={"base_uri": "https://localhost:8080/"} id="d_A7DrhB6zvm" outputId="11ee327b-d732-4d9f-f15c-66dfad9adf47"
numEpochs = 300
history = model.fit(x = trainFeatures, y = trainLabels, validation_data = (testFeatures, testLabels), epochs = numEpochs)

# + id="6zYkLGS6wQyy" colab={"base_uri": "https://localhost:8080/"} outputId="c00bec61-d053-4100-ba3c-e3e5d613f3da"
print(history)

# + colab={"base_uri": "https://localhost:8080/"} id="1lsbk_ohAu1j" outputId="522e4bd9-fdab-438f-ed4c-c882ba14891b"
model.summary()


# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="P6VnkPim7tH7" outputId="70fc0776-a0a7-45f5-a26d-a927324077fc"
# tempString = 'mse'
def curvePlots(tempString):
  plt.plot(history.history[tempString])
  plt.plot(history.history[f'val_{tempString}'])
  plt.xlabel('NumEpochs')
  plt.ylabel(tempString)
  plt.legend([tempString, f'val_{tempString}'])
  plt.show()


curvePlots('mse')
curvePlots('mae')
curvePlots('loss')

# + id="esW0eHQbzoj4" colab={"base_uri": "https://localhost:8080/"} outputId="4d8989dd-945d-4235-f4c4-f2b6e8906263"
model.predict([1])

# + colab={"base_uri": "https://localhost:8080/"} id="KJrLWamX8Tg-" outputId="15a0d32e-9571-4ff6-9f59-707e4c7c8f01"
# testPreds = model.predict(testFeatures).flatten() #array of all prediction values

#for single values

print(f'Prediction for input value 1: {model.predict([1])}')


# for a list of values
tempListforPreds = [1,2,3,4,5]
print(f'''

input List = {tempListforPreds}


List of Predictions:
{model.predict(tempListforPreds)}

List of Predictions (flattened out):
{model.predict(tempListforPreds).flatten()}
''')

# + id="vJNFh6hc0K6r" colab={"base_uri": "https://localhost:8080/"} outputId="182b0011-f838-43f5-cde1-73897ebd5724"
print(testFeatures)

# + id="MniGZOu50IyN"
testPreds = model.predict(testFeatures).flatten() #array of all prediction values

# + id="du0Orwu40Qan" colab={"base_uri": "https://localhost:8080/"} outputId="e6a9bb52-951d-4def-ecab-b075bce442e2"
print(len(testPreds))
print(testPreds)


# + colab={"base_uri": "https://localhost:8080/", "height": 361} id="EfP8bB_v89Wa" outputId="fe6a06a0-48c9-4375-e9d2-d6f516151b30"
# prediciton plot --> how well is your model predicting across the actual labels

def predPlot(labels, predictions):
  plt.scatter(labels, predictions)
  plt.ylabel('Predictions')
  plt.xlabel('True Value or Labels')
  plt.axis('equal')
  plt.axis('square')
  plt.xlim([0, plt.xlim()[1]])
  plt.ylim([0, plt.ylim()[1]])
  plt.show()

predPlot(testLabels, testPreds)


# + colab={"base_uri": "https://localhost:8080/", "height": 361} id="70608Z0o9tWL" outputId="59629145-be63-4b36-eaff-c4f7c056b719"
#error plot --> gaussian distribution 

def errorPlot(preds, labels, counts):
  errors = preds - labels
  plt.hist(errors, counts)
  plt.xlabel('Error')
  plt.ylabel('Counts')
  plt.show()

errorPlot(testPreds, testLabels, numEpochs)

# + [markdown] id="oshf2mUG1vPk"
# Note: 
#
#
# Validation loss `val_loss` is a metric that tells you how much deviation from the actual label can you expect in the predicted label
#
# To optimize your predicitons -->
#
# - Hyperparameter tuning --> `numEpochs`, `optimizer`, `learning_rate`, lossFunctions

# + id="1UZhoCFOCoGk"


StarcoderdataJupyterScriptsDedupFiltered