<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import pylab as pl <EOL> from matplotlib import ticker <EOL> from matplotlib . patches import FancyArrow <EOL> import warnings <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , message = '<STR_LIT>' ) <EOL> np . random . seed ( <NUM_LIT> ) <EOL> def test_func ( x , err = <NUM_LIT:0.5> ) : <EOL> return np . random . normal ( <NUM_LIT:10> - <NUM_LIT:1.> / ( x + <NUM_LIT:0.1> ) , err ) <EOL> def compute_error ( x , y , p ) : <EOL> yfit = np . polyval ( p , x ) <EOL> return np . sqrt ( np . mean ( ( y - yfit ) ** <NUM_LIT:2> ) ) <EOL> np . random . seed ( <NUM_LIT> ) <EOL> x = np . random . random ( <NUM_LIT:20> ) <EOL> y = np . sin ( <NUM_LIT:2> * x ) <EOL> p = np . polyfit ( x , y , <NUM_LIT:1> ) <EOL> xfit = np . linspace ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:10> ) <EOL> yfit = np . polyval ( p , xfit ) <EOL> pl . scatter ( x , y , c = '<STR_LIT:k>' ) <EOL> pl . plot ( xfit , yfit ) <EOL> pl . xlabel ( '<STR_LIT:x>' ) <EOL> pl . ylabel ( '<STR_LIT:y>' ) <EOL> pl . title ( '<STR_LIT>' ) <EOL> N = <NUM_LIT:8> <EOL> np . random . seed ( <NUM_LIT> ) <EOL> x = <NUM_LIT:10> ** np . linspace ( - <NUM_LIT:2> , <NUM_LIT:0> , N ) <EOL> y = test_func ( x ) <EOL> xfit = np . linspace ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1000> ) <EOL> titles = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> degrees = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:6> ] <EOL> pl . figure ( figsize = ( <NUM_LIT:9> , <NUM_LIT> ) ) <EOL> for i , d in enumerate ( degrees ) : <EOL> pl . subplot ( <NUM_LIT> + i , xticks = [ ] , yticks = [ ] ) <EOL> pl . scatter ( x , y , marker = '<STR_LIT:x>' , c = '<STR_LIT:k>' , s = <NUM_LIT:50> ) <EOL> p = np . polyfit ( x , y , d ) <EOL> yfit = np . polyval ( p , xfit ) <EOL> pl . plot ( xfit , yfit , '<STR_LIT>' ) <EOL> pl . xlim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> pl . ylim ( <NUM_LIT:0> , <NUM_LIT:12> ) <EOL> pl . xlabel ( '<STR_LIT>' ) <EOL> if i == <NUM_LIT:0> : <EOL> pl . ylabel ( '<STR_LIT>' ) <EOL> pl . title ( titles [ i ] ) <EOL> pl . subplots_adjust ( left = <NUM_LIT> , right = <NUM_LIT> , <EOL> bottom = <NUM_LIT> , top = <NUM_LIT> , <EOL> wspace = <NUM_LIT> ) <EOL> Ntrain = <NUM_LIT:100> <EOL> Ncrossval = <NUM_LIT:100> <EOL> error = <NUM_LIT:1.0> <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> x = np . random . random ( Ntrain + Ncrossval ) <EOL> y = test_func ( x , error ) <EOL> xtrain = x [ : Ntrain ] <EOL> ytrain = y [ : Ntrain ] <EOL> xcrossval = x [ Ntrain : ] <EOL> ycrossval = y [ Ntrain : ] <EOL> degrees = np . arange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> train_err = np . zeros ( len ( degrees ) ) <EOL> crossval_err = np . zeros ( len ( degrees ) ) <EOL> for i , d in enumerate ( degrees ) : <EOL> p = np . polyfit ( xtrain , ytrain , d ) <EOL> train_err [ i ] = compute_error ( xtrain , ytrain , p ) <EOL> crossval_err [ i ] = compute_error ( xcrossval , ycrossval , p ) <EOL> pl . figure ( ) <EOL> pl . title ( '<STR_LIT>' ) <EOL> pl . plot ( degrees , crossval_err , lw = <NUM_LIT:2> , label = '<STR_LIT>' ) <EOL> pl . plot ( degrees , train_err , lw = <NUM_LIT:2> , label = '<STR_LIT>' ) <EOL> pl . plot ( [ <NUM_LIT:0> , <NUM_LIT:20> ] , [ error , error ] , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> pl . legend ( ) <EOL> pl . xlabel ( '<STR_LIT>' ) <EOL> pl . ylabel ( '<STR_LIT>' ) <EOL> pl . gca ( ) . add_patch ( FancyArrow ( <NUM_LIT:5> , <NUM_LIT> , - <NUM_LIT:3> , <NUM_LIT:0> , width = <NUM_LIT> , <EOL> head_width = <NUM_LIT> , head_length = <NUM_LIT:1.0> , <EOL> length_includes_head = True ) ) <EOL> pl . text ( <NUM_LIT> , <NUM_LIT> , "<STR_LIT>" , fontsize = <NUM_LIT> , va = '<STR_LIT>' ) <EOL> pl . gca ( ) . add_patch ( FancyArrow ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> , - <NUM_LIT:0.1> , width = <NUM_LIT> , <EOL> head_width = <NUM_LIT:1.0> , head_length = <NUM_LIT> , <EOL> length_includes_head = True ) ) <EOL> pl . text ( <NUM_LIT> , <NUM_LIT> , "<STR_LIT>" , ha = '<STR_LIT:right>' , fontsize = <NUM_LIT> ) <EOL> Ntrain = <NUM_LIT:100> <EOL> Ncrossval = <NUM_LIT:100> <EOL> error = <NUM_LIT:1.0> <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> x = np . random . random ( Ntrain + Ncrossval ) <EOL> y = test_func ( x , error ) <EOL> xtrain = x [ : Ntrain ] <EOL> ytrain = y [ : Ntrain ] <EOL> xcrossval = x [ Ntrain : ] <EOL> ycrossval = y [ Ntrain : ] <EOL> sizes = np . linspace ( <NUM_LIT:2> , Ntrain , <NUM_LIT:50> ) . astype ( int ) <EOL> train_err = np . zeros ( sizes . shape ) <EOL> crossval_err = np . zeros ( sizes . shape ) <EOL> pl . figure ( figsize = ( <NUM_LIT:10> , <NUM_LIT:5> ) ) <EOL> for j , d in enumerate ( ( <NUM_LIT:1> , <NUM_LIT:20> ) ) : <EOL> for i , size in enumerate ( sizes ) : <EOL> p = np . polyfit ( xtrain [ : size ] , ytrain [ : size ] , d ) <EOL> crossval_err [ i ] = compute_error ( xcrossval , ycrossval , p ) <EOL> train_err [ i ] = compute_error ( xtrain [ : size ] , ytrain [ : size ] , p ) <EOL> ax = pl . subplot ( <NUM_LIT> + j ) <EOL> pl . plot ( sizes , crossval_err , lw = <NUM_LIT:2> , label = '<STR_LIT>' ) <EOL> pl . plot ( sizes , train_err , lw = <NUM_LIT:2> , label = '<STR_LIT>' ) <EOL> pl . plot ( [ <NUM_LIT:0> , Ntrain ] , [ error , error ] , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> pl . xlabel ( '<STR_LIT>' ) <EOL> if j == <NUM_LIT:0> : <EOL> pl . ylabel ( '<STR_LIT>' ) <EOL> else : <EOL> ax . yaxis . set_major_formatter ( ticker . NullFormatter ( ) ) <EOL> pl . legend ( loc = <NUM_LIT:4> ) <EOL> pl . ylim ( <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> pl . xlim ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> pl . text ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT>' % d , ha = '<STR_LIT:right>' , va = '<STR_LIT>' , fontsize = '<STR_LIT>' ) <EOL> pl . subplots_adjust ( wspace = <NUM_LIT> , left = <NUM_LIT> , right = <NUM_LIT> , <EOL> bottom = <NUM_LIT:0.1> , top = <NUM_LIT> ) <EOL> pl . show ( ) </s>
