# Run this cell to start.
import numpy as np
import pandas as pd
from scipy import optimize
# Safe settings for Pandas.
pd.set_option('mode.chained_assignment', 'raise')
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
y_values = [12,16,10,25,21,22]
x_values = [1,2,3,4,5,6]
plt.plot(x_values,y_values,'o')
# A little function to plot errors --
def plot_guess_error(x_values,y_values,my_guess):
my_guess_repeated = np.repeat(my_guess,len(x_values))
plt.plot(x_values,y_values,'o')
plt.plot(x_values, my_guess_repeated)
for index in np.arange(len(x_values)):
error_line_x=[x_values[index],x_values[index]]
error_line_y=[my_guess,y_values[index]]
plt.plot(error_line_x, error_line_y, \
linewidth=1, color="black")
my_guess=11
plot_guess_error(x_values,y_values,my_guess)
# a function for sse
def sse_show(my_guess):
total_error=0
for current_value in y_values:
error=my_guess-current_value
error_sqr=error*error
total_error=total_error + error_sqr
print("guess: ",my_guess," error: ",error," squared error: ",error_sqr)
print("total error so far: ",total_error)
return total_error
current_sse=sse_show(my_guess)
print("\ncurrent sse: ",current_sse)
# re-show the old guess for comparison
my_guess=11
plot_guess_error(x_values,y_values,my_guess)
# show the new guess and error
my_guess=15
plot_guess_error(x_values,y_values,my_guess)
current_sse=sse_show(my_guess)
print("\ncurrent sse: ",current_sse)
# a function for sse - without showing values
def sse(my_guess):
total_error=0
for current_value in y_values:
error=my_guess-current_value
error_sqr=error*error
total_error=total_error + error_sqr
return total_error
guesses = [11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
for current_guess in guesses:
total_error=sse(current_guess)
print("current guess for mean: ",current_guess," error: ",total_error)
guesses = [17.2, 17.4, 17.6, 17.8, 18.0, 18.2, 18.4, 18.6, 18.8]
for current_guess in guesses:
total_error=sse(current_guess)
print("current guess for mean: ",current_guess," error: ",total_error)
opt_mean=optimize.minimize(sse,[11])
print("mean by optimization: ",opt_mean.x)
np.mean(y_values)
def make_a_noisy_line(slope,intercept,x_values,noise):
y=np.zeros(len(x_values))
for i in np.arange(0,len(x_values)):
y[i] = 2*x_values[i] + intercept + np.random.normal(0,noise)
return y
x_values = [1,2,3,4,5,1,2,3,4,5,1.5,2.5,3.5,4.5,1.5,2.5,3.5,4.5]
y_values = make_a_noisy_line(2,0,x_values,2)
plt.plot(x_values,y_values,'o')
# A little function to plot errors --
def plot_guess_error(x_values,y_values,guess_values):
plt.plot(x_values,y_values,'o')
plt.plot(x_values, guess_values)
for index in np.arange(len(x_values)):
error_line_x=[x_values[index],x_values[index]]
error_line_y=[guess_values[index],y_values[index]]
plt.plot(error_line_x, error_line_y, \
linewidth=1, color="black")
# A poor guess, set y = 2 always
guess_values=np.repeat(2,len(x_values))
plot_guess_error(x_values,y_values,guess_values)
# A bit better guess, set y = x
guess_values = x_values
plot_guess_error(x_values,y_values,guess_values)
# A bit better still... set y = 1.5 * x
guess_values = x_values * np.repeat(1.5,len(x_values))
plot_guess_error(x_values,y_values,guess_values)