Skip to content

Instantly share code, notes, and snippets.

@alstat
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alstat/9833b04843449747ba1b to your computer and use it in GitHub Desktop.
Save alstat/9833b04843449747ba1b to your computer and use it in GitHub Desktop.
import plotly.plotly as py
from plotly.graph_objs import *
from pandas import DataFrame
from numpy import linspace, zeros
dat = {'height': [58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72],
'weight': [115, 117, 120, 123, 126, 129, 132, 135, 139, 142, 146, 150, 154, 159, 164]}
women = DataFrame(data = dat, columns = ['height', 'weight'])
beta1 = beta0 = linspace(-10, 10, 100)
def S1(beta0, beta1, data = women):
x = (data.ix[:,0] - data.ix[:,0].mean()) / data.ix[:,0].std()
y = (data.ix[:,1] - data.ix[:,1].mean()) / data.ix[:,1].std()
result = 0
for i in range(len(data)):
result = result + (y[i] - beta0 - beta1 * x[i]) ** 2
return result
z_val = zeros((beta0.shape[0], beta1.shape[0]))
for i in range(beta0.shape[0]):
for j in range(beta1.shape[0]):
z_val[i, j] = S1(beta0[i], beta1[j])
data = Data([
Surface(
x = beta0,
y = beta1,
z = z_val,
name = 'RSS Surface',
colorscale = 'YIGnBu',
)
])
axis = dict(
showbackground = True, # (!) show axis background
backgroundcolor = "rgb(204, 204, 204)", # set background color to grey
gridcolor = "rgb(255, 255, 255)", # set grid line color
zerolinecolor = "rgb(255, 255, 255)", # set zero grid line color
)
layout = Layout(
title = 'Error Surface',
xaxis = XAxis(
title = "beta_0"
),
yaxis = YAxis(
title = "beta_1"
),
zaxis = ZAxis(
title = "RSS"
),
autosize = True,
margin = Margin(
l = 65,
r = 50,
b = 65,
t = 90
),
scene = Scene(
xaxis = XAxis(axis),
yaxis = YAxis(axis),
zaxis = ZAxis(axis)
)
)
fig = Figure(data = data, layout = layout)
plot_url = py.plot(fig, filename = 'linear reg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment