<s> from itertools import product <EOL> import numpy as np <EOL> from scipy . sparse import ( bsr_matrix , coo_matrix , csc_matrix , csr_matrix , <EOL> dok_matrix , lil_matrix ) <EOL> from sklearn import metrics <EOL> from sklearn . model_selection import train_test_split <EOL> from sklearn . model_selection import cross_val_score <EOL> from sklearn . utils . testing import assert_array_almost_equal <EOL> from sklearn . utils . testing import assert_array_equal <EOL> from sklearn . utils . testing import assert_raises <EOL> from sklearn . utils . testing import assert_equal <EOL> from sklearn . utils . testing import assert_true <EOL> from sklearn . utils . testing import assert_warns <EOL> from sklearn . utils . testing import ignore_warnings <EOL> from sklearn . utils . testing import assert_greater <EOL> from sklearn . utils . validation import check_random_state <EOL> from sklearn . metrics . pairwise import pairwise_distances <EOL> from sklearn import neighbors , datasets <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> iris = datasets . load_iris ( ) <EOL> perm = rng . permutation ( iris . target . size ) <EOL> iris . data = iris . data [ perm ] <EOL> iris . target = iris . target [ perm ] <EOL> digits = datasets . load_digits ( ) <EOL> perm = rng . permutation ( digits . target . size ) <EOL> digits . data = digits . data [ perm ] <EOL> digits . target = digits . target [ perm ] <EOL> SPARSE_TYPES = ( bsr_matrix , coo_matrix , csc_matrix , csr_matrix , dok_matrix , <EOL> lil_matrix ) <EOL> SPARSE_OR_DENSE = SPARSE_TYPES + ( np . asarray , ) <EOL> ALGORITHMS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> P = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , np . inf ) <EOL> neighbors . kneighbors_graph = ignore_warnings ( neighbors . kneighbors_graph ) <EOL> neighbors . radius_neighbors_graph = ignore_warnings ( <EOL> neighbors . radius_neighbors_graph ) <EOL> def _weight_func ( dist ) : <EOL> """<STR_LIT>""" <EOL> with np . errstate ( divide = '<STR_LIT:ignore>' ) : <EOL> retval = <NUM_LIT:1.> / dist <EOL> return retval ** <NUM_LIT:2> <EOL> def test_unsupervised_kneighbors ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:5> , <EOL> n_query_pts = <NUM_LIT:2> , n_neighbors = <NUM_LIT:5> ) : <EOL> X = rng . rand ( n_samples , n_features ) <EOL> test = rng . rand ( n_query_pts , n_features ) <EOL> for p in P : <EOL> results_nodist = [ ] <EOL> results = [ ] <EOL> for algorithm in ALGORITHMS : <EOL> neigh = neighbors . NearestNeighbors ( n_neighbors = n_neighbors , <EOL> algorithm = algorithm , <EOL> p = p ) <EOL> neigh . fit ( X ) <EOL> results_nodist . append ( neigh . kneighbors ( test , <EOL> return_distance = False ) ) <EOL> results . append ( neigh . kneighbors ( test , return_distance = True ) ) <EOL> for i in range ( len ( results ) - <NUM_LIT:1> ) : <EOL> assert_array_almost_equal ( results_nodist [ i ] , results [ i ] [ <NUM_LIT:1> ] ) <EOL> assert_array_almost_equal ( results [ i ] [ <NUM_LIT:0> ] , results [ i + <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> assert_array_almost_equal ( results [ i ] [ <NUM_LIT:1> ] , results [ i + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> def test_unsupervised_inputs ( ) : <EOL> X = rng . random_sample ( ( <NUM_LIT:10> , <NUM_LIT:3> ) ) <EOL> nbrs_fid = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:1> ) <EOL> nbrs_fid . fit ( X ) <EOL> dist1 , ind1 = nbrs_fid . kneighbors ( X ) <EOL> nbrs = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:1> ) <EOL> for input in ( nbrs_fid , neighbors . BallTree ( X ) , neighbors . KDTree ( X ) ) : <EOL> nbrs . fit ( input ) <EOL> dist2 , ind2 = nbrs . kneighbors ( X ) <EOL> assert_array_almost_equal ( dist1 , dist2 ) <EOL> assert_array_almost_equal ( ind1 , ind2 ) <EOL> def test_precomputed ( random_state = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = rng . random_sample ( ( <NUM_LIT:10> , <NUM_LIT:4> ) ) <EOL> Y = rng . random_sample ( ( <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> DXX = metrics . pairwise_distances ( X , metric = '<STR_LIT>' ) <EOL> DYX = metrics . pairwise_distances ( Y , X , metric = '<STR_LIT>' ) <EOL> for method in [ '<STR_LIT>' ] : <EOL> nbrs_X = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:3> ) <EOL> nbrs_X . fit ( X ) <EOL> dist_X , ind_X = getattr ( nbrs_X , method ) ( Y ) <EOL> nbrs_D = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:3> , algorithm = '<STR_LIT>' , <EOL> metric = '<STR_LIT>' ) <EOL> nbrs_D . fit ( DXX ) <EOL> dist_D , ind_D = getattr ( nbrs_D , method ) ( DYX ) <EOL> assert_array_almost_equal ( dist_X , dist_D ) <EOL> assert_array_almost_equal ( ind_X , ind_D ) <EOL> nbrs_D = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:3> , algorithm = '<STR_LIT>' , <EOL> metric = '<STR_LIT>' ) <EOL> nbrs_D . fit ( DXX ) <EOL> dist_D , ind_D = getattr ( nbrs_D , method ) ( DYX ) <EOL> assert_array_almost_equal ( dist_X , dist_D ) <EOL> assert_array_almost_equal ( ind_X , ind_D ) <EOL> dist_X , ind_X = getattr ( nbrs_X , method ) ( None ) <EOL> dist_D , ind_D = getattr ( nbrs_D , method ) ( None ) <EOL> assert_array_almost_equal ( dist_X , dist_D ) <EOL> assert_array_almost_equal ( ind_X , ind_D ) <EOL> assert_raises ( ValueError , getattr ( nbrs_D , method ) , X ) <EOL> target = np . arange ( X . shape [ <NUM_LIT:0> ] ) <EOL> for Est in ( neighbors . KNeighborsClassifier , <EOL> neighbors . RadiusNeighborsClassifier , <EOL> neighbors . KNeighborsRegressor , <EOL> neighbors . RadiusNeighborsRegressor ) : <EOL> print ( Est ) <EOL> est = Est ( metric = '<STR_LIT>' ) <EOL> est . radius = est . n_neighbors = <NUM_LIT:1> <EOL> pred_X = est . fit ( X , target ) . predict ( Y ) <EOL> est . metric = '<STR_LIT>' <EOL> pred_D = est . fit ( DXX , target ) . predict ( DYX ) <EOL> assert_array_almost_equal ( pred_X , pred_D ) <EOL> def test_precomputed_cross_validation ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . rand ( <NUM_LIT:20> , <NUM_LIT:2> ) <EOL> D = pairwise_distances ( X , metric = '<STR_LIT>' ) <EOL> y = rng . randint ( <NUM_LIT:3> , size = <NUM_LIT:20> ) <EOL> for Est in ( neighbors . KNeighborsClassifier , <EOL> neighbors . RadiusNeighborsClassifier , <EOL> neighbors . KNeighborsRegressor , <EOL> neighbors . RadiusNeighborsRegressor ) : <EOL> metric_score = cross_val_score ( Est ( ) , X , y ) <EOL> precomp_score = cross_val_score ( Est ( metric = '<STR_LIT>' ) , D , y ) <EOL> assert_array_equal ( metric_score , precomp_score ) <EOL> def test_unsupervised_radius_neighbors ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:5> , <EOL> n_query_pts = <NUM_LIT:2> , radius = <NUM_LIT:0.5> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = rng . rand ( n_samples , n_features ) <EOL> test = rng . rand ( n_query_pts , n_features ) <EOL> for p in P : <EOL> results = [ ] <EOL> for algorithm in ALGORITHMS : <EOL> neigh = neighbors . NearestNeighbors ( radius = radius , <EOL> algorithm = algorithm , <EOL> p = p ) <EOL> neigh . fit ( X ) <EOL> ind1 = neigh . radius_neighbors ( test , return_distance = False ) <EOL> dist , ind = neigh . radius_neighbors ( test , return_distance = True ) <EOL> for ( d , i , i1 ) in zip ( dist , ind , ind1 ) : <EOL> j = d . argsort ( ) <EOL> d [ : ] = d [ j ] <EOL> i [ : ] = i [ j ] <EOL> i1 [ : ] = i1 [ j ] <EOL> results . append ( ( dist , ind ) ) <EOL> assert_array_almost_equal ( np . concatenate ( list ( ind ) ) , <EOL> np . concatenate ( list ( ind1 ) ) ) <EOL> for i in range ( len ( results ) - <NUM_LIT:1> ) : <EOL> assert_array_almost_equal ( np . concatenate ( list ( results [ i ] [ <NUM_LIT:0> ] ) ) , <EOL> np . concatenate ( list ( results [ i + <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) ) ) , <EOL> assert_array_almost_equal ( np . concatenate ( list ( results [ i ] [ <NUM_LIT:1> ] ) ) , <EOL> np . concatenate ( list ( results [ i + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) ) ) <EOL> def test_kneighbors_classifier ( n_samples = <NUM_LIT> , <EOL> n_features = <NUM_LIT:5> , <EOL> n_test_pts = <NUM_LIT:10> , <EOL> n_neighbors = <NUM_LIT:5> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> y = ( ( X ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:1> ) < <NUM_LIT> ) . astype ( np . int ) <EOL> y_str = y . astype ( str ) <EOL> weight_func = _weight_func <EOL> for algorithm in ALGORITHMS : <EOL> for weights in [ '<STR_LIT>' , '<STR_LIT>' , weight_func ] : <EOL> knn = neighbors . KNeighborsClassifier ( n_neighbors = n_neighbors , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> knn . fit ( X , y ) <EOL> epsilon = <NUM_LIT> * ( <NUM_LIT:2> * rng . rand ( <NUM_LIT:1> , n_features ) - <NUM_LIT:1> ) <EOL> y_pred = knn . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_array_equal ( y_pred , y [ : n_test_pts ] ) <EOL> knn . fit ( X , y_str ) <EOL> y_pred = knn . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_array_equal ( y_pred , y_str [ : n_test_pts ] ) <EOL> def test_kneighbors_classifier_float_labels ( n_samples = <NUM_LIT> , n_features = <NUM_LIT:5> , <EOL> n_test_pts = <NUM_LIT:10> , n_neighbors = <NUM_LIT:5> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> y = ( ( X ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:1> ) < <NUM_LIT> ) . astype ( np . int ) <EOL> knn = neighbors . KNeighborsClassifier ( n_neighbors = n_neighbors ) <EOL> knn . fit ( X , y . astype ( np . float ) ) <EOL> epsilon = <NUM_LIT> * ( <NUM_LIT:2> * rng . rand ( <NUM_LIT:1> , n_features ) - <NUM_LIT:1> ) <EOL> y_pred = knn . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_array_equal ( y_pred , y [ : n_test_pts ] ) <EOL> def test_kneighbors_classifier_predict_proba ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> y = np . array ( [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> cls = neighbors . KNeighborsClassifier ( n_neighbors = <NUM_LIT:3> , p = <NUM_LIT:1> ) <EOL> cls . fit ( X , y ) <EOL> y_prob = cls . predict_proba ( X ) <EOL> real_prob = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT> / <NUM_LIT:3> , <NUM_LIT:1.> / <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:1.> / <NUM_LIT:3> , <NUM_LIT> / <NUM_LIT:3> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1.> / <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT> / <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1.> / <NUM_LIT:3> , <NUM_LIT> / <NUM_LIT:3> ] , <EOL> [ <NUM_LIT> / <NUM_LIT:3> , <NUM_LIT:1.> / <NUM_LIT:3> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT> / <NUM_LIT:3> , <NUM_LIT:1.> / <NUM_LIT:3> , <NUM_LIT:0> ] ] ) <EOL> assert_array_equal ( real_prob , y_prob ) <EOL> cls . fit ( X , y . astype ( str ) ) <EOL> y_prob = cls . predict_proba ( X ) <EOL> assert_array_equal ( real_prob , y_prob ) <EOL> cls = neighbors . KNeighborsClassifier ( <EOL> n_neighbors = <NUM_LIT:2> , p = <NUM_LIT:1> , weights = '<STR_LIT>' ) <EOL> cls . fit ( X , y ) <EOL> y_prob = cls . predict_proba ( np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:0> ] , [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] ] ) ) <EOL> real_prob = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert_array_almost_equal ( real_prob , y_prob ) <EOL> def test_radius_neighbors_classifier ( n_samples = <NUM_LIT> , <EOL> n_features = <NUM_LIT:5> , <EOL> n_test_pts = <NUM_LIT:10> , <EOL> radius = <NUM_LIT:0.5> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> y = ( ( X ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:1> ) < <NUM_LIT> ) . astype ( np . int ) <EOL> y_str = y . astype ( str ) <EOL> weight_func = _weight_func <EOL> for algorithm in ALGORITHMS : <EOL> for weights in [ '<STR_LIT>' , '<STR_LIT>' , weight_func ] : <EOL> neigh = neighbors . RadiusNeighborsClassifier ( radius = radius , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> neigh . fit ( X , y ) <EOL> epsilon = <NUM_LIT> * ( <NUM_LIT:2> * rng . rand ( <NUM_LIT:1> , n_features ) - <NUM_LIT:1> ) <EOL> y_pred = neigh . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_array_equal ( y_pred , y [ : n_test_pts ] ) <EOL> neigh . fit ( X , y_str ) <EOL> y_pred = neigh . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_array_equal ( y_pred , y_str [ : n_test_pts ] ) <EOL> def test_radius_neighbors_classifier_when_no_neighbors ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> y = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> radius = <NUM_LIT:0.1> <EOL> z1 = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> z2 = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> weight_func = _weight_func <EOL> for outlier_label in [ <NUM_LIT:0> , - <NUM_LIT:1> , None ] : <EOL> for algorithm in ALGORITHMS : <EOL> for weights in [ '<STR_LIT>' , '<STR_LIT>' , weight_func ] : <EOL> rnc = neighbors . RadiusNeighborsClassifier <EOL> clf = rnc ( radius = radius , weights = weights , algorithm = algorithm , <EOL> outlier_label = outlier_label ) <EOL> clf . fit ( X , y ) <EOL> assert_array_equal ( np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , <EOL> clf . predict ( z1 ) ) <EOL> if outlier_label is None : <EOL> assert_raises ( ValueError , clf . predict , z2 ) <EOL> elif False : <EOL> assert_array_equal ( np . array ( [ <NUM_LIT:1> , outlier_label ] ) , <EOL> clf . predict ( z2 ) ) <EOL> def test_radius_neighbors_classifier_outlier_labeling ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> y = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> radius = <NUM_LIT:0.1> <EOL> z1 = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> z2 = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> correct_labels1 = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> correct_labels2 = np . array ( [ <NUM_LIT:1> , - <NUM_LIT:1> ] ) <EOL> weight_func = _weight_func <EOL> for algorithm in ALGORITHMS : <EOL> for weights in [ '<STR_LIT>' , '<STR_LIT>' , weight_func ] : <EOL> clf = neighbors . RadiusNeighborsClassifier ( radius = radius , <EOL> weights = weights , <EOL> algorithm = algorithm , <EOL> outlier_label = - <NUM_LIT:1> ) <EOL> clf . fit ( X , y ) <EOL> assert_array_equal ( correct_labels1 , clf . predict ( z1 ) ) <EOL> assert_array_equal ( correct_labels2 , clf . predict ( z2 ) ) <EOL> def test_radius_neighbors_classifier_zero_distance ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> y = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> radius = <NUM_LIT:0.1> <EOL> z1 = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> correct_labels1 = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> weight_func = _weight_func <EOL> for algorithm in ALGORITHMS : <EOL> for weights in [ '<STR_LIT>' , '<STR_LIT>' , weight_func ] : <EOL> clf = neighbors . RadiusNeighborsClassifier ( radius = radius , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> clf . fit ( X , y ) <EOL> assert_array_equal ( correct_labels1 , clf . predict ( z1 ) ) <EOL> def test_neighbors_regressors_zero_distance ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> y = np . array ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.0> ] ) <EOL> radius = <NUM_LIT> <EOL> z = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> rnn_correct_labels = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> knn_correct_unif = np . array ( [ <NUM_LIT> , <NUM_LIT:1.0> ] ) <EOL> knn_correct_dist = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> for algorithm in ALGORITHMS : <EOL> for weights in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> rnn = neighbors . RadiusNeighborsRegressor ( radius = radius , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> rnn . fit ( X , y ) <EOL> assert_array_almost_equal ( rnn_correct_labels , rnn . predict ( z ) ) <EOL> for weights , corr_labels in zip ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ knn_correct_unif , knn_correct_dist ] ) : <EOL> knn = neighbors . KNeighborsRegressor ( n_neighbors = <NUM_LIT:2> , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> knn . fit ( X , y ) <EOL> assert_array_almost_equal ( corr_labels , knn . predict ( z ) ) <EOL> def test_radius_neighbors_boundary_handling ( ) : <EOL> """<STR_LIT>""" <EOL> X = np . array ( [ [ <NUM_LIT> ] , [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> radius = <NUM_LIT> <EOL> for algorithm in ALGORITHMS : <EOL> nbrs = neighbors . NearestNeighbors ( radius = radius , <EOL> algorithm = algorithm ) . fit ( X ) <EOL> results = nbrs . radius_neighbors ( [ [ <NUM_LIT:0.0> ] ] , return_distance = False ) <EOL> assert_equal ( results . shape , ( <NUM_LIT:1> , ) ) <EOL> assert_equal ( results . dtype , object ) <EOL> assert_array_equal ( results [ <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> def test_RadiusNeighborsClassifier_multioutput ( ) : <EOL> rng = check_random_state ( <NUM_LIT:0> ) <EOL> n_features = <NUM_LIT:2> <EOL> n_samples = <NUM_LIT> <EOL> n_output = <NUM_LIT:3> <EOL> X = rng . rand ( n_samples , n_features ) <EOL> y = rng . randint ( <NUM_LIT:0> , <NUM_LIT:3> , ( n_samples , n_output ) ) <EOL> X_train , X_test , y_train , y_test = train_test_split ( X , y , random_state = <NUM_LIT:0> ) <EOL> weights = [ None , '<STR_LIT>' , '<STR_LIT>' , _weight_func ] <EOL> for algorithm , weights in product ( ALGORITHMS , weights ) : <EOL> y_pred_so = [ ] <EOL> for o in range ( n_output ) : <EOL> rnn = neighbors . RadiusNeighborsClassifier ( weights = weights , <EOL> algorithm = algorithm ) <EOL> rnn . fit ( X_train , y_train [ : , o ] ) <EOL> y_pred_so . append ( rnn . predict ( X_test ) ) <EOL> y_pred_so = np . vstack ( y_pred_so ) . T <EOL> assert_equal ( y_pred_so . shape , y_test . shape ) <EOL> rnn_mo = neighbors . RadiusNeighborsClassifier ( weights = weights , <EOL> algorithm = algorithm ) <EOL> rnn_mo . fit ( X_train , y_train ) <EOL> y_pred_mo = rnn_mo . predict ( X_test ) <EOL> assert_equal ( y_pred_mo . shape , y_test . shape ) <EOL> assert_array_almost_equal ( y_pred_mo , y_pred_so ) <EOL> def test_kneighbors_classifier_sparse ( n_samples = <NUM_LIT> , <EOL> n_features = <NUM_LIT:5> , <EOL> n_test_pts = <NUM_LIT:10> , <EOL> n_neighbors = <NUM_LIT:5> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> X *= X > <NUM_LIT> <EOL> y = ( ( X ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:1> ) < <NUM_LIT> ) . astype ( np . int ) <EOL> for sparsemat in SPARSE_TYPES : <EOL> knn = neighbors . KNeighborsClassifier ( n_neighbors = n_neighbors , <EOL> algorithm = '<STR_LIT>' ) <EOL> knn . fit ( sparsemat ( X ) , y ) <EOL> epsilon = <NUM_LIT> * ( <NUM_LIT:2> * rng . rand ( <NUM_LIT:1> , n_features ) - <NUM_LIT:1> ) <EOL> for sparsev in SPARSE_TYPES + ( np . asarray , ) : <EOL> X_eps = sparsev ( X [ : n_test_pts ] + epsilon ) <EOL> y_pred = knn . predict ( X_eps ) <EOL> assert_array_equal ( y_pred , y [ : n_test_pts ] ) <EOL> def test_KNeighborsClassifier_multioutput ( ) : <EOL> rng = check_random_state ( <NUM_LIT:0> ) <EOL> n_features = <NUM_LIT:5> <EOL> n_samples = <NUM_LIT:50> <EOL> n_output = <NUM_LIT:3> <EOL> X = rng . rand ( n_samples , n_features ) <EOL> y = rng . randint ( <NUM_LIT:0> , <NUM_LIT:3> , ( n_samples , n_output ) ) <EOL> X_train , X_test , y_train , y_test = train_test_split ( X , y , random_state = <NUM_LIT:0> ) <EOL> weights = [ None , '<STR_LIT>' , '<STR_LIT>' , _weight_func ] <EOL> for algorithm , weights in product ( ALGORITHMS , weights ) : <EOL> y_pred_so = [ ] <EOL> y_pred_proba_so = [ ] <EOL> for o in range ( n_output ) : <EOL> knn = neighbors . KNeighborsClassifier ( weights = weights , <EOL> algorithm = algorithm ) <EOL> knn . fit ( X_train , y_train [ : , o ] ) <EOL> y_pred_so . append ( knn . predict ( X_test ) ) <EOL> y_pred_proba_so . append ( knn . predict_proba ( X_test ) ) <EOL> y_pred_so = np . vstack ( y_pred_so ) . T <EOL> assert_equal ( y_pred_so . shape , y_test . shape ) <EOL> assert_equal ( len ( y_pred_proba_so ) , n_output ) <EOL> knn_mo = neighbors . KNeighborsClassifier ( weights = weights , <EOL> algorithm = algorithm ) <EOL> knn_mo . fit ( X_train , y_train ) <EOL> y_pred_mo = knn_mo . predict ( X_test ) <EOL> assert_equal ( y_pred_mo . shape , y_test . shape ) <EOL> assert_array_almost_equal ( y_pred_mo , y_pred_so ) <EOL> y_pred_proba_mo = knn_mo . predict_proba ( X_test ) <EOL> assert_equal ( len ( y_pred_proba_mo ) , n_output ) <EOL> for proba_mo , proba_so in zip ( y_pred_proba_mo , y_pred_proba_so ) : <EOL> assert_array_almost_equal ( proba_mo , proba_so ) <EOL> def test_kneighbors_regressor ( n_samples = <NUM_LIT> , <EOL> n_features = <NUM_LIT:5> , <EOL> n_test_pts = <NUM_LIT:10> , <EOL> n_neighbors = <NUM_LIT:3> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> y = np . sqrt ( ( X ** <NUM_LIT:2> ) . sum ( <NUM_LIT:1> ) ) <EOL> y /= y . max ( ) <EOL> y_target = y [ : n_test_pts ] <EOL> weight_func = _weight_func <EOL> for algorithm in ALGORITHMS : <EOL> for weights in [ '<STR_LIT>' , '<STR_LIT>' , weight_func ] : <EOL> knn = neighbors . KNeighborsRegressor ( n_neighbors = n_neighbors , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> knn . fit ( X , y ) <EOL> epsilon = <NUM_LIT> * ( <NUM_LIT:2> * rng . rand ( <NUM_LIT:1> , n_features ) - <NUM_LIT:1> ) <EOL> y_pred = knn . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_true ( np . all ( abs ( y_pred - y_target ) < <NUM_LIT> ) ) <EOL> def test_KNeighborsRegressor_multioutput_uniform_weight ( ) : <EOL> rng = check_random_state ( <NUM_LIT:0> ) <EOL> n_features = <NUM_LIT:5> <EOL> n_samples = <NUM_LIT> <EOL> n_output = <NUM_LIT:4> <EOL> X = rng . rand ( n_samples , n_features ) <EOL> y = rng . rand ( n_samples , n_output ) <EOL> X_train , X_test , y_train , y_test = train_test_split ( X , y , random_state = <NUM_LIT:0> ) <EOL> for algorithm , weights in product ( ALGORITHMS , [ None , '<STR_LIT>' ] ) : <EOL> knn = neighbors . KNeighborsRegressor ( weights = weights , <EOL> algorithm = algorithm ) <EOL> knn . fit ( X_train , y_train ) <EOL> neigh_idx = knn . kneighbors ( X_test , return_distance = False ) <EOL> y_pred_idx = np . array ( [ np . mean ( y_train [ idx ] , axis = <NUM_LIT:0> ) <EOL> for idx in neigh_idx ] ) <EOL> y_pred = knn . predict ( X_test ) <EOL> assert_equal ( y_pred . shape , y_test . shape ) <EOL> assert_equal ( y_pred_idx . shape , y_test . shape ) <EOL> assert_array_almost_equal ( y_pred , y_pred_idx ) <EOL> def test_kneighbors_regressor_multioutput ( n_samples = <NUM_LIT> , <EOL> n_features = <NUM_LIT:5> , <EOL> n_test_pts = <NUM_LIT:10> , <EOL> n_neighbors = <NUM_LIT:3> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> y = np . sqrt ( ( X ** <NUM_LIT:2> ) . sum ( <NUM_LIT:1> ) ) <EOL> y /= y . max ( ) <EOL> y = np . vstack ( [ y , y ] ) . T <EOL> y_target = y [ : n_test_pts ] <EOL> weights = [ '<STR_LIT>' , '<STR_LIT>' , _weight_func ] <EOL> for algorithm , weights in product ( ALGORITHMS , weights ) : <EOL> knn = neighbors . KNeighborsRegressor ( n_neighbors = n_neighbors , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> knn . fit ( X , y ) <EOL> epsilon = <NUM_LIT> * ( <NUM_LIT:2> * rng . rand ( <NUM_LIT:1> , n_features ) - <NUM_LIT:1> ) <EOL> y_pred = knn . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_equal ( y_pred . shape , y_target . shape ) <EOL> assert_true ( np . all ( np . abs ( y_pred - y_target ) < <NUM_LIT> ) ) <EOL> def test_radius_neighbors_regressor ( n_samples = <NUM_LIT> , <EOL> n_features = <NUM_LIT:3> , <EOL> n_test_pts = <NUM_LIT:10> , <EOL> radius = <NUM_LIT:0.5> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> y = np . sqrt ( ( X ** <NUM_LIT:2> ) . sum ( <NUM_LIT:1> ) ) <EOL> y /= y . max ( ) <EOL> y_target = y [ : n_test_pts ] <EOL> weight_func = _weight_func <EOL> for algorithm in ALGORITHMS : <EOL> for weights in [ '<STR_LIT>' , '<STR_LIT>' , weight_func ] : <EOL> neigh = neighbors . RadiusNeighborsRegressor ( radius = radius , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> neigh . fit ( X , y ) <EOL> epsilon = <NUM_LIT> * ( <NUM_LIT:2> * rng . rand ( <NUM_LIT:1> , n_features ) - <NUM_LIT:1> ) <EOL> y_pred = neigh . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_true ( np . all ( abs ( y_pred - y_target ) < radius / <NUM_LIT:2> ) ) <EOL> def test_RadiusNeighborsRegressor_multioutput_with_uniform_weight ( ) : <EOL> rng = check_random_state ( <NUM_LIT:0> ) <EOL> n_features = <NUM_LIT:5> <EOL> n_samples = <NUM_LIT> <EOL> n_output = <NUM_LIT:4> <EOL> X = rng . rand ( n_samples , n_features ) <EOL> y = rng . rand ( n_samples , n_output ) <EOL> X_train , X_test , y_train , y_test = train_test_split ( X , y , random_state = <NUM_LIT:0> ) <EOL> for algorithm , weights in product ( ALGORITHMS , [ None , '<STR_LIT>' ] ) : <EOL> rnn = neighbors . RadiusNeighborsRegressor ( weights = weights , <EOL> algorithm = algorithm ) <EOL> rnn . fit ( X_train , y_train ) <EOL> neigh_idx = rnn . radius_neighbors ( X_test , return_distance = False ) <EOL> y_pred_idx = np . array ( [ np . mean ( y_train [ idx ] , axis = <NUM_LIT:0> ) <EOL> for idx in neigh_idx ] ) <EOL> y_pred_idx = np . array ( y_pred_idx ) <EOL> y_pred = rnn . predict ( X_test ) <EOL> assert_equal ( y_pred_idx . shape , y_test . shape ) <EOL> assert_equal ( y_pred . shape , y_test . shape ) <EOL> assert_array_almost_equal ( y_pred , y_pred_idx ) <EOL> def test_RadiusNeighborsRegressor_multioutput ( n_samples = <NUM_LIT> , <EOL> n_features = <NUM_LIT:5> , <EOL> n_test_pts = <NUM_LIT:10> , <EOL> n_neighbors = <NUM_LIT:3> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> y = np . sqrt ( ( X ** <NUM_LIT:2> ) . sum ( <NUM_LIT:1> ) ) <EOL> y /= y . max ( ) <EOL> y = np . vstack ( [ y , y ] ) . T <EOL> y_target = y [ : n_test_pts ] <EOL> weights = [ '<STR_LIT>' , '<STR_LIT>' , _weight_func ] <EOL> for algorithm , weights in product ( ALGORITHMS , weights ) : <EOL> rnn = neighbors . RadiusNeighborsRegressor ( n_neighbors = n_neighbors , <EOL> weights = weights , <EOL> algorithm = algorithm ) <EOL> rnn . fit ( X , y ) <EOL> epsilon = <NUM_LIT> * ( <NUM_LIT:2> * rng . rand ( <NUM_LIT:1> , n_features ) - <NUM_LIT:1> ) <EOL> y_pred = rnn . predict ( X [ : n_test_pts ] + epsilon ) <EOL> assert_equal ( y_pred . shape , y_target . shape ) <EOL> assert_true ( np . all ( np . abs ( y_pred - y_target ) < <NUM_LIT> ) ) <EOL> def test_kneighbors_regressor_sparse ( n_samples = <NUM_LIT> , <EOL> n_features = <NUM_LIT:5> , <EOL> n_test_pts = <NUM_LIT:10> , <EOL> n_neighbors = <NUM_LIT:5> , <EOL> random_state = <NUM_LIT:0> ) : <EOL> rng = np . random . RandomState ( random_state ) <EOL> X = <NUM_LIT:2> * rng . rand ( n_samples , n_features ) - <NUM_LIT:1> <EOL> y = ( ( X ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:1> ) < <NUM_LIT> ) . astype ( np . int ) <EOL> for sparsemat in SPARSE_TYPES : <EOL> knn = neighbors . KNeighborsRegressor ( n_neighbors = n_neighbors , <EOL> algorithm = '<STR_LIT>' ) <EOL> knn . fit ( sparsemat ( X ) , y ) <EOL> for sparsev in SPARSE_OR_DENSE : <EOL> X2 = sparsev ( X ) <EOL> assert_true ( np . mean ( knn . predict ( X2 ) . round ( ) == y ) > <NUM_LIT> ) <EOL> def test_neighbors_iris ( ) : <EOL> for algorithm in ALGORITHMS : <EOL> clf = neighbors . KNeighborsClassifier ( n_neighbors = <NUM_LIT:1> , <EOL> algorithm = algorithm ) <EOL> clf . fit ( iris . data , iris . target ) <EOL> assert_array_equal ( clf . predict ( iris . data ) , iris . target ) <EOL> clf . set_params ( n_neighbors = <NUM_LIT:9> , algorithm = algorithm ) <EOL> clf . fit ( iris . data , iris . target ) <EOL> assert_true ( np . mean ( clf . predict ( iris . data ) == iris . target ) > <NUM_LIT> ) <EOL> rgs = neighbors . KNeighborsRegressor ( n_neighbors = <NUM_LIT:5> , algorithm = algorithm ) <EOL> rgs . fit ( iris . data , iris . target ) <EOL> assert_greater ( np . mean ( rgs . predict ( iris . data ) . round ( ) == iris . target ) , <EOL> <NUM_LIT> ) <EOL> def test_neighbors_digits ( ) : <EOL> X = digits . data . astype ( '<STR_LIT>' ) <EOL> Y = digits . target <EOL> ( n_samples , n_features ) = X . shape <EOL> train_test_boundary = int ( n_samples * <NUM_LIT> ) <EOL> train = np . arange ( <NUM_LIT:0> , train_test_boundary ) <EOL> test = np . arange ( train_test_boundary , n_samples ) <EOL> ( X_train , Y_train , X_test , Y_test ) = X [ train ] , Y [ train ] , X [ test ] , Y [ test ] <EOL> clf = neighbors . KNeighborsClassifier ( n_neighbors = <NUM_LIT:1> , algorithm = '<STR_LIT>' ) <EOL> score_uint8 = clf . fit ( X_train , Y_train ) . score ( X_test , Y_test ) <EOL> score_float = clf . fit ( X_train . astype ( float ) , Y_train ) . score ( <EOL> X_test . astype ( float ) , Y_test ) <EOL> assert_equal ( score_uint8 , score_float ) <EOL> def test_kneighbors_graph ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT> , <NUM_LIT:1.> ] , [ <NUM_LIT:2> , <NUM_LIT:0> ] ] ) <EOL> A = neighbors . kneighbors_graph ( X , <NUM_LIT:1> , mode = '<STR_LIT>' , <EOL> include_self = True ) <EOL> assert_array_equal ( A . toarray ( ) , np . eye ( A . shape [ <NUM_LIT:0> ] ) ) <EOL> A = neighbors . kneighbors_graph ( X , <NUM_LIT:1> , mode = '<STR_LIT>' ) <EOL> assert_array_almost_equal ( <EOL> A . toarray ( ) , <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> ] ] ) <EOL> A = neighbors . kneighbors_graph ( X , <NUM_LIT:2> , mode = '<STR_LIT>' , <EOL> include_self = True ) <EOL> assert_array_equal ( <EOL> A . toarray ( ) , <EOL> [ [ <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ] ) <EOL> A = neighbors . kneighbors_graph ( X , <NUM_LIT:2> , mode = '<STR_LIT>' ) <EOL> assert_array_almost_equal ( <EOL> A . toarray ( ) , <EOL> [ [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> ] ] ) <EOL> A = neighbors . kneighbors_graph ( X , <NUM_LIT:3> , mode = '<STR_LIT>' , <EOL> include_self = True ) <EOL> assert_array_almost_equal ( <EOL> A . toarray ( ) , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> def test_kneighbors_graph_sparse ( seed = <NUM_LIT> ) : <EOL> rng = np . random . RandomState ( seed ) <EOL> X = rng . randn ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> Xcsr = csr_matrix ( X ) <EOL> for n_neighbors in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> for mode in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> assert_array_almost_equal ( <EOL> neighbors . kneighbors_graph ( X , <EOL> n_neighbors , <EOL> mode = mode ) . toarray ( ) , <EOL> neighbors . kneighbors_graph ( Xcsr , <EOL> n_neighbors , <EOL> mode = mode ) . toarray ( ) ) <EOL> def test_radius_neighbors_graph ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT> , <NUM_LIT:1.> ] , [ <NUM_LIT:2> , <NUM_LIT:0> ] ] ) <EOL> A = neighbors . radius_neighbors_graph ( X , <NUM_LIT> , mode = '<STR_LIT>' , <EOL> include_self = True ) <EOL> assert_array_equal ( <EOL> A . toarray ( ) , <EOL> [ [ <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] , <EOL> [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ] ) <EOL> A = neighbors . radius_neighbors_graph ( X , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> assert_array_almost_equal ( <EOL> A . toarray ( ) , <EOL> [ [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT:0.> ] ] ) <EOL> def test_radius_neighbors_graph_sparse ( seed = <NUM_LIT> ) : <EOL> rng = np . random . RandomState ( seed ) <EOL> X = rng . randn ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> Xcsr = csr_matrix ( X ) <EOL> for n_neighbors in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> for mode in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> assert_array_almost_equal ( <EOL> neighbors . radius_neighbors_graph ( X , <EOL> n_neighbors , <EOL> mode = mode ) . toarray ( ) , <EOL> neighbors . radius_neighbors_graph ( Xcsr , <EOL> n_neighbors , <EOL> mode = mode ) . toarray ( ) ) <EOL> def test_neighbors_badargs ( ) : <EOL> assert_raises ( ValueError , <EOL> neighbors . NearestNeighbors , <EOL> algorithm = '<STR_LIT>' ) <EOL> X = rng . random_sample ( ( <NUM_LIT:10> , <NUM_LIT:2> ) ) <EOL> Xsparse = csr_matrix ( X ) <EOL> y = np . ones ( <NUM_LIT:10> ) <EOL> for cls in ( neighbors . KNeighborsClassifier , <EOL> neighbors . RadiusNeighborsClassifier , <EOL> neighbors . KNeighborsRegressor , <EOL> neighbors . RadiusNeighborsRegressor ) : <EOL> assert_raises ( ValueError , <EOL> cls , <EOL> weights = '<STR_LIT>' ) <EOL> assert_raises ( ValueError , <EOL> cls , p = - <NUM_LIT:1> ) <EOL> assert_raises ( ValueError , <EOL> cls , algorithm = '<STR_LIT>' ) <EOL> nbrs = cls ( algorithm = '<STR_LIT>' , metric = '<STR_LIT>' ) <EOL> assert_raises ( ValueError , <EOL> nbrs . predict , <EOL> X ) <EOL> assert_raises ( ValueError , <EOL> ignore_warnings ( nbrs . fit ) , <EOL> Xsparse , y ) <EOL> nbrs = cls ( ) <EOL> assert_raises ( ValueError , <EOL> nbrs . fit , <EOL> np . ones ( ( <NUM_LIT:0> , <NUM_LIT:2> ) ) , np . ones ( <NUM_LIT:0> ) ) <EOL> assert_raises ( ValueError , <EOL> nbrs . fit , <EOL> X [ : , : , None ] , y ) <EOL> nbrs . fit ( X , y ) <EOL> assert_raises ( ValueError , <EOL> nbrs . predict , <EOL> [ [ ] ] ) <EOL> if ( isinstance ( cls , neighbors . KNeighborsClassifier ) or <EOL> isinstance ( cls , neighbors . KNeighborsRegressor ) ) : <EOL> nbrs = cls ( n_neighbors = - <NUM_LIT:1> ) <EOL> assert_raises ( ValueError , nbrs . fit , X , y ) <EOL> nbrs = neighbors . NearestNeighbors ( ) . fit ( X ) <EOL> assert_raises ( ValueError , nbrs . kneighbors_graph , X , mode = '<STR_LIT>' ) <EOL> assert_raises ( ValueError , nbrs . radius_neighbors_graph , X , mode = '<STR_LIT>' ) <EOL> def test_neighbors_metrics ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:3> , <EOL> n_query_pts = <NUM_LIT:2> , n_neighbors = <NUM_LIT:5> ) : <EOL> V = rng . rand ( n_features , n_features ) <EOL> VI = np . dot ( V , V . T ) <EOL> metrics = [ ( '<STR_LIT>' , { } ) , <EOL> ( '<STR_LIT>' , { } ) , <EOL> ( '<STR_LIT>' , dict ( p = <NUM_LIT:1> ) ) , <EOL> ( '<STR_LIT>' , dict ( p = <NUM_LIT:2> ) ) , <EOL> ( '<STR_LIT>' , dict ( p = <NUM_LIT:3> ) ) , <EOL> ( '<STR_LIT>' , dict ( p = np . inf ) ) , <EOL> ( '<STR_LIT>' , { } ) , <EOL> ( '<STR_LIT>' , dict ( V = rng . rand ( n_features ) ) ) , <EOL> ( '<STR_LIT>' , dict ( p = <NUM_LIT:3> , w = rng . rand ( n_features ) ) ) , <EOL> ( '<STR_LIT>' , dict ( VI = VI ) ) ] <EOL> algorithms = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> X = rng . rand ( n_samples , n_features ) <EOL> test = rng . rand ( n_query_pts , n_features ) <EOL> for metric , metric_params in metrics : <EOL> results = [ ] <EOL> p = metric_params . pop ( '<STR_LIT:p>' , <NUM_LIT:2> ) <EOL> for algorithm in algorithms : <EOL> if ( algorithm == '<STR_LIT>' and <EOL> metric not in neighbors . KDTree . valid_metrics ) : <EOL> assert_raises ( ValueError , <EOL> neighbors . NearestNeighbors , <EOL> algorithm = algorithm , <EOL> metric = metric , metric_params = metric_params ) <EOL> continue <EOL> neigh = neighbors . NearestNeighbors ( n_neighbors = n_neighbors , <EOL> algorithm = algorithm , <EOL> metric = metric , p = p , <EOL> metric_params = metric_params ) <EOL> neigh . fit ( X ) <EOL> results . append ( neigh . kneighbors ( test , return_distance = True ) ) <EOL> assert_array_almost_equal ( results [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , results [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> assert_array_almost_equal ( results [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , results [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> def test_callable_metric ( ) : <EOL> def custom_metric ( x1 , x2 ) : <EOL> return np . sqrt ( np . sum ( x1 ** <NUM_LIT:2> + x2 ** <NUM_LIT:2> ) ) <EOL> X = np . random . RandomState ( <NUM_LIT> ) . rand ( <NUM_LIT:20> , <NUM_LIT:2> ) <EOL> nbrs1 = neighbors . NearestNeighbors ( <NUM_LIT:3> , algorithm = '<STR_LIT>' , <EOL> metric = custom_metric ) <EOL> nbrs2 = neighbors . NearestNeighbors ( <NUM_LIT:3> , algorithm = '<STR_LIT>' , <EOL> metric = custom_metric ) <EOL> nbrs1 . fit ( X ) <EOL> nbrs2 . fit ( X ) <EOL> dist1 , ind1 = nbrs1 . kneighbors ( X ) <EOL> dist2 , ind2 = nbrs2 . kneighbors ( X ) <EOL> assert_array_almost_equal ( dist1 , dist2 ) <EOL> def test_metric_params_interface ( ) : <EOL> assert_warns ( SyntaxWarning , neighbors . KNeighborsClassifier , <EOL> metric_params = { '<STR_LIT:p>' : <NUM_LIT:3> } ) <EOL> def test_predict_sparse_ball_kd_tree ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . rand ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> y = rng . randint ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> nbrs1 = neighbors . KNeighborsClassifier ( <NUM_LIT:1> , algorithm = '<STR_LIT>' ) <EOL> nbrs2 = neighbors . KNeighborsRegressor ( <NUM_LIT:1> , algorithm = '<STR_LIT>' ) <EOL> for model in [ nbrs1 , nbrs2 ] : <EOL> model . fit ( X , y ) <EOL> assert_raises ( ValueError , model . predict , csr_matrix ( X ) ) <EOL> def test_non_euclidean_kneighbors ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . rand ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> dist_array = pairwise_distances ( X ) . flatten ( ) <EOL> np . sort ( dist_array ) <EOL> radius = dist_array [ <NUM_LIT:15> ] <EOL> for metric in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> nbrs_graph = neighbors . kneighbors_graph ( <EOL> X , <NUM_LIT:3> , metric = metric , mode = '<STR_LIT>' , <EOL> include_self = True ) . toarray ( ) <EOL> nbrs1 = neighbors . NearestNeighbors ( <NUM_LIT:3> , metric = metric ) . fit ( X ) <EOL> assert_array_equal ( nbrs_graph , nbrs1 . kneighbors_graph ( X ) . toarray ( ) ) <EOL> for metric in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> nbrs_graph = neighbors . radius_neighbors_graph ( <EOL> X , radius , metric = metric , mode = '<STR_LIT>' , <EOL> include_self = True ) . toarray ( ) <EOL> nbrs1 = neighbors . NearestNeighbors ( metric = metric , radius = radius ) . fit ( X ) <EOL> assert_array_equal ( nbrs_graph , nbrs1 . radius_neighbors_graph ( X ) . A ) <EOL> X_nbrs = neighbors . NearestNeighbors ( <NUM_LIT:3> , metric = '<STR_LIT>' ) <EOL> X_nbrs . fit ( X ) <EOL> assert_raises ( ValueError , neighbors . kneighbors_graph , X_nbrs , <NUM_LIT:3> , <EOL> metric = '<STR_LIT>' ) <EOL> X_nbrs = neighbors . NearestNeighbors ( radius = radius , metric = '<STR_LIT>' ) <EOL> X_nbrs . fit ( X ) <EOL> assert_raises ( ValueError , neighbors . radius_neighbors_graph , X_nbrs , <EOL> radius , metric = '<STR_LIT>' ) <EOL> def check_object_arrays ( nparray , list_check ) : <EOL> for ind , ele in enumerate ( nparray ) : <EOL> assert_array_equal ( ele , list_check [ ind ] ) <EOL> def test_k_and_radius_neighbors_train_is_not_query ( ) : <EOL> for algorithm in ALGORITHMS : <EOL> nn = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:1> , algorithm = algorithm ) <EOL> X = [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] ] <EOL> nn . fit ( X ) <EOL> test_data = [ [ <NUM_LIT:2> ] , [ <NUM_LIT:1> ] ] <EOL> dist , ind = nn . kneighbors ( test_data ) <EOL> assert_array_equal ( dist , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:0> ] ] ) <EOL> assert_array_equal ( ind , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] ] ) <EOL> dist , ind = nn . radius_neighbors ( [ [ <NUM_LIT:2> ] , [ <NUM_LIT:1> ] ] , radius = <NUM_LIT> ) <EOL> check_object_arrays ( dist , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> check_object_arrays ( ind , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> assert_array_equal ( <EOL> nn . kneighbors_graph ( test_data ) . A , [ [ <NUM_LIT:0.> , <NUM_LIT:1.> ] , [ <NUM_LIT:0.> , <NUM_LIT:1.> ] ] ) <EOL> assert_array_equal ( <EOL> nn . kneighbors_graph ( [ [ <NUM_LIT:2> ] , [ <NUM_LIT:1> ] ] , mode = '<STR_LIT>' ) . A , <EOL> np . array ( [ [ <NUM_LIT:0.> , <NUM_LIT:1.> ] , [ <NUM_LIT:0.> , <NUM_LIT:0.> ] ] ) ) <EOL> rng = nn . radius_neighbors_graph ( [ [ <NUM_LIT:2> ] , [ <NUM_LIT:1> ] ] , radius = <NUM_LIT> ) <EOL> assert_array_equal ( rng . A , [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> def test_k_and_radius_neighbors_X_None ( ) : <EOL> for algorithm in ALGORITHMS : <EOL> nn = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:1> , algorithm = algorithm ) <EOL> X = [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] ] <EOL> nn . fit ( X ) <EOL> dist , ind = nn . kneighbors ( ) <EOL> assert_array_equal ( dist , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] ] ) <EOL> assert_array_equal ( ind , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:0> ] ] ) <EOL> dist , ind = nn . radius_neighbors ( None , radius = <NUM_LIT> ) <EOL> check_object_arrays ( dist , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] ] ) <EOL> check_object_arrays ( ind , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:0> ] ] ) <EOL> rng = nn . radius_neighbors_graph ( None , radius = <NUM_LIT> ) <EOL> kng = nn . kneighbors_graph ( None ) <EOL> for graph in [ rng , kng ] : <EOL> assert_array_equal ( rng . A , [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> assert_array_equal ( rng . data , [ <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> assert_array_equal ( rng . indices , [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> X = [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] ] <EOL> nn = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:2> , algorithm = algorithm ) <EOL> nn . fit ( X ) <EOL> assert_array_equal ( <EOL> nn . kneighbors_graph ( ) . A , <EOL> np . array ( [ [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:0> ] ] ) ) <EOL> def test_k_and_radius_neighbors_duplicates ( ) : <EOL> for algorithm in ALGORITHMS : <EOL> nn = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:1> , algorithm = algorithm ) <EOL> nn . fit ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] ] ) <EOL> kng = nn . kneighbors_graph ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] ] , mode = '<STR_LIT>' ) <EOL> assert_array_equal ( <EOL> kng . A , <EOL> np . array ( [ [ <NUM_LIT:0.> , <NUM_LIT:0.> ] , [ <NUM_LIT:0.> , <NUM_LIT:0.> ] ] ) ) <EOL> assert_array_equal ( kng . data , [ <NUM_LIT:0.> , <NUM_LIT:0.> ] ) <EOL> assert_array_equal ( kng . indices , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> dist , ind = nn . radius_neighbors ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] ] , radius = <NUM_LIT> ) <EOL> check_object_arrays ( dist , [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> check_object_arrays ( ind , [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> rng = nn . radius_neighbors_graph ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] ] , radius = <NUM_LIT> ) <EOL> assert_array_equal ( rng . A , np . ones ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> rng = nn . radius_neighbors_graph ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] ] , radius = <NUM_LIT> , <EOL> mode = '<STR_LIT>' ) <EOL> assert_array_equal ( rng . A , [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> assert_array_equal ( rng . indices , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> assert_array_equal ( rng . data , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> X = np . ones ( ( <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> nn = neighbors . NearestNeighbors ( n_neighbors = <NUM_LIT:1> ) <EOL> nn . fit ( X ) <EOL> dist , ind = nn . kneighbors ( ) <EOL> assert_array_equal ( dist , np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:1> ) ) ) <EOL> assert_array_equal ( ind , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] ] ) <EOL> kng = nn . kneighbors_graph ( mode = '<STR_LIT>' ) <EOL> assert_array_equal ( <EOL> kng . A , np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:3> ) ) ) <EOL> assert_array_equal ( kng . data , np . zeros ( <NUM_LIT:3> ) ) <EOL> assert_array_equal ( kng . indices , [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:1.> ] ) <EOL> assert_array_equal ( <EOL> nn . kneighbors_graph ( ) . A , <EOL> np . array ( [ [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:0.> ] , [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] , [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:0.> ] ] ) ) <EOL> def test_include_self_neighbors_graph ( ) : <EOL> X = [ [ <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> ] ] <EOL> kng = neighbors . kneighbors_graph ( X , <NUM_LIT:1> , include_self = True ) . A <EOL> kng_not_self = neighbors . kneighbors_graph ( X , <NUM_LIT:1> , include_self = False ) . A <EOL> assert_array_equal ( kng , [ [ <NUM_LIT:1.> , <NUM_LIT:0.> ] , [ <NUM_LIT:0.> , <NUM_LIT:1.> ] ] ) <EOL> assert_array_equal ( kng_not_self , [ [ <NUM_LIT:0.> , <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , <NUM_LIT:0.> ] ] ) <EOL> rng = neighbors . radius_neighbors_graph ( X , <NUM_LIT> , include_self = True ) . A <EOL> rng_not_self = neighbors . radius_neighbors_graph ( <EOL> X , <NUM_LIT> , include_self = False ) . A <EOL> assert_array_equal ( rng , [ [ <NUM_LIT:1.> , <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , <NUM_LIT:1.> ] ] ) <EOL> assert_array_equal ( rng_not_self , [ [ <NUM_LIT:0.> , <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , <NUM_LIT:0.> ] ] ) <EOL> def test_same_knn_parallel ( ) : <EOL> X , y = datasets . make_classification ( n_samples = <NUM_LIT:30> , n_features = <NUM_LIT:5> , <EOL> n_redundant = <NUM_LIT:0> , random_state = <NUM_LIT:0> ) <EOL> X_train , X_test , y_train , y_test = train_test_split ( X , y ) <EOL> def check_same_knn_parallel ( algorithm ) : <EOL> clf = neighbors . KNeighborsClassifier ( n_neighbors = <NUM_LIT:3> , <EOL> algorithm = algorithm ) <EOL> clf . fit ( X_train , y_train ) <EOL> y = clf . predict ( X_test ) <EOL> dist , ind = clf . kneighbors ( X_test ) <EOL> graph = clf . kneighbors_graph ( X_test , mode = '<STR_LIT>' ) . toarray ( ) <EOL> clf . set_params ( n_jobs = <NUM_LIT:3> ) <EOL> clf . fit ( X_train , y_train ) <EOL> y_parallel = clf . predict ( X_test ) <EOL> dist_parallel , ind_parallel = clf . kneighbors ( X_test ) <EOL> graph_parallel = clf . kneighbors_graph ( X_test , mode = '<STR_LIT>' ) . toarray ( ) <EOL> assert_array_equal ( y , y_parallel ) <EOL> assert_array_almost_equal ( dist , dist_parallel ) <EOL> assert_array_equal ( ind , ind_parallel ) <EOL> assert_array_almost_equal ( graph , graph_parallel ) <EOL> for algorithm in ALGORITHMS : <EOL> yield check_same_knn_parallel , algorithm <EOL> def test_dtype_convert ( ) : <EOL> classifier = neighbors . KNeighborsClassifier ( n_neighbors = <NUM_LIT:1> ) <EOL> CLASSES = <NUM_LIT:15> <EOL> X = np . eye ( CLASSES ) <EOL> y = [ ch for ch in '<STR_LIT>' [ : CLASSES ] ] <EOL> result = classifier . fit ( X , y ) . predict ( X ) <EOL> assert_array_equal ( result , y ) </s>
