code: 
  '''simple docstring'''
  
  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""")
  
  _UpperCAmelCase   :      List[str] 						=  """K-MEANS-CLUST/ """
  
  
  
  
  
  
  
  def 						snake_case__	(      UpperCamelCase     :   Any			,UpperCamelCase     :   List[str]			,UpperCamelCase     :   Any=None	) ->      Optional[int]:
  
  
  
  
  
  
  
  		if seed is not None:  # useful for obtaining consistent results
  				np.random.seed(UpperCamelCase	)
  		_UpperCamelCase       : List[Any]    =      data.shape[0]  # number of data points
  
  		# Pick K indices from range [0, N).
  		_UpperCamelCase       : str    =      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.
  		_UpperCamelCase       : Tuple    =      data[rand_indices, :]
  
  		return centroids
  
  
  
  
  
  
  
  def 						snake_case__	(      UpperCamelCase     :   int			,UpperCamelCase     :   Optional[int]	) ->      List[Any]:
  		return pairwise_distances(UpperCamelCase			,UpperCamelCase			,metric='''euclidean'''	)
  
  
  
  
  
  
  
  def 						snake_case__	(      UpperCamelCase     :   Optional[int]			,UpperCamelCase     :   str	) ->      Any:
  		_UpperCamelCase       : List[str]    =      centroid_pairwise_dist(UpperCamelCase			,UpperCamelCase	)
  
  		# Compute cluster assignments for each data point:
  		# Fill in the blank (RHS only)
  		_UpperCamelCase       : Optional[int]    =      np.argmin(UpperCamelCase			,axis=1	)
  
  		return cluster_assignment
  
  
  
  
  
  
  
  def 						snake_case__	(      UpperCamelCase     :   Optional[Any]			,UpperCamelCase     :   str			,UpperCamelCase     :   List[str]	) ->      str:
  		_UpperCamelCase       : List[Any]    =      []
  		for i in range(UpperCamelCase	):
  				# Select all data points that belong to cluster i. Fill in the blank (RHS only)
  				_UpperCamelCase       : Dict    =      data[cluster_assignment == i]
  				# Compute the mean of the data points. Fill in the blank (RHS only)
  				_UpperCamelCase       : Optional[int]    =      member_data_points.mean(axis=0	)
  				new_centroids.append(UpperCamelCase	)
  		_UpperCamelCase       : int    =      np.array(UpperCamelCase	)
  
  		return new_centroids
  
  
  
  
  
  
  
  def 						snake_case__	(      UpperCamelCase     :   Any			,UpperCamelCase     :   int			,UpperCamelCase     :   Optional[Any]			,UpperCamelCase     :   Union[str, Any]	) ->      Optional[int]:
  		_UpperCamelCase       : Union[str, Any]    =      0.0
  		for i in range(UpperCamelCase	):
  				# Select all data points that belong to cluster i. Fill in the blank (RHS only)
  				_UpperCamelCase       : int    =      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)
  						_UpperCamelCase       : List[str]    =      pairwise_distances(
  						    UpperCamelCase			,[centroids[i]]			,metric='''euclidean'''	)
  						_UpperCamelCase       : str    =      distances**2
  						heterogeneity += np.sum(UpperCamelCase	)
  
  		return heterogeneity
  
  
  
  
  
  
  
  def 						snake_case__	(      UpperCamelCase     :   List[str]			,UpperCamelCase     :   Any	) ->      int:
  		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 						snake_case__	(      UpperCamelCase     :   Optional[Any]			,UpperCamelCase     :   List[Any]			,UpperCamelCase     :   List[str]			,UpperCamelCase     :   int=5_00			,UpperCamelCase     :   int=None			,UpperCamelCase     :   List[Any]=False	) ->      List[str]:
  		_UpperCamelCase       : Optional[int]    =      initial_centroids[:]
  		_UpperCamelCase       : List[Any]    =      None
  
  		for itr in range(UpperCamelCase	):
  				if verbose:
  						print(UpperCamelCase			,end=''''''	)
  
  				# 1. Make cluster assignments using nearest centroids
  				_UpperCamelCase       : Optional[int]    =      assign_clusters(UpperCamelCase			,UpperCamelCase	)
  
  				# 2. Compute a new centroid for each of the k clusters, averaging all data
  				#    points assigned to that cluster.
  				_UpperCamelCase       : List[str]    =      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:
  						_UpperCamelCase       : int    =      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
  						_UpperCamelCase       : Dict    =      compute_heterogeneity(UpperCamelCase			,UpperCamelCase			,UpperCamelCase			,UpperCamelCase	)
  						record_heterogeneity.append(UpperCamelCase	)
  
  				_UpperCamelCase       : Tuple    =      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
  
  	_UpperCAmelCase   :      str 						=  ds.load_iris()
  	_UpperCAmelCase   :      List[str] 						=  3
  	_UpperCAmelCase   :      Any 						=  []
  	_UpperCAmelCase   :      int 						=  get_initial_centroids(dataset["""data"""], k, seed=0)
  	_UpperCAmelCase						,							_UpperCAmelCase   :      Union[str, Any] 						=  kmeans(
  	    dataset["""data"""],
  	    k,
  	    initial_centroids,
  	    maxiter=400,
  	    record_heterogeneity=heterogeneity,
  	    verbose=True,
  	)
  	plot_heterogeneity(heterogeneity, k)
  
  
  
  
  
  
  
  def 						snake_case__	(      UpperCamelCase     :   List[Any]			,UpperCamelCase     :   Tuple			,UpperCamelCase     :   Tuple=None	) ->      List[Any]:
  
  
  
  
  
  
  
  		if fill_missing_report:
  				_UpperCamelCase       : Union[str, Any]    =      df.fillna(value=UpperCamelCase	)
  		_UpperCamelCase       : Any    =      1
  		_UpperCamelCase       : str    =      df.select_dtypes(np.number	).columns
  		_UpperCamelCase       : int    =      (
  		    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 / 1_00	) : round(len(UpperCamelCase	) * 75 / 1_00	)
  		                        ]	)	),
  		            ),
  		            ('''mean_with_na''', np.mean),
  		            ('''min''', lambda UpperCamelCase	: x.min()),
  		            ('''5%''', lambda UpperCamelCase	: x.quantile(0.05	)),
  		            ('''25%''', lambda UpperCamelCase	: x.quantile(0.25	)),
  		            ('''50%''', lambda UpperCamelCase	: x.quantile(0.50	)),
  		            ('''75%''', lambda UpperCamelCase	: x.quantile(0.75	)),
  		            ('''95%''', lambda UpperCamelCase	: x.quantile(0.95	)),
  		            ('''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)
  		_UpperCamelCase       : List[str]    =      report[
  		    (report['''Features'''] == '''dummy''') & (report['''Type'''] == '''count''')
  		].copy()  # avoid SettingWithCopyWarning
  		_UpperCamelCase       : Any    =      (
  		    '''ClusterSize'''  # rename created cluster df to match report column names
  		)
  		_UpperCamelCase       : Tuple    =      '''# of Customers'''
  		_UpperCamelCase       : str    =      pd.DataFrame(
  		    clustersize.iloc[:, 2:].values
  		    / clustersize.iloc[:, 2:].values.sum()	)
  		_UpperCamelCase       : Dict    =      '''% of Customers'''  # rename created cluster df to match report column names
  		_UpperCamelCase       : Dict    =      '''ClusterProportion'''
  		_UpperCamelCase       : Union[str, Any]    =      clusterproportion.columns.tolist()
  		_UpperCamelCase       : Tuple    =      cols[-2:] + cols[:-2]
  		_UpperCamelCase       : Tuple    =      clusterproportion[cols]  # rearrange columns to match report
  		_UpperCamelCase       : Dict    =      report.columns
  		_UpperCamelCase       : List[str]    =      pd.DataFrame(
  		    abs(
  		        report[report['''Type'''] == '''count'''].iloc[:, 2:].values
  		        - clustersize.iloc[:, 2:].values	)	)  # generating df with count of nan values
  		_UpperCamelCase       : Union[str, Any]    =      0
  		_UpperCamelCase       : List[str]    =      '''# of nan'''
  		_UpperCamelCase       : Optional[int]    =      report[
  		    report['''Type'''] == '''count'''
  		].Features.tolist()  # filling values in order to match report
  		_UpperCamelCase       : Optional[Any]    =      a.columns.tolist()
  		_UpperCamelCase       : List[Any]    =      cols[-2:] + cols[:-2]
  		_UpperCamelCase       : Dict    =      a[cols]  # rearrange columns to match report
  		_UpperCamelCase       : Union[str, Any]    =      report.columns  # rename columns to match report
  		_UpperCamelCase       : Any    =      report.drop(
  		    report[report.Type == '''count'''].index	)  # drop count values except cluster size
  		_UpperCamelCase       : List[Any]    =      pd.concat(
  		    [report, a, clustersize, clusterproportion]			,axis=0	)  # concat report with clustert size and nan values
  		_UpperCamelCase       : Any    =      report['''Features'''].isin(UpperCamelCase	)
  		_UpperCamelCase       : Dict    =      report.columns.tolist()
  		_UpperCamelCase       : List[str]    =      cols[0:2] + cols[-1:] + cols[2:-1]
  		_UpperCamelCase       : Any    =      report[cols]
  		_UpperCamelCase       : int    =      {
  		    '''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,
  		}
  		_UpperCamelCase       : Any    =      (
  		    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	)
  		)
  		_UpperCamelCase       : Any    =      ''''''
  		_UpperCamelCase       : Tuple    =      report.reset_index()
  		_UpperCamelCase       : Optional[Any]    =      report.drop(columns=['''index''']	)
  		return report
  
  
  if __name__ == "__main__":
  	import doctest
  
  	doctest.testmod()
  
  
  
  
  
  
style_context: 
  '''simple docstring'''
  
  import unittest
  
  from transformers.image_processing_utils import get_size_dict
  
  class 			UpperCAmelCase				(					unittest.TestCase ):
  
  
  
  
  
  		"""simple docstring"""
  
  
  
  
  
  		def _lowercase  (    self     )				->    Dict:
  				# Test a dict with the wrong keys raises an error
  				_UpperCamelCase       : List[Any]    =      {'''wrong_key''': 224}
  				with self.assertRaises(_snake_case     ):
  						get_size_dict(_snake_case     )
  
  				_UpperCamelCase       : Dict    =      {'''height''': 224}
  				with self.assertRaises(_snake_case     ):
  						get_size_dict(_snake_case     )
  
  				_UpperCamelCase       : str    =      {'''width''': 224, '''shortest_edge''': 224}
  				with self.assertRaises(_snake_case     ):
  						get_size_dict(_snake_case     )
  
  				# Test a dict with the correct keys is returned as is
  				_UpperCamelCase       : Any    =      {'''height''': 224, '''width''': 224}
  				_UpperCamelCase       : Optional[int]    =      get_size_dict(_snake_case     )
  				self.assertEqual(_snake_case  ,					_snake_case     )
  
  				_UpperCamelCase       : Optional[Any]    =      {'''shortest_edge''': 224}
  				_UpperCamelCase       : int    =      get_size_dict(_snake_case     )
  				self.assertEqual(_snake_case  ,					{'''shortest_edge''': 224}     )
  
  				_UpperCamelCase       : int    =      {'''longest_edge''': 224, '''shortest_edge''': 224}
  				_UpperCamelCase       : Tuple    =      get_size_dict(_snake_case     )
  				self.assertEqual(_snake_case  ,					{'''longest_edge''': 224, '''shortest_edge''': 224}     )
  
  				# Test a single int value which  represents (size, size)
  				_UpperCamelCase       : Any    =      get_size_dict(224     )
  				self.assertEqual(_snake_case  ,					{'''height''': 224, '''width''': 224}     )
  
  				# Test a single int value which represents the shortest edge
  				_UpperCamelCase       : List[Any]    =      get_size_dict(224  ,					default_to_square=_snake_case     )
  				self.assertEqual(_snake_case  ,					{'''shortest_edge''': 224}     )
  
  				# Test a tuple of ints which represents (height, width)
  				_UpperCamelCase       : Any    =      get_size_dict((150, 200)     )
  				self.assertEqual(_snake_case  ,					{'''height''': 150, '''width''': 200}     )
  
  				# Test a tuple of ints which represents (width, height)
  				_UpperCamelCase       : List[str]    =      get_size_dict((150, 200)  ,					height_width_order=_snake_case     )
  				self.assertEqual(_snake_case  ,					{'''height''': 200, '''width''': 150}     )
  
  				# Test an int representing the shortest edge and max_size which represents the longest edge
  				_UpperCamelCase       : Any    =      get_size_dict(224  ,					max_size=256  ,					default_to_square=_snake_case     )
  				self.assertEqual(_snake_case  ,					{'''shortest_edge''': 224, '''longest_edge''': 256}     )
  
  				# Test int with default_to_square=True and max_size fails
  				with self.assertRaises(_snake_case     ):
  						get_size_dict(224  ,					max_size=256  ,					default_to_square=_snake_case     )
  
  
  
  
  
  