<s> from __future__ import division <EOL> import numpy as np <EOL> import scipy . sparse as sp <EOL> from scipy . misc import comb as combinations <EOL> from numpy . testing import assert_array_almost_equal <EOL> from sklearn . utils . random import sample_without_replacement <EOL> from sklearn . utils . random import random_choice_csc <EOL> from sklearn . utils . testing import ( <EOL> assert_raises , <EOL> assert_equal , <EOL> assert_true ) <EOL> def test_invalid_sample_without_replacement_algorithm ( ) : <EOL> assert_raises ( ValueError , sample_without_replacement , <NUM_LIT:5> , <NUM_LIT:4> , "<STR_LIT>" ) <EOL> def test_sample_without_replacement_algorithms ( ) : <EOL> methods = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> for m in methods : <EOL> def sample_without_replacement_method ( n_population , n_samples , <EOL> random_state = None ) : <EOL> return sample_without_replacement ( n_population , n_samples , <EOL> method = m , <EOL> random_state = random_state ) <EOL> check_edge_case_of_sample_int ( sample_without_replacement_method ) <EOL> check_sample_int ( sample_without_replacement_method ) <EOL> check_sample_int_distribution ( sample_without_replacement_method ) <EOL> def check_edge_case_of_sample_int ( sample_without_replacement ) : <EOL> assert_raises ( ValueError , sample_without_replacement , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> assert_raises ( ValueError , sample_without_replacement , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert_equal ( sample_without_replacement ( <NUM_LIT:0> , <NUM_LIT:0> ) . shape , ( <NUM_LIT:0> , ) ) <EOL> assert_equal ( sample_without_replacement ( <NUM_LIT:1> , <NUM_LIT:1> ) . shape , ( <NUM_LIT:1> , ) ) <EOL> assert_equal ( sample_without_replacement ( <NUM_LIT:5> , <NUM_LIT:0> ) . shape , ( <NUM_LIT:0> , ) ) <EOL> assert_equal ( sample_without_replacement ( <NUM_LIT:5> , <NUM_LIT:1> ) . shape , ( <NUM_LIT:1> , ) ) <EOL> assert_raises ( ValueError , sample_without_replacement , - <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> assert_raises ( ValueError , sample_without_replacement , <NUM_LIT:5> , - <NUM_LIT:1> ) <EOL> def check_sample_int ( sample_without_replacement ) : <EOL> n_population = <NUM_LIT:100> <EOL> for n_samples in range ( n_population + <NUM_LIT:1> ) : <EOL> s = sample_without_replacement ( n_population , n_samples ) <EOL> assert_equal ( len ( s ) , n_samples ) <EOL> unique = np . unique ( s ) <EOL> assert_equal ( np . size ( unique ) , n_samples ) <EOL> assert_true ( np . all ( unique < n_population ) ) <EOL> assert_equal ( np . size ( sample_without_replacement ( <NUM_LIT:0> , <NUM_LIT:0> ) ) , <NUM_LIT:0> ) <EOL> def check_sample_int_distribution ( sample_without_replacement ) : <EOL> n_population = <NUM_LIT:10> <EOL> n_trials = <NUM_LIT> <EOL> for n_samples in range ( n_population ) : <EOL> n_expected = combinations ( n_population , n_samples , exact = True ) <EOL> output = { } <EOL> for i in range ( n_trials ) : <EOL> output [ frozenset ( sample_without_replacement ( n_population , <EOL> n_samples ) ) ] = None <EOL> if len ( output ) == n_expected : <EOL> break <EOL> else : <EOL> raise AssertionError ( <EOL> "<STR_LIT>" % <EOL> ( len ( output ) , n_expected ) ) <EOL> def test_random_choice_csc ( n_samples = <NUM_LIT> , random_state = <NUM_LIT> ) : <EOL> classes = [ np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) ] <EOL> class_probabilites = [ np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ) , np . array ( [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) ] <EOL> got = random_choice_csc ( n_samples , classes , class_probabilites , <EOL> random_state ) <EOL> assert_true ( sp . issparse ( got ) ) <EOL> for k in range ( len ( classes ) ) : <EOL> p = np . bincount ( got . getcol ( k ) . toarray ( ) . ravel ( ) ) / float ( n_samples ) <EOL> assert_array_almost_equal ( class_probabilites [ k ] , p , decimal = <NUM_LIT:1> ) <EOL> classes = [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] <EOL> class_probabilites = [ np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:1> / <NUM_LIT:2> ] ) ] <EOL> got = random_choice_csc ( n_samples = n_samples , <EOL> classes = classes , <EOL> random_state = random_state ) <EOL> assert_true ( sp . issparse ( got ) ) <EOL> for k in range ( len ( classes ) ) : <EOL> p = np . bincount ( got . getcol ( k ) . toarray ( ) . ravel ( ) ) / float ( n_samples ) <EOL> assert_array_almost_equal ( class_probabilites [ k ] , p , decimal = <NUM_LIT:1> ) <EOL> classes = [ np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) ] <EOL> class_probabilites = [ np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> ] ) , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT:0.0> ] ) ] <EOL> got = random_choice_csc ( n_samples , classes , class_probabilites , <EOL> random_state ) <EOL> assert_true ( sp . issparse ( got ) ) <EOL> for k in range ( len ( classes ) ) : <EOL> p = np . bincount ( got . getcol ( k ) . toarray ( ) . ravel ( ) , <EOL> minlength = len ( class_probabilites [ k ] ) ) / n_samples <EOL> assert_array_almost_equal ( class_probabilites [ k ] , p , decimal = <NUM_LIT:1> ) <EOL> classes = [ [ <NUM_LIT:1> ] , [ <NUM_LIT:0> ] ] <EOL> class_probabilites = [ np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:1.0> ] ) , np . array ( [ <NUM_LIT:1.0> ] ) ] <EOL> got = random_choice_csc ( n_samples = n_samples , <EOL> classes = classes , <EOL> random_state = random_state ) <EOL> assert_true ( sp . issparse ( got ) ) <EOL> for k in range ( len ( classes ) ) : <EOL> p = np . bincount ( got . getcol ( k ) . toarray ( ) . ravel ( ) ) / n_samples <EOL> assert_array_almost_equal ( class_probabilites [ k ] , p , decimal = <NUM_LIT:1> ) <EOL> def test_random_choice_csc_errors ( ) : <EOL> classes = [ np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ] <EOL> class_probabilites = [ np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ) , np . array ( [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) ] <EOL> assert_raises ( ValueError , random_choice_csc , <NUM_LIT:4> , classes , <EOL> class_probabilites , <NUM_LIT:1> ) <EOL> classes = [ np . array ( [ "<STR_LIT:a>" , "<STR_LIT:1>" ] ) , np . array ( [ "<STR_LIT:z>" , "<STR_LIT:1>" , "<STR_LIT:2>" ] ) ] <EOL> class_probabilites = [ np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ) , np . array ( [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) ] <EOL> assert_raises ( ValueError , random_choice_csc , <NUM_LIT:4> , classes , <EOL> class_probabilites , <NUM_LIT:1> ) <EOL> classes = [ np . array ( [ <NUM_LIT> , <NUM_LIT:0.1> ] ) , np . array ( [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ] ) ] <EOL> class_probabilites = [ np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ) , np . array ( [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) ] <EOL> assert_raises ( ValueError , random_choice_csc , <NUM_LIT:4> , classes , <EOL> class_probabilites , <NUM_LIT:1> ) <EOL> classes = [ np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) ] <EOL> class_probabilites = [ np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> ] ) , np . array ( [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) ] <EOL> assert_raises ( ValueError , random_choice_csc , <NUM_LIT:4> , classes , <EOL> class_probabilites , <NUM_LIT:1> ) </s>
