def sse_for_line(slope_intercept,x_values,y_values):
slope=slope_intercept[0]
intercept=slope_intercept[1]
...
real_slope=2
real_intercept=10
x_values = np.array([-3,0,3,6])
y_values = real_slope*x_values + real_intercept
def sse_scan(slope_values,intercept_values,x_values,y_values):
sse_matrix = np.zeros((len(intercept_values),len(slope_values)),float)
...
def plot_trys(try_slopes,try_intercepts,x_values,y_values):
mesh_slopes, mesh_intercepts = np.meshgrid(try_slopes,try_intercepts) # for plotting
sse_values = sse_scan(try_slopes, try_intercepts,x_values,y_values)
print(mesh_slopes)
print("__________")
print(mesh_intercepts)
print("__________")
print(sse_values)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_wireframe(X=mesh_slopes,Y=mesh_intercepts,
Z=sse_values)
ax.view_init(elev=30,azim= 120)
plt.show()
plot_trys(try_slopes,try_intercepts,x_values,y_values)
# e.g.
def sse_for_line(slope_intercept,x_values,y_values):
... (substitue your function here)
real_slope=2
real_intercept=10
x_values = np.array([-3,0,3,6])
y_values = real_slope*x_values + real_intercept
number_of_x = len(x_values)
try_slopes=np.linspace(1,4,4)
try_intercepts=np.linspace(0,15,4)
from scipy.optimize import minimize
min_result=minimize(sse_for_line,[4,3],args=(x_values,y_values))
print("All of min result: ")
print("______________________\n")
print(min_result)
print("______________________\n")
print("Best min values: ",min_result.x)
### Remember this from last week?
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')
opt_result=minimize(sse_for_line,[4,3],args=(x_values,y_values)) # see above -- the true value is slope=2, intercept=0
print(opt_result.x)