code: 
  
  
  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""")
  
  A_    :					Tuple             ="""K-MEANS-CLUST/ """
  
  
  
  
  def   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__=None      ):
  
  
  
  						"""simple docstring"""
  
  
  
  
  						if seed is not None:  # useful for obtaining consistent results
  												np.random.seed(UpperCAmelCase__      )
  						a_					=		data.shape[0]  # number of data points
  
  						# Pick K indices from range [0, N).
  						a_					=		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.
  						a_					=		data[rand_indices, :]
  
  						return centroids
  
  
  
  
  def   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__      ):
  
  
  
  						"""simple docstring"""
  
  
  
  
  						return pairwise_distances(UpperCAmelCase__       ,    UpperCAmelCase__       ,    metric="""euclidean"""      )
  
  
  
  
  def   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__      ):
  
  
  
  						"""simple docstring"""
  
  
  
  
  						a_					=		centroid_pairwise_dist(UpperCAmelCase__       ,    UpperCAmelCase__      )
  
  						# Compute cluster assignments for each data point:
  						# Fill in the blank (RHS only)
  						a_					=		np.argmin(UpperCAmelCase__       ,    axis=1      )
  
  						return cluster_assignment
  
  
  
  
  def   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__      ):
  
  
  
  						"""simple docstring"""
  
  
  
  
  						a_					=		[]
  						for i in range(UpperCAmelCase__      ):
  												# Select all data points that belong to cluster i. Fill in the blank (RHS only)
  												a_					=		data[cluster_assignment == i]
  												# Compute the mean of the data points. Fill in the blank (RHS only)
  												a_					=		member_data_points.mean(axis=0      )
  												new_centroids.append(UpperCAmelCase__      )
  						a_					=		np.array(UpperCAmelCase__      )
  
  						return new_centroids
  
  
  
  
  def   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__      ):
  
  
  
  						"""simple docstring"""
  
  
  
  
  						a_					=		0.0
  						for i in range(UpperCAmelCase__      ):
  												# Select all data points that belong to cluster i. Fill in the blank (RHS only)
  												a_					=		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)
  																		a_					=		pairwise_distances(
  																		    UpperCAmelCase__       ,    [centroids[i]]       ,    metric="""euclidean"""      )
  																		a_					=		distances**2
  																		heterogeneity += np.sum(UpperCAmelCase__      )
  
  						return heterogeneity
  
  
  
  
  def   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__      ):
  
  
  
  						"""simple docstring"""
  
  
  
  
  						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   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__=500       ,    UpperCAmelCase__=None       ,    UpperCAmelCase__=False      ):
  
  
  
  						"""simple docstring"""
  
  
  
  
  						a_					=		initial_centroids[:]
  						a_					=		None
  
  						for itr in range(UpperCAmelCase__      ):
  												if verbose:
  																		print(UpperCAmelCase__       ,    end=""""""      )
  
  												# 1. Make cluster assignments using nearest centroids
  												a_					=		assign_clusters(UpperCAmelCase__       ,    UpperCAmelCase__      )
  
  												# 2. Compute a new centroid for each of the k clusters, averaging all data
  												#    points assigned to that cluster.
  												a_					=		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:
  																		a_					=		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
  																		a_					=		compute_heterogeneity(UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__      )
  																		record_heterogeneity.append(UpperCAmelCase__      )
  
  												a_					=		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
  
  	A_    :					Dict             =ds.load_iris()
  	A_    :					Optional[int]             =3
  	A_    :					Optional[Any]             =[]
  	A_    :					Any             =get_initial_centroids(dataset["""data"""], k, seed=0)
  	A_       ,	A_    :					Any             =kmeans(
  	    dataset["""data"""],
  	    k,
  	    initial_centroids,
  	    maxiter=400,
  	    record_heterogeneity=heterogeneity,
  	    verbose=True,
  	)
  	plot_heterogeneity(heterogeneity, k)
  
  
  
  
  def   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__=None      ):
  
  
  
  						"""simple docstring"""
  
  
  
  
  						if fill_missing_report:
  												a_					=		df.fillna(value=UpperCAmelCase__      )
  						a_					=		1
  						a_					=		df.select_dtypes(np.number      ).columns
  						a_					=		(
  						    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.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)
  						a_					=		report[
  						    (report["""Features"""] == """dummy""") & (report["""Type"""] == """count""")
  						].copy()  # avoid SettingWithCopyWarning
  						a_					=		(
  						    """ClusterSize"""  # rename created cluster df to match report column names
  						)
  						a_					=		"""# of Customers"""
  						a_					=		pd.DataFrame(
  						    clustersize.iloc[:, 2:].values
  						    / clustersize.iloc[:, 2:].values.sum()      )
  						a_					=		"""% of Customers"""  # rename created cluster df to match report column names
  						a_					=		"""ClusterProportion"""
  						a_					=		clusterproportion.columns.tolist()
  						a_					=		cols[-2:] + cols[:-2]
  						a_					=		clusterproportion[cols]  # rearrange columns to match report
  						a_					=		report.columns
  						a_					=		pd.DataFrame(
  						    abs(
  						        report[report["""Type"""] == """count"""].iloc[:, 2:].values
  						        - clustersize.iloc[:, 2:].values      )      )  # generating df with count of nan values
  						a_					=		0
  						a_					=		"""# of nan"""
  						a_					=		report[
  						    report["""Type"""] == """count"""
  						].Features.tolist()  # filling values in order to match report
  						a_					=		a.columns.tolist()
  						a_					=		cols[-2:] + cols[:-2]
  						a_					=		a[cols]  # rearrange columns to match report
  						a_					=		report.columns  # rename columns to match report
  						a_					=		report.drop(
  						    report[report.Type == """count"""].index      )  # drop count values except cluster size
  						a_					=		pd.concat(
  						    [report, a, clustersize, clusterproportion]       ,    axis=0      )  # concat report with clustert size and nan values
  						a_					=		report["""Features"""].isin(UpperCAmelCase__      )
  						a_					=		report.columns.tolist()
  						a_					=		cols[0:2] + cols[-1:] + cols[2:-1]
  						a_					=		report[cols]
  						a_					=		{
  						    """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,
  						}
  						a_					=		(
  						    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      )
  						)
  						a_					=		""""""
  						a_					=		report.reset_index()
  						a_					=		report.drop(columns=["""index"""]      )
  						return report
  
  
  if __name__ == "__main__":
  	import doctest
  
  	doctest.testmod()
style_context: 
  
  
  import json
  import os
  import unittest
  
  from transformers import BartTokenizer, BartTokenizerFast, BatchEncoding
  from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES
  from transformers.testing_utils import require_tokenizers, require_torch
  from transformers.utils import cached_property
  
  from ...test_tokenization_common import TokenizerTesterMixin, filter_roberta_detectors
  
  
  
  
  
  @require_tokenizers
  class        lowercase_				( UpperCamelCase__			,unittest.TestCase):
  
  
  
  
  
  
  							"""simple docstring"""
  
  
  
  
  
  
  
  							snake_case_        =		BartTokenizer
  							snake_case_        =		BartTokenizerFast
  							snake_case_        =		True
  							snake_case_        =		filter_roberta_detectors
  
  
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													super().setUp()
  													a_					=		[
  													    """l""",
  													    """o""",
  													    """w""",
  													    """e""",
  													    """r""",
  													    """s""",
  													    """t""",
  													    """i""",
  													    """d""",
  													    """n""",
  													    """\u0120""",
  													    """\u0120l""",
  													    """\u0120n""",
  													    """\u0120lo""",
  													    """\u0120low""",
  													    """er""",
  													    """\u0120lowest""",
  													    """\u0120newer""",
  													    """\u0120wider""",
  													    """<unk>""",
  													]
  													a_					=		dict(zip(_UpperCAmelCase			,    range(len(_UpperCAmelCase		)		)		)		)
  													a_					=		["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""]
  													a_					=		{"""unk_token""": """<unk>"""}
  
  													a_					=		os.path.join(self.tmpdirname			,    VOCAB_FILES_NAMES["""vocab_file"""]		)
  													a_					=		os.path.join(self.tmpdirname			,    VOCAB_FILES_NAMES["""merges_file"""]		)
  													with open(self.vocab_file			,    """w"""			,    encoding="""utf-8"""		) as fp:
  																			fp.write(json.dumps(_UpperCAmelCase		) + """\n"""		)
  													with open(self.merges_file			,    """w"""			,    encoding="""utf-8"""		) as fp:
  																			fp.write("""\n""".join(_UpperCAmelCase		)		)
  
  
  							def 						lowercase__     (   self			,    **_UpperCAmelCase		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													kwargs.update(self.special_tokens_map		)
  													return self.tokenizer_class.from_pretrained(self.tmpdirname			,    **_UpperCAmelCase		)
  
  
  							def 						lowercase__     (   self			,    **_UpperCAmelCase		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													kwargs.update(self.special_tokens_map		)
  													return self.rust_tokenizer_class.from_pretrained(self.tmpdirname			,    **_UpperCAmelCase		)
  
  
  							def 						lowercase__     (   self			,    _UpperCAmelCase		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													return "lower newer", "lower newer"
  
  
  							@cached_property
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													return BartTokenizer.from_pretrained("""facebook/bart-large"""		)
  
  
  							@cached_property
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													return BartTokenizerFast.from_pretrained("""facebook/bart-large"""		)
  
  
  							@require_torch
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													a_					=		["""A long paragraph for summarization.""", """Another paragraph for summarization."""]
  													a_					=		[0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2]
  
  													for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  																			a_					=		tokenizer(_UpperCAmelCase			,    max_length=len(_UpperCAmelCase		)			,    padding=_UpperCAmelCase			,    return_tensors="""pt"""		)
  																			self.assertIsInstance(_UpperCAmelCase			,    _UpperCAmelCase		)
  
  																			self.assertEqual((2, 9)			,    batch.input_ids.shape		)
  																			self.assertEqual((2, 9)			,    batch.attention_mask.shape		)
  																			a_					=		batch.input_ids.tolist()[0]
  																			self.assertListEqual(_UpperCAmelCase			,    _UpperCAmelCase		)
  																			# Test that special tokens are reset
  
  
  							@require_torch
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													a_					=		["""A long paragraph for summarization.""", """Another paragraph for summarization."""]
  													for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  																			a_					=		tokenizer(_UpperCAmelCase			,    padding=_UpperCAmelCase			,    return_tensors="""pt"""		)
  																			# check if input_ids are returned and no labels
  																			self.assertIn("""input_ids"""			,    _UpperCAmelCase		)
  																			self.assertIn("""attention_mask"""			,    _UpperCAmelCase		)
  																			self.assertNotIn("""labels"""			,    _UpperCAmelCase		)
  																			self.assertNotIn("""decoder_attention_mask"""			,    _UpperCAmelCase		)
  
  
  							@require_torch
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													a_					=		[
  													    """Summary of the text.""",
  													    """Another summary.""",
  													]
  													for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  																			a_					=		tokenizer(text_target=_UpperCAmelCase			,    max_length=32			,    padding="""max_length"""			,    return_tensors="""pt"""		)
  																			self.assertEqual(32			,    targets["""input_ids"""].shape[1]		)
  
  
  							@require_torch
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  																			a_					=		tokenizer(
  																			    ["""I am a small frog""" * 1_024, """I am a small frog"""]			,    padding=_UpperCAmelCase			,    truncation=_UpperCAmelCase			,    return_tensors="""pt"""		)
  																			self.assertIsInstance(_UpperCAmelCase			,    _UpperCAmelCase		)
  																			self.assertEqual(batch.input_ids.shape			,    (2, 1_024)		)
  
  
  							@require_torch
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													a_					=		["""A long paragraph for summarization."""]
  													a_					=		[
  													    """Summary of the text.""",
  													]
  													for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  																			a_					=		tokenizer(_UpperCAmelCase			,    return_tensors="""pt"""		)
  																			a_					=		tokenizer(text_target=_UpperCAmelCase			,    return_tensors="""pt"""		)
  																			a_					=		inputs["""input_ids"""]
  																			a_					=		targets["""input_ids"""]
  																			self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item()		)
  																			self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item()		)
  																			self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item()		)
  																			self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item()		)
  
  
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													pass
  
  
  
  
  
  
  							def 						lowercase__     (   self		):
  
  
  
  
  
  
  
  													"""simple docstring"""
  
  
  													for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
  																			with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"		):
  																									a_					=		self.rust_tokenizer_class.from_pretrained(_UpperCAmelCase			,    **_UpperCAmelCase		)
  																									a_					=		self.tokenizer_class.from_pretrained(_UpperCAmelCase			,    **_UpperCAmelCase		)
  																									a_					=		"""A, <mask> AllenNLP sentence."""
  																									a_					=		tokenizer_r.encode_plus(_UpperCAmelCase			,    add_special_tokens=_UpperCAmelCase			,    return_token_type_ids=_UpperCAmelCase		)
  																									a_					=		tokenizer_p.encode_plus(_UpperCAmelCase			,    add_special_tokens=_UpperCAmelCase			,    return_token_type_ids=_UpperCAmelCase		)
  
  																									# token_type_ids should put 0 everywhere
  																									self.assertEqual(sum(tokens_r["""token_type_ids"""]		)			,    sum(tokens_p["""token_type_ids"""]		)		)
  
  																									# attention_mask should put 1 everywhere, so sum over length should be 1
  																									self.assertEqual(
  																									    sum(tokens_r["""attention_mask"""]		) / len(tokens_r["""attention_mask"""]		)			,    sum(tokens_p["""attention_mask"""]		) / len(tokens_p["""attention_mask"""]		)			,    )
  
  																									a_					=		tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""]		)
  																									a_					=		tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""]		)
  
  																									# Rust correctly handles the space before the mask while python doesnt
  																									self.assertSequenceEqual(tokens_p["""input_ids"""]			,    [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]		)
  																									self.assertSequenceEqual(tokens_r["""input_ids"""]			,    [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]		)
  
  																									self.assertSequenceEqual(
  																									    _UpperCAmelCase			,    ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""]		)
  																									self.assertSequenceEqual(
  																									    _UpperCAmelCase			,    ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""]		)