<s> """<STR_LIT>""" <EOL> from scipy import sparse <EOL> import numpy as np <EOL> from sklearn . cluster . tests . common import generate_clustered_data <EOL> from sklearn . cluster . birch import Birch <EOL> from sklearn . cluster . hierarchical import AgglomerativeClustering <EOL> from sklearn . datasets import make_blobs <EOL> from sklearn . linear_model import ElasticNet <EOL> from sklearn . metrics import pairwise_distances_argmin , v_measure_score <EOL> from sklearn . utils . testing import assert_greater_equal <EOL> from sklearn . utils . testing import assert_equal <EOL> from sklearn . utils . testing import assert_greater <EOL> from sklearn . utils . testing import assert_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_warns <EOL> def test_n_samples_leaves_roots ( ) : <EOL> X , y = make_blobs ( n_samples = <NUM_LIT:10> ) <EOL> brc = Birch ( ) <EOL> brc . fit ( X ) <EOL> n_samples_root = sum ( [ sc . n_samples_ for sc in brc . root_ . subclusters_ ] ) <EOL> n_samples_leaves = sum ( [ sc . n_samples_ for leaf in brc . _get_leaves ( ) <EOL> for sc in leaf . subclusters_ ] ) <EOL> assert_equal ( n_samples_leaves , X . shape [ <NUM_LIT:0> ] ) <EOL> assert_equal ( n_samples_root , X . shape [ <NUM_LIT:0> ] ) <EOL> def test_partial_fit ( ) : <EOL> X , y = make_blobs ( n_samples = <NUM_LIT:100> ) <EOL> brc = Birch ( n_clusters = <NUM_LIT:3> ) <EOL> brc . fit ( X ) <EOL> brc_partial = Birch ( n_clusters = None ) <EOL> brc_partial . partial_fit ( X [ : <NUM_LIT:50> ] ) <EOL> brc_partial . partial_fit ( X [ <NUM_LIT:50> : ] ) <EOL> assert_array_equal ( brc_partial . subcluster_centers_ , <EOL> brc . subcluster_centers_ ) <EOL> brc_partial . set_params ( n_clusters = <NUM_LIT:3> ) <EOL> brc_partial . partial_fit ( None ) <EOL> assert_array_equal ( brc_partial . subcluster_labels_ , brc . subcluster_labels_ ) <EOL> def test_birch_predict ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = generate_clustered_data ( n_clusters = <NUM_LIT:3> , n_features = <NUM_LIT:3> , <EOL> n_samples_per_cluster = <NUM_LIT:10> ) <EOL> shuffle_indices = np . arange ( <NUM_LIT:30> ) <EOL> rng . shuffle ( shuffle_indices ) <EOL> X_shuffle = X [ shuffle_indices , : ] <EOL> brc = Birch ( n_clusters = <NUM_LIT:4> , threshold = <NUM_LIT:1.> ) <EOL> brc . fit ( X_shuffle ) <EOL> centroids = brc . subcluster_centers_ <EOL> assert_array_equal ( brc . labels_ , brc . predict ( X_shuffle ) ) <EOL> nearest_centroid = pairwise_distances_argmin ( X_shuffle , centroids ) <EOL> assert_almost_equal ( v_measure_score ( nearest_centroid , brc . labels_ ) , <NUM_LIT:1.0> ) <EOL> def test_n_clusters ( ) : <EOL> X , y = make_blobs ( n_samples = <NUM_LIT:100> , centers = <NUM_LIT:10> ) <EOL> brc1 = Birch ( n_clusters = <NUM_LIT:10> ) <EOL> brc1 . fit ( X ) <EOL> assert_greater ( len ( brc1 . subcluster_centers_ ) , <NUM_LIT:10> ) <EOL> assert_equal ( len ( np . unique ( brc1 . labels_ ) ) , <NUM_LIT:10> ) <EOL> gc = AgglomerativeClustering ( n_clusters = <NUM_LIT:10> ) <EOL> brc2 = Birch ( n_clusters = gc ) <EOL> brc2 . fit ( X ) <EOL> assert_array_equal ( brc1 . subcluster_labels_ , brc2 . subcluster_labels_ ) <EOL> assert_array_equal ( brc1 . labels_ , brc2 . labels_ ) <EOL> clf = ElasticNet ( ) <EOL> brc3 = Birch ( n_clusters = clf ) <EOL> assert_raises ( ValueError , brc3 . fit , X ) <EOL> brc4 = Birch ( threshold = <NUM_LIT> ) <EOL> assert_warns ( UserWarning , brc4 . fit , X ) <EOL> def test_sparse_X ( ) : <EOL> X , y = make_blobs ( n_samples = <NUM_LIT:100> , centers = <NUM_LIT:10> ) <EOL> brc = Birch ( n_clusters = <NUM_LIT:10> ) <EOL> brc . fit ( X ) <EOL> csr = sparse . csr_matrix ( X ) <EOL> brc_sparse = Birch ( n_clusters = <NUM_LIT:10> ) <EOL> brc_sparse . fit ( csr ) <EOL> assert_array_equal ( brc . labels_ , brc_sparse . labels_ ) <EOL> assert_array_equal ( brc . subcluster_centers_ , <EOL> brc_sparse . subcluster_centers_ ) <EOL> def check_branching_factor ( node , branching_factor ) : <EOL> subclusters = node . subclusters_ <EOL> assert_greater_equal ( branching_factor , len ( subclusters ) ) <EOL> for cluster in subclusters : <EOL> if cluster . child_ : <EOL> check_branching_factor ( cluster . child_ , branching_factor ) <EOL> def test_branching_factor ( ) : <EOL> X , y = make_blobs ( ) <EOL> branching_factor = <NUM_LIT:9> <EOL> brc = Birch ( n_clusters = None , branching_factor = branching_factor , <EOL> threshold = <NUM_LIT> ) <EOL> brc . fit ( X ) <EOL> check_branching_factor ( brc . root_ , branching_factor ) <EOL> brc = Birch ( n_clusters = <NUM_LIT:3> , branching_factor = branching_factor , <EOL> threshold = <NUM_LIT> ) <EOL> brc . fit ( X ) <EOL> check_branching_factor ( brc . root_ , branching_factor ) <EOL> brc = Birch ( n_clusters = None , branching_factor = <NUM_LIT:1> , threshold = <NUM_LIT> ) <EOL> assert_raises ( ValueError , brc . fit , X ) <EOL> def check_threshold ( birch_instance , threshold ) : <EOL> """<STR_LIT>""" <EOL> current_leaf = birch_instance . dummy_leaf_ . next_leaf_ <EOL> while current_leaf : <EOL> subclusters = current_leaf . subclusters_ <EOL> for sc in subclusters : <EOL> assert_greater_equal ( threshold , sc . radius ) <EOL> current_leaf = current_leaf . next_leaf_ <EOL> def test_threshold ( ) : <EOL> X , y = make_blobs ( n_samples = <NUM_LIT> , centers = <NUM_LIT:4> ) <EOL> brc = Birch ( threshold = <NUM_LIT:0.5> , n_clusters = None ) <EOL> brc . fit ( X ) <EOL> check_threshold ( brc , <NUM_LIT:0.5> ) <EOL> brc = Birch ( threshold = <NUM_LIT> , n_clusters = None ) <EOL> brc . fit ( X ) <EOL> check_threshold ( brc , <NUM_LIT> ) </s>
