<s> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from sklearn . neighbors import KNeighborsRegressor <EOL> def make_dataset ( n_samples = <NUM_LIT:100> ) : <EOL> rnd = np . random . RandomState ( <NUM_LIT> ) <EOL> x = np . linspace ( - <NUM_LIT:3> , <NUM_LIT:3> , n_samples ) <EOL> y_no_noise = np . sin ( <NUM_LIT:4> * x ) + x <EOL> y = y_no_noise + rnd . normal ( size = len ( x ) ) <EOL> return x , y <EOL> def plot_regression_datasets ( ) : <EOL> fig , axes = plt . subplots ( <NUM_LIT:1> , <NUM_LIT:3> , figsize = ( <NUM_LIT:15> , <NUM_LIT:5> ) ) <EOL> for n_samples , ax in zip ( [ <NUM_LIT:10> , <NUM_LIT:100> , <NUM_LIT:1000> ] , axes ) : <EOL> x , y = make_dataset ( n_samples ) <EOL> ax . plot ( x , y , '<STR_LIT:o>' , alpha = <NUM_LIT> ) <EOL> def plot_kneighbors_regularization ( ) : <EOL> rnd = np . random . RandomState ( <NUM_LIT> ) <EOL> x = np . linspace ( - <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:100> ) <EOL> y_no_noise = np . sin ( <NUM_LIT:4> * x ) + x <EOL> y = y_no_noise + rnd . normal ( size = len ( x ) ) <EOL> X = x [ : , np . newaxis ] <EOL> fig , axes = plt . subplots ( <NUM_LIT:1> , <NUM_LIT:3> , figsize = ( <NUM_LIT:15> , <NUM_LIT:5> ) ) <EOL> x_test = np . linspace ( - <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:1000> ) <EOL> for n_neighbors , ax in zip ( [ <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:20> ] , axes . ravel ( ) ) : <EOL> kneighbor_regression = KNeighborsRegressor ( n_neighbors = n_neighbors ) <EOL> kneighbor_regression . fit ( X , y ) <EOL> ax . plot ( x , y_no_noise , label = "<STR_LIT>" ) <EOL> ax . plot ( x , y , "<STR_LIT:o>" , label = "<STR_LIT:data>" ) <EOL> ax . plot ( x_test , kneighbor_regression . predict ( x_test [ : , np . newaxis ] ) , <EOL> label = "<STR_LIT>" ) <EOL> ax . legend ( loc = "<STR_LIT>" ) <EOL> ax . set_title ( "<STR_LIT>" % n_neighbors ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> plot_kneighbors_regularization ( ) <EOL> plt . show ( ) </s>
