<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import time <EOL> import numpy as np <EOL> from sklearn . svm import SVR <EOL> from sklearn . model_selection import GridSearchCV <EOL> from sklearn . model_selection import learning_curve <EOL> from sklearn . kernel_ridge import KernelRidge <EOL> import matplotlib . pyplot as plt <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = <NUM_LIT:5> * rng . rand ( <NUM_LIT> , <NUM_LIT:1> ) <EOL> y = np . sin ( X ) . ravel ( ) <EOL> y [ : : <NUM_LIT:5> ] += <NUM_LIT:3> * ( <NUM_LIT:0.5> - rng . rand ( X . shape [ <NUM_LIT:0> ] / <NUM_LIT:5> ) ) <EOL> X_plot = np . linspace ( <NUM_LIT:0> , <NUM_LIT:5> , <NUM_LIT> ) [ : , None ] <EOL> train_size = <NUM_LIT:100> <EOL> svr = GridSearchCV ( SVR ( kernel = '<STR_LIT>' , gamma = <NUM_LIT:0.1> ) , cv = <NUM_LIT:5> , <EOL> param_grid = { "<STR_LIT:C>" : [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> "<STR_LIT>" : np . logspace ( - <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:5> ) } ) <EOL> kr = GridSearchCV ( KernelRidge ( kernel = '<STR_LIT>' , gamma = <NUM_LIT:0.1> ) , cv = <NUM_LIT:5> , <EOL> param_grid = { "<STR_LIT>" : [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ] , <EOL> "<STR_LIT>" : np . logspace ( - <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:5> ) } ) <EOL> t0 = time . time ( ) <EOL> svr . fit ( X [ : train_size ] , y [ : train_size ] ) <EOL> svr_fit = time . time ( ) - t0 <EOL> print ( "<STR_LIT>" <EOL> % svr_fit ) <EOL> t0 = time . time ( ) <EOL> kr . fit ( X [ : train_size ] , y [ : train_size ] ) <EOL> kr_fit = time . time ( ) - t0 <EOL> print ( "<STR_LIT>" <EOL> % kr_fit ) <EOL> sv_ratio = svr . best_estimator_ . support_ . shape [ <NUM_LIT:0> ] / train_size <EOL> print ( "<STR_LIT>" % sv_ratio ) <EOL> t0 = time . time ( ) <EOL> y_svr = svr . predict ( X_plot ) <EOL> svr_predict = time . time ( ) - t0 <EOL> print ( "<STR_LIT>" <EOL> % ( X_plot . shape [ <NUM_LIT:0> ] , svr_predict ) ) <EOL> t0 = time . time ( ) <EOL> y_kr = kr . predict ( X_plot ) <EOL> kr_predict = time . time ( ) - t0 <EOL> print ( "<STR_LIT>" <EOL> % ( X_plot . shape [ <NUM_LIT:0> ] , kr_predict ) ) <EOL> sv_ind = svr . best_estimator_ . support_ <EOL> plt . scatter ( X [ sv_ind ] , y [ sv_ind ] , c = '<STR_LIT:r>' , s = <NUM_LIT:50> , label = '<STR_LIT>' , <EOL> zorder = <NUM_LIT:2> ) <EOL> plt . scatter ( X [ : <NUM_LIT:100> ] , y [ : <NUM_LIT:100> ] , c = '<STR_LIT:k>' , label = '<STR_LIT:data>' , zorder = <NUM_LIT:1> ) <EOL> plt . hold ( '<STR_LIT>' ) <EOL> plt . plot ( X_plot , y_svr , c = '<STR_LIT:r>' , <EOL> label = '<STR_LIT>' % ( svr_fit , svr_predict ) ) <EOL> plt . plot ( X_plot , y_kr , c = '<STR_LIT:g>' , <EOL> label = '<STR_LIT>' % ( kr_fit , kr_predict ) ) <EOL> plt . xlabel ( '<STR_LIT:data>' ) <EOL> plt . ylabel ( '<STR_LIT:target>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . legend ( ) <EOL> plt . figure ( ) <EOL> X = <NUM_LIT:5> * rng . rand ( <NUM_LIT> , <NUM_LIT:1> ) <EOL> y = np . sin ( X ) . ravel ( ) <EOL> y [ : : <NUM_LIT:5> ] += <NUM_LIT:3> * ( <NUM_LIT:0.5> - rng . rand ( X . shape [ <NUM_LIT:0> ] / <NUM_LIT:5> ) ) <EOL> sizes = np . logspace ( <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:7> ) <EOL> for name , estimator in { "<STR_LIT>" : KernelRidge ( kernel = '<STR_LIT>' , alpha = <NUM_LIT:0.1> , <EOL> gamma = <NUM_LIT:10> ) , <EOL> "<STR_LIT>" : SVR ( kernel = '<STR_LIT>' , C = <NUM_LIT> , gamma = <NUM_LIT:10> ) } . items ( ) : <EOL> train_time = [ ] <EOL> test_time = [ ] <EOL> for train_test_size in sizes : <EOL> t0 = time . time ( ) <EOL> estimator . fit ( X [ : train_test_size ] , y [ : train_test_size ] ) <EOL> train_time . append ( time . time ( ) - t0 ) <EOL> t0 = time . time ( ) <EOL> estimator . predict ( X_plot [ : <NUM_LIT:1000> ] ) <EOL> test_time . append ( time . time ( ) - t0 ) <EOL> plt . plot ( sizes , train_time , '<STR_LIT>' , color = "<STR_LIT:r>" if name == "<STR_LIT>" else "<STR_LIT:g>" , <EOL> label = "<STR_LIT>" % name ) <EOL> plt . plot ( sizes , test_time , '<STR_LIT>' , color = "<STR_LIT:r>" if name == "<STR_LIT>" else "<STR_LIT:g>" , <EOL> label = "<STR_LIT>" % name ) <EOL> plt . xscale ( "<STR_LIT>" ) <EOL> plt . yscale ( "<STR_LIT>" ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( "<STR_LIT>" ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . legend ( loc = "<STR_LIT>" ) <EOL> plt . figure ( ) <EOL> svr = SVR ( kernel = '<STR_LIT>' , C = <NUM_LIT> , gamma = <NUM_LIT:0.1> ) <EOL> kr = KernelRidge ( kernel = '<STR_LIT>' , alpha = <NUM_LIT:0.1> , gamma = <NUM_LIT:0.1> ) <EOL> train_sizes , train_scores_svr , test_scores_svr = learning_curve ( svr , X [ : <NUM_LIT:100> ] , y [ : <NUM_LIT:100> ] , train_sizes = np . linspace ( <NUM_LIT:0.1> , <NUM_LIT:1> , <NUM_LIT:10> ) , <EOL> scoring = "<STR_LIT>" , cv = <NUM_LIT:10> ) <EOL> train_sizes_abs , train_scores_kr , test_scores_kr = learning_curve ( kr , X [ : <NUM_LIT:100> ] , y [ : <NUM_LIT:100> ] , train_sizes = np . linspace ( <NUM_LIT:0.1> , <NUM_LIT:1> , <NUM_LIT:10> ) , <EOL> scoring = "<STR_LIT>" , cv = <NUM_LIT:10> ) <EOL> plt . plot ( train_sizes , test_scores_svr . mean ( <NUM_LIT:1> ) , '<STR_LIT>' , color = "<STR_LIT:r>" , <EOL> label = "<STR_LIT>" ) <EOL> plt . plot ( train_sizes , test_scores_kr . mean ( <NUM_LIT:1> ) , '<STR_LIT>' , color = "<STR_LIT:g>" , <EOL> label = "<STR_LIT>" ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( "<STR_LIT>" ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . legend ( loc = "<STR_LIT>" ) <EOL> plt . show ( ) </s>
