#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
https://github.com/Tan-Moy/medium_articles/blob/master/art2_multivariate_linear_regression/mlr.py
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

my_data = pd.read_csv('home.txt', names=["size", "bedroom", "price"])

# we need to normalize the features using mean normalization
my_data = (my_data - my_data.mean())/my_data.std()


# setting the matrixes
X = my_data.iloc[:, 0:2]
ones = np.ones([X.shape[0], 1])
X = np.concatenate((ones, X), axis=1)
# print(X)
# .values converts it from pandas.core.frame.DataFrame to numpy.ndarray
y = my_data.iloc[:, 2:3].values
# print(y)
theta = np.zeros([1, 3])
# print(theta)
# print(X @ theta.T)

# computecost


def computeCost(X, y, theta):
    tobesummed = np.power(((X @ theta.T)-y), 2)
    return np.sum(tobesummed)/(2 * len(X))


def gradientDescent(X, y, theta, iters, alpha):
    cost = np.zeros(iters)
    for i in range(iters):
        theta = theta - (alpha/len(X)) * np.sum(X * (X @ theta.T - y), axis=0)
        cost[i] = computeCost(X, y, theta)

    return theta, cost


# set hyper parameters
alpha = 0.01
iters = 1000

g, cost = gradientDescent(X, y, theta, iters, alpha)
print(g)

finalCost = computeCost(X, y, g)
print(finalCost)

fig, ax = plt.subplots()
ax.plot(np.arange(iters), cost, 'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('Error vs. Training Epoch')
