0

using this csv: https://docs.google.com/spreadsheets/d/1QbFIUE1AcFCOgDZH5K2vPColxXeBVDV-sJhS9On2BYY/edit?usp=sharing

I'm trying to figure out how to write a program to fit a quadratic function to predict the change in global temperature (y) as a function of Year (x). The function in question is:

function

The program should create a plot showing the training examples and the quadratic fit to the data.

This is what I have so far as a trial to figure out the basics. Just need to convert it:

import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt

#Generate 200 training examples

m = 200
x = np.random.randn(m)
y = np.random.randn(1) * x ** 2 + np.random.randn(1) * x + 
np.random.randn(1)
y = y + 0.4 * np.random.randn(m)

#Quadratic Fit

X = np.transpose([np.ones(m), x, x ** 2])
print(np.shape(X))
print(np.shape(y))

theta = inv(np.transpose(X) @X) @ np.transpose(X) @ y

plt.plot(x, y, 'bo')

xp = np.arange(-5, 5, 0.1)
yp = theta[0] + theta[1] * xp + theta[2] * xp ** 2

plt.plot(xp, yp, 'r-')

0 Answers0