def display_prediction(basis, num_basis=<NUM_LIT:4>, wlim=(-<NUM_LIT:1.>,<NUM_LIT:1.>), fig=None, ax=None, xlim=None, ylim=None, num_points=<NUM_LIT:1000>, offset=<NUM_LIT:0.0>, **kwargs):
import numpy as np<EOL>import pylab as plt<EOL>if fig is not None:<EOL><INDENT>if ax is None:<EOL><INDENT>ax = fig.gca()<EOL><DEDENT><DEDENT>if xlim is None:<EOL><INDENT>if ax is not None:<EOL><INDENT>xlim = ax.get_xlim()<EOL><DEDENT>else:<EOL><INDENT>xlim = (-<NUM_LIT>, <NUM_LIT>)<EOL><DEDENT><DEDENT>if ylim is None:<EOL><INDENT>if ax is not None:<EOL><INDENT>ylim = ax.get_ylim()<EOL><DEDENT>else:<EOL><INDENT>ylim = (-<NUM_LIT:1.>, <NUM_LIT:1.>)<EOL><DEDENT><DEDENT>x = np.zeros((num_points, <NUM_LIT:1>))<EOL>x[:, <NUM_LIT:0>] = np.linspace(xlim[<NUM_LIT:0>], xlim[<NUM_LIT:1>], num_points)<EOL>param_args = {}<EOL>for i in range(num_basis):<EOL><INDENT>lim = list(wlim)<EOL>if i ==<NUM_LIT:0>:<EOL><INDENT>lim[<NUM_LIT:0>] += offset<EOL>lim[<NUM_LIT:1>] += offset<EOL><DEDENT>param_args['<STR_LIT>' + str(i)] = tuple(lim)<EOL><DEDENT>def predict_basis(w, basis, x, num_basis, **kwargs):<EOL><INDENT>Phi = basis(x, num_basis, **kwargs)<EOL>f = np.dot(Phi, w)<EOL>return f, Phi<EOL><DEDENT>if type(basis) is dict:<EOL><INDENT>use_basis = basis[list(basis.keys())[<NUM_LIT:0>]]<EOL><DEDENT>else:<EOL><INDENT>use_basis = basis<EOL><DEDENT>f, Phi = predict_basis(np.zeros((num_basis, <NUM_LIT:1>)),<EOL>use_basis, x, num_basis,<EOL>**kwargs)<EOL>if fig is None:<EOL><INDENT>fig, ax=plt.subplots(figsize=(<NUM_LIT:12>,<NUM_LIT:4>))<EOL>ax.set_ylim(ylim)<EOL>ax.set_xlim(xlim)<EOL><DEDENT>predline = ax.plot(x, f, linewidth=<NUM_LIT:2>)[<NUM_LIT:0>]<EOL>basislines = []<EOL>for i in range(num_basis):<EOL><INDENT>basislines.append(ax.plot(x, Phi[:, i], '<STR_LIT:r>')[<NUM_LIT:0>])<EOL><DEDENT>ax.set_ylim(ylim)<EOL>ax.set_xlim(xlim)<EOL>def generate_function(basis, num_basis, predline, basislines, basis_args, display_basis, offset, **kwargs):<EOL><INDENT>w = np.zeros((num_basis, <NUM_LIT:1>))<EOL>for i in range(num_basis):<EOL><INDENT>w[i] = kwargs['<STR_LIT>'+ str(i)]<EOL><DEDENT>f, Phi = predict_basis(w, basis, x, num_basis, **basis_args)<EOL>predline.set_xdata(x[:, <NUM_LIT:0>])<EOL>predline.set_ydata(f)<EOL>for i in range(num_basis):<EOL><INDENT>basislines[i].set_xdata(x[:, <NUM_LIT:0>])<EOL>basislines[i].set_ydata(Phi[:, i])<EOL><DEDENT>if display_basis:<EOL><INDENT>for i in range(num_basis):<EOL><INDENT>basislines[i].set_alpha(<NUM_LIT:1>) <EOL><DEDENT><DEDENT>else:<EOL><INDENT>for i in range(num_basis):<EOL><INDENT>basislines[i].set_alpha(<NUM_LIT:0>) <EOL><DEDENT><DEDENT>display(fig)<EOL><DEDENT>if type(basis) is not dict:<EOL><INDENT>basis = fixed(basis)<EOL><DEDENT>plt.close(fig)<EOL>interact(generate_function, <EOL>basis=basis,<EOL>num_basis=fixed(num_basis),<EOL>predline=fixed(predline),<EOL>basislines=fixed(basislines),<EOL>basis_args=fixed(kwargs),<EOL>offset = fixed(offset),<EOL>display_basis = False,<EOL>**param_args)<EOL>
Interactive widget for displaying a prediction function based on summing separate basis functions.
    :param basis: a function handle that calls the basis functions.
    :type basis: function handle.
    :param xlim: limits of the x axis to use.
    :param ylim: limits of the y axis to use.
    :param wlim: limits for the basis function weights.