Press the button 'Toggle code' below to toggle code on and off for entire this presentation.
from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)
# This line will hide code by default when the notebook is eåxported as HTML
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)
# This line will add a button to toggle visibility of code blocks, for use with the HTML export version
di.display_html('''<button onclick="jQuery('.input_area').toggle(); jQuery('.prompt').toggle();">Toggle code</button>''', raw=True)
# create instance of linear regression demo, used below and in the next examples
demo1 = nonlib.nonlinear_regression_visualizer.Visualizer(csvname = datapath + 'noisy_sin_sample.csv')
demo1.plot_data(xlabel = 'x',ylabel = 'y')
could fit the data well if parameters are tuned
# nonlinearity
def f(x_val,w):
# create feature
f_val = np.sin(w[2] + w[3]*x_val)
return f_val
# prediction
def predict(x_val,w):
# linear combo
val = w[0] + w[1]*f(x_val,w)
return val
# least squares
def least_squares(w):
cost = 0
for p in range(0,len(y)):
x_p = x[p]
y_p = y[p]
cost +=(predict(x_p,w) - y_p)**2
return cost
Now we can minimize the cost using gradient descent
We can then plot the resulting fit to the data
# static transform image
demo1.static_img(w_best,least_squares,predict)
# static transform image
demo1.static_img(w_best,least_squares,predict,f1_x = [f(v,w_best) for v in x])
A properly designed feature (or set of features) provides a good nonlinear fit in the original feature space and, simultaneously, a good linear fit in the transformed feature space.
Next we examine a population growth dataset - of Yeast cells growing in a constrained enviroment (source).
Both input and output normalized
# create instance of linear regression demo, used below and in the next examples
demo2 = nonlib.nonlinear_regression_visualizer.Visualizer(csvname = datapath + 'yeast.csv')
# plot dataset
demo2.plot_data(xlabel = 'time', ylabel = 'yeast population')
# nonlinearity
def f(x,w):
# shove through nonlinearity
c = np.tanh( w[2] + w[3]*x)
return c
# prediction
def predict(x,w):
# linear combo
val = w[0] + w[1]*f(x,w)
return val
# least squares
def least_squares(w):
cost = 0
for p in range(0,len(y)):
x_p = x[p]
y_p = y[p]
cost +=(predict(x_p,w) - y_p)**2
return cost
# static transform image
demo2.static_img(w_best,least_squares,predict,f1_x = [f(v,w_best) for v in x])
# create instance of linear regression demo, used below and in the next examples
demo3 = nonlib.nonlinear_regression_visualizer.Visualizer(csvname = datapath + 'galileo_ramp_data.csv')
# plot dataset
demo3.plot_data(xlabel = 'time (in seconds)',ylabel = 'portion of ramp traveled')
# feature transformation
def f1(x):
return x
def f2(x):
return x**2
# prediction
def predict(x,w):
# linear combo
a = w[0] + w[1]*f1(x) + w[2]*f2(x)
return a
# least squares
def least_squares(w):
cost = 0
for p in range(0,len(y)):
x_p = x[p]
y_p = y[p]
cost +=(predict(x_p,w) - y_p)**2
return cost
And we optimize using e.g., (unnormalized) gradient descent
# static transform image
f1_x = [f1(v) for v in x]; f2_x = [f2(v) for v in x]
demo3.static_img(w_best,least_squares,predict,f1_x=f1_x,f2_x=f2_x,view = [35,100])
# create instance of linear regression demo, used below and in the next examples
demo4 = nonlib.nonlinear_classification_visualizer.Visualizer(csvname = datapath + 'signed_projectile.csv')
# plot dataset
demo4.plot_data()
then once tuned if $\text{predict}(x) > 0$ $x$ in class $+1$, if $\text{predict}(x)<0$ class $-1$
# feature transforms
def f1(x):
return x
def f2(x):
return x**2
# prediction
def predict(x,w):
# linear combo
val = w[0] + w[1]*f1(x) + w[2]*f2(x)
return val
# softmax cost
def softmax(w):
cost = 0
for p in range(0,len(y)):
x_p = x[p]
y_p = y[p]
cost += np.log(1 + np.exp(-y_p*predict(x_p,w)))
return cost
# static transform image
demo4.static_N1_img(w_best,least_squares,predict,f1_x = [f1(s) for s in x], f2_x = [f2(s) for s in x],view = [25,15])
Properly designed features provide good nonlinear separation in the original feature space and, simultaneously, good linear separation in the transformed feature space.
# create instance of linear regression demo, used below and in the next examples
demo5 = nonlib.nonlinear_classification_visualizer.Visualizer(datapath + 'ellipse_2class_data.csv')
# an implementation of the least squares cost function for linear regression for N = 2 input dimension datasets
demo5.plot_data()
# features
def f1(x):
return (x[0])**2
def f2(x):
return (x[1])**2
# prediction
def predict(x,w):
# linear combo
a = w[0] + w[1]*f1(x) + w[2]*f2(x)
return a
# softmax cost
def softmax(w):
cost = 0
for p in range(0,len(y)):
x_p = x[p,:]
y_p = y[p]
cost += np.log(1 + np.exp(-y_p*predict(x_p,w)))
return cost
# illustrate results
demo5.static_N2_img(w_best,softmax,predict,f1,f2,view1 = [20,45],view2 = [20,30])
A properly designed feature (or set of features) provides a good nonlinear fit in the original feature space and, simultaneously, a good linear fit in the transformed feature space.
Properly designed features provide good nonlinear separation in the original feature space and, simultaneously, good linear separation in the transformed feature space.