# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
# %matplotlib inline

df = pd.read_csv("data/cars.csv", delimiter=";")
df[-5:]
# -

y = np.matrix(df.Car[1:]).T
x = np.matrix(df.Car[:-1]).T
# displaying X, Y pairs
np.hstack([x[-4:], y[-4:]])


# +
def linreg_via_np(X, Y, **kwargs):
    theta, e, r, s = np.linalg.lstsq(X, Y)
    return theta

def linreg_via_syseq(X, Y, **kwargs):
    theta = (X.T * X).I * X.T * Y
    return theta


# +
def prepareX(X, order=1, **kwargs):
    return np.hstack([ np.power(X, i) for i in range(order + 1) ])

def test(X, Y, trainer, **kwargs):
    X = prepareX(X, **kwargs)
    
    # plotting the original data
    plt.plot(X[:,1], Y, "rx")
    
    model = trainer(X, Y, **kwargs)
    
    # plotting the model
    xTest = np.linspace(
        int(X[:,1][0] - 1000), 
        int(X[:,1][-1] + 2000), 
        int(X[:,1][-1] - X[:,1][0]) * 2)
    
    xTest = prepareX(np.matrix(xTest).T, **kwargs)
    plt.plot(xTest[:,1], xTest * model)
    plt.title(kwargs.get("title", ""))
    plt.show()

test(X, Y, linreg_via_np, order=1, title="linreg_via_np order 1")
test(X, Y, linreg_via_syseq, order=1, title="linreg_via_syseq order 1")

test(X, Y, linreg_via_np, order=3, title="linreg_via_np order 3")
test(X, Y, linreg_via_syseq, order=3, title="linreg_via_syseq order 3")

# -



StarcoderdataJupyterScriptsDedupFiltered