

import warnings

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.metrics import pairwise_distances

warnings.filterwarnings('''ignore''')

lowerCAmelCase						:   Any										=    '''K-MEANS-CLUST/ '''





def 			_lowercase    (      __UpperCamelCase     :		List[Any]   ,       __UpperCamelCase     :		List[Any]   ,       __UpperCamelCase     :		Optional[Any]=None ):
      if seed is not None:  # useful for obtaining consistent results
            np.random.seed(__UpperCamelCase )
      snake_case__					=   data.shape[0]  # number of data points

      # Pick K indices from range [0, N).
      snake_case__					=   np.random.randint(0   ,       __UpperCamelCase   ,       __UpperCamelCase )

      # Keep centroids as dense format, as many entries will be nonzero due to averaging.
      # As long as at least one document in a cluster contains a word,
      # it will carry a nonzero weight in the TF-IDF vector of the centroid.
      snake_case__					=   data[rand_indices, :]

      return centroids





def 			_lowercase    (      __UpperCamelCase     :		Union[str, Any]   ,       __UpperCamelCase     :		Any ):
      return pairwise_distances(__UpperCamelCase   ,       __UpperCamelCase   ,       metric="""euclidean""" )





def 			_lowercase    (      __UpperCamelCase     :		List[str]   ,       __UpperCamelCase     :		Union[str, Any] ):
      # Compute distances between each data point and the set of centroids:
      # Fill in the blank (RHS only)
      snake_case__					=   centroid_pairwise_dist(__UpperCamelCase   ,       __UpperCamelCase )

      # Compute cluster assignments for each data point:
      # Fill in the blank (RHS only)
      snake_case__					=   np.argmin(__UpperCamelCase   ,       axis=1 )

      return cluster_assignment





def 			_lowercase    (      __UpperCamelCase     :		Optional[Any]   ,       __UpperCamelCase     :		Any   ,       __UpperCamelCase     :		Any ):
      snake_case__					=   []
      for i in range(__UpperCamelCase ):
            # Select all data points that belong to cluster i. Fill in the blank (RHS only)
            snake_case__					=   data[cluster_assignment == i]
            # Compute the mean of the data points. Fill in the blank (RHS only)
            snake_case__					=   member_data_points.mean(axis=0 )
            new_centroids.append(__UpperCamelCase )
      snake_case__					=   np.array(__UpperCamelCase )

      return new_centroids





def 			_lowercase    (      __UpperCamelCase     :		str   ,       __UpperCamelCase     :		Tuple   ,       __UpperCamelCase     :		Optional[int]   ,       __UpperCamelCase     :		List[Any] ):
      snake_case__					=   0.0
      for i in range(__UpperCamelCase ):
            # Select all data points that belong to cluster i. Fill in the blank (RHS only)
            snake_case__					=   data[cluster_assignment == i, :]

            if member_data_points.shape[0] > 0:  # check if i-th cluster is non-empty
                  # Compute distances from centroid to data points (RHS only)
                  snake_case__					=   pairwise_distances(
                      __UpperCamelCase   ,       [centroids[i]]   ,       metric="""euclidean""" )
                  snake_case__					=   distances**2
                  heterogeneity += np.sum(__UpperCamelCase )

      return heterogeneity





def 			_lowercase    (      __UpperCamelCase     :		Optional[Any]   ,       __UpperCamelCase     :		Tuple ):
      plt.figure(figsize=(7, 4) )
      plt.plot(__UpperCamelCase   ,       linewidth=4 )
      plt.xlabel("""# Iterations""" )
      plt.ylabel("""Heterogeneity""" )
      plt.title(F'''Heterogeneity of clustering over time, K={k:d}''' )
      plt.rcParams.update({"""font.size""": 16} )
      plt.show()





def 			_lowercase    (      __UpperCamelCase     :		List[Any]   ,       __UpperCamelCase     :		str   ,       __UpperCamelCase     :		Any   ,       __UpperCamelCase     :		int=500   ,       __UpperCamelCase     :		Optional[Any]=None   ,       __UpperCamelCase     :		int=False ):
      snake_case__					=   initial_centroids[:]
      snake_case__					=   None

      for itr in range(__UpperCamelCase ):
            if verbose:
                  print(__UpperCamelCase   ,       end="""""" )

            # 1. Make cluster assignments using nearest centroids
            snake_case__					=   assign_clusters(__UpperCamelCase   ,       __UpperCamelCase )

            # 2. Compute a new centroid for each of the k clusters, averaging all data
            #    points assigned to that cluster.
            snake_case__					=   revise_centroids(__UpperCamelCase   ,       __UpperCamelCase   ,       __UpperCamelCase )

            # Check for convergence: if none of the assignments changed, stop
            if (
                prev_cluster_assignment is not None
                and (prev_cluster_assignment == cluster_assignment).all()
            ):
                  break

            # Print number of new assignments
            if prev_cluster_assignment is not None:
                  snake_case__					=   np.sum(prev_cluster_assignment != cluster_assignment )
                  if verbose:
                        print(
                            F'''    {num_changed:5d} elements changed their cluster assignment.''' )

        # Record heterogeneity convergence metric
            if record_heterogeneity is not None:
                  # YOUR CODE HERE
                  snake_case__					=   compute_heterogeneity(__UpperCamelCase   ,       __UpperCamelCase   ,       __UpperCamelCase   ,       __UpperCamelCase )
                  record_heterogeneity.append(__UpperCamelCase )

            snake_case__					=   cluster_assignment[:]

      return centroids, cluster_assignment


# Mock test below
if False:  # change to true to run this test case.
       from sklearn import datasets as ds

       lowerCAmelCase						:   List[str]										=    ds.load_iris()
       lowerCAmelCase						:   Dict										=    3
       lowerCAmelCase						:   Optional[int]										=    []
       lowerCAmelCase						:   Tuple										=    get_initial_centroids(dataset['''data'''], k, seed=0)
       lowerCAmelCase					,  lowerCAmelCase						:   Tuple										=    kmeans(
           dataset['''data'''],
           k,
           initial_centroids,
           maxiter=400,
           record_heterogeneity=heterogeneity,
           verbose=True,
       )
       plot_heterogeneity(heterogeneity, k)





def 			_lowercase    (      __UpperCamelCase     :		pd.DataFrame   ,       __UpperCamelCase     :		np.ndarray   ,       __UpperCamelCase     :		Optional[Any]=None ):
      # Fill missing values with given rules
      if fill_missing_report:
            snake_case__					=   df.fillna(value=__UpperCamelCase )
      snake_case__					=   1
      snake_case__					=   df.select_dtypes(np.number ).columns
      snake_case__					=   (
          df.groupby(["""Cluster"""] )[  # construct report dataframe
              numeric_cols
          ]  # group by cluster number
          .agg(
              [
                  ("""sum""", np.sum),
                  ("""mean_with_zeros""", lambda __UpperCamelCase : np.mean(np.nan_to_num(__UpperCamelCase ) )),
                  ("""mean_without_zeros""", lambda __UpperCamelCase : x.replace(0   ,       np.NaN ).mean()),
                  (
                      """mean_25-75""",
                      lambda __UpperCamelCase : np.mean(
                          np.nan_to_num(
                              sorted(__UpperCamelCase )[
                                  round(len(__UpperCamelCase ) * 25 / 100 ) : round(len(__UpperCamelCase ) * 75 / 100 )
                              ] ) ),
                  ),
                  ("""mean_with_na""", np.mean),
                  ("""min""", lambda __UpperCamelCase : x.min()),
                  ("""5%""", lambda __UpperCamelCase : x.quantile(0.0_5 )),
                  ("""25%""", lambda __UpperCamelCase : x.quantile(0.2_5 )),
                  ("""50%""", lambda __UpperCamelCase : x.quantile(0.5_0 )),
                  ("""75%""", lambda __UpperCamelCase : x.quantile(0.7_5 )),
                  ("""95%""", lambda __UpperCamelCase : x.quantile(0.9_5 )),
                  ("""max""", lambda __UpperCamelCase : x.max()),
                  ("""count""", lambda __UpperCamelCase : x.count()),
                  ("""stdev""", lambda __UpperCamelCase : x.std()),
                  ("""mode""", lambda __UpperCamelCase : x.mode()[0]),
                  ("""median""", lambda __UpperCamelCase : x.median()),
                  ("""# > 0""", lambda __UpperCamelCase : (x > 0).sum()),
              ] )
          .T.reset_index()
          .rename(index=__UpperCamelCase   ,       columns={"""level_0""": """Features""", """level_1""": """Type"""} )
      )  # rename columns
      # calculate the size of cluster(count of clientID's)
      snake_case__					=   report[
          (report["""Features"""] == """dummy""") & (report["""Type"""] == """count""")
      ].copy()  # avoid SettingWithCopyWarning
      snake_case__					=   (
          """ClusterSize"""  # rename created cluster df to match report column names
      )
      snake_case__					=   """# of Customers"""
      snake_case__					=   pd.DataFrame(
          clustersize.iloc[:, 2:].values
          / clustersize.iloc[:, 2:].values.sum() )
      snake_case__					=   """% of Customers"""  # rename created cluster df to match report column names
      snake_case__					=   """ClusterProportion"""
      snake_case__					=   clusterproportion.columns.tolist()
      snake_case__					=   cols[-2:] + cols[:-2]
      snake_case__					=   clusterproportion[cols]  # rearrange columns to match report
      snake_case__					=   report.columns
      snake_case__					=   pd.DataFrame(
          abs(
              report[report["""Type"""] == """count"""].iloc[:, 2:].values
              - clustersize.iloc[:, 2:].values ) )  # generating df with count of nan values
      snake_case__					=   0
      snake_case__					=   """# of nan"""
      snake_case__					=   report[
          report["""Type"""] == """count"""
      ].Features.tolist()  # filling values in order to match report
      snake_case__					=   a.columns.tolist()
      snake_case__					=   cols[-2:] + cols[:-2]
      snake_case__					=   a[cols]  # rearrange columns to match report
      snake_case__					=   report.columns  # rename columns to match report
      snake_case__					=   report.drop(
          report[report.Type == """count"""].index )  # drop count values except cluster size
      snake_case__					=   pd.concat(
          [report, a, clustersize, clusterproportion]   ,       axis=0 )  # concat report with clustert size and nan values
      snake_case__					=   report["""Features"""].isin(__UpperCamelCase )
      snake_case__					=   report.columns.tolist()
      snake_case__					=   cols[0:2] + cols[-1:] + cols[2:-1]
      snake_case__					=   report[cols]
      snake_case__					=   {
          """ClusterSize""": 9,
          """ClusterProportion""": 8,
          """mean_with_zeros""": 7,
          """mean_with_na""": 6,
          """max""": 5,
          """50%""": 4,
          """min""": 3,
          """25%""": 2,
          """75%""": 1,
          """# of nan""": 0,
          """# > 0""": -1,
          """sum_with_na""": -2,
      }
      snake_case__					=   (
          report.assign(
              Sortera=lambda __UpperCamelCase : x.Type.map(__UpperCamelCase )   ,       Sortera=lambda __UpperCamelCase : list(reversed(range(len(__UpperCamelCase ) ) ) )   ,       )
          .sort_values(["""Sorter1""", """Mark""", """Sorter2"""]   ,       ascending=__UpperCamelCase )
          .drop(["""Sorter1""", """Sorter2"""]   ,       axis=1 )
      )
      snake_case__					=   """"""
      snake_case__					=   report.reset_index()
      snake_case__					=   report.drop(columns=["""index"""] )
      return report


if __name__ == "__main__":
       import doctest

       doctest.testmod()









import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase						:   Dict										=    logging.get_logger(__name__)

lowerCAmelCase						:   str										=    {
    '''facebook/hubert-base-ls960''': '''https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json''',
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}
class     SCREAMING_SNAKE_CASE__     (						__a		):





       '''simple docstring'''




       UpperCamelCase__ :     Tuple				    =       '''hubert'''

       def __init__(						self       :						Union[str, Any]    ,		lowerCAmelCase__       :						List[Any]=32    ,		lowerCAmelCase__       :						Any=768    ,		lowerCAmelCase__       :						List[Any]=12    ,		lowerCAmelCase__       :						Tuple=12    ,		lowerCAmelCase__       :						Optional[int]=3072    ,		lowerCAmelCase__       :						Dict="gelu"    ,		lowerCAmelCase__       :						List[str]=0.1    ,		lowerCAmelCase__       :						str=0.1    ,		lowerCAmelCase__       :						List[str]=0.1    ,		lowerCAmelCase__       :						int=True    ,		lowerCAmelCase__       :						Tuple=0.0    ,		lowerCAmelCase__       :						Union[str, Any]=0.1    ,		lowerCAmelCase__       :						Dict=0.1    ,		lowerCAmelCase__       :						int=0.02    ,		lowerCAmelCase__       :						Optional[int]=1E-5    ,		lowerCAmelCase__       :						List[str]="group"    ,		lowerCAmelCase__       :						List[str]="gelu"    ,		lowerCAmelCase__       :						Any=(512, 512, 512, 512, 512, 512, 512)    ,		lowerCAmelCase__       :						str=(5, 2, 2, 2, 2, 2, 2)    ,		lowerCAmelCase__       :						Union[str, Any]=(10, 3, 3, 3, 3, 2, 2)    ,		lowerCAmelCase__       :						Optional[Any]=False    ,		lowerCAmelCase__       :						Dict=128    ,		lowerCAmelCase__       :						List[Any]=16    ,		lowerCAmelCase__       :						Optional[Any]=False    ,		lowerCAmelCase__       :						Optional[int]=True    ,		lowerCAmelCase__       :						List[Any]=0.05    ,		lowerCAmelCase__       :						Any=10    ,		lowerCAmelCase__       :						Dict=2    ,		lowerCAmelCase__       :						Dict=0.0    ,		lowerCAmelCase__       :						Tuple=10    ,		lowerCAmelCase__       :						List[str]=0    ,		lowerCAmelCase__       :						int="sum"    ,		lowerCAmelCase__       :						Optional[Any]=False    ,		lowerCAmelCase__       :						List[Any]=False    ,		lowerCAmelCase__       :						int=256    ,		lowerCAmelCase__       :						List[Any]=0    ,		lowerCAmelCase__       :						Union[str, Any]=1    ,		lowerCAmelCase__       :						Tuple=2    ,		**lowerCAmelCase__       :						str    ,		)			->    Union[str, Any]:
             super().__init__(**lowerCAmelCase__    ,		pad_token_id=lowerCAmelCase__    ,		bos_token_id=lowerCAmelCase__    ,		eos_token_id=lowerCAmelCase__							)
             snake_case__					=   hidden_size
             snake_case__					=   feat_extract_norm
             snake_case__					=   feat_extract_activation
             snake_case__					=   list(lowerCAmelCase__							)
             snake_case__					=   list(lowerCAmelCase__							)
             snake_case__					=   list(lowerCAmelCase__							)
             snake_case__					=   conv_bias
             snake_case__					=   num_conv_pos_embeddings
             snake_case__					=   num_conv_pos_embedding_groups
             snake_case__					=   len(self.conv_dim							)
             snake_case__					=   num_hidden_layers
             snake_case__					=   intermediate_size
             snake_case__					=   hidden_act
             snake_case__					=   num_attention_heads
             snake_case__					=   hidden_dropout
             snake_case__					=   attention_dropout
             snake_case__					=   activation_dropout
             snake_case__					=   feat_proj_layer_norm
             snake_case__					=   feat_proj_dropout
             snake_case__					=   final_dropout
             snake_case__					=   layerdrop
             snake_case__					=   layer_norm_eps
             snake_case__					=   initializer_range
             snake_case__					=   vocab_size
             snake_case__					=   do_stable_layer_norm
             snake_case__					=   use_weighted_layer_sum
             snake_case__					=   classifier_proj_size

             if (
                 (len(self.conv_stride							) != self.num_feat_extract_layers)
                 or (len(self.conv_kernel							) != self.num_feat_extract_layers)
                 or (len(self.conv_dim							) != self.num_feat_extract_layers)
             ):
                   raise ValueError(
                       """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
                       """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
                       f''' {len(self.conv_dim							)}`, `len(config.conv_stride) = {len(self.conv_stride							)}`,'''
                       f''' `len(config.conv_kernel) = {len(self.conv_kernel							)}`.'''							)

             # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
             snake_case__					=   apply_spec_augment
             snake_case__					=   mask_time_prob
             snake_case__					=   mask_time_length
             snake_case__					=   mask_time_min_masks
             snake_case__					=   mask_feature_prob
             snake_case__					=   mask_feature_length
             snake_case__					=   mask_feature_min_masks

             # ctc loss
             snake_case__					=   ctc_loss_reduction
             snake_case__					=   ctc_zero_infinity

       @property
       def UpperCAmelCase_						(						self       :						Union[str, Any]							)			->    Tuple:
             return functools.reduce(operator.mul    ,		self.conv_stride    ,		1							)






