#NEWLINE# def test_kernel_density_sampling( n_samples = 100 , n_features = 3 ) : #NEWLINE# #INDENT# rng = np . random . RandomState ( 0 ) #NEWLINE# X = rng . randn ( n_samples , n_features ) #NEWLINE# bandwidth = 0.2 #NEWLINE# for kernel in [ 'gaussian' , 'tophat' ] : #NEWLINE# #INDENT# kde = KernelDensity ( bandwidth , kernel = kernel ) . fit ( X ) #NEWLINE# samp = kde . sample ( 100 ) #NEWLINE# assert_equal ( X . shape , samp . shape ) #NEWLINE# nbrs = NearestNeighbors ( n_neighbors = 1 ) . fit ( X ) #NEWLINE# ( dist , ind ) = nbrs . kneighbors ( X , return_distance = True ) #NEWLINE# if ( kernel == 'tophat' ) : #NEWLINE# #INDENT# assert np . all ( ( dist < bandwidth ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( kernel == 'gaussian' ) : #NEWLINE# #INDENT# assert np . all ( ( dist < ( 5 * bandwidth ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for kernel in [ 'epanechnikov' , 'exponential' , 'linear' , 'cosine' ] : #NEWLINE# #INDENT# kde = KernelDensity ( bandwidth , kernel = kernel ) . fit ( X ) #NEWLINE# assert_raises ( NotImplementedError , kde . sample , 100 ) #NEWLINE# #UNINDENT# X = rng . randn ( 4 , 1 ) #NEWLINE# kde = KernelDensity ( kernel = 'gaussian' ) . fit ( X ) #NEWLINE# assert_equal ( kde . sample ( ) . shape , ( 1 , 1 ) )