




'''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')

lowerCAmelCase_			:     List[Any]							    =     'K-MEANS-CLUST/ '



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Dict   ,						UpperCamelCase__						:				str   ,						UpperCamelCase__						:				int=None							):




     """simple docstring"""


     if seed is not None:  # useful for obtaining consistent results
          np.random.seed(UpperCamelCase__							)
     a_    :       Dict    	=     data.shape[0]  # number of data points

     # Pick K indices from range [0, N).
     a_    :       List[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.
     a_    :       int    	=     data[rand_indices, :]

     return centroids



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				List[Any]   ,						UpperCamelCase__						:				List[Any]							):




     """simple docstring"""


     return pairwise_distances(UpperCamelCase__   ,						UpperCamelCase__   ,						metric="""euclidean"""							)



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Optional[int]   ,						UpperCamelCase__						:				List[str]							):




     """simple docstring"""


     a_    :       Optional[Any]    	=     centroid_pairwise_dist(UpperCamelCase__   ,						UpperCamelCase__							)

     # Compute cluster assignments for each data point:
     # Fill in the blank (RHS only)
     a_    :       Optional[int]    	=     np.argmin(UpperCamelCase__   ,						axis=1							)

     return cluster_assignment



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Optional[int]   ,						UpperCamelCase__						:				Tuple   ,						UpperCamelCase__						:				Tuple							):




     """simple docstring"""


     a_    :       Union[str, Any]    	=     []
     for i in range(UpperCamelCase__							):
          # Select all data points that belong to cluster i. Fill in the blank (RHS only)
          a_    :       List[str]    	=     data[cluster_assignment == i]
          # Compute the mean of the data points. Fill in the blank (RHS only)
          a_    :       int    	=     member_data_points.mean(axis=0							)
          new_centroids.append(UpperCamelCase__							)
     a_    :       List[Any]    	=     np.array(UpperCamelCase__							)

     return new_centroids



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Optional[int]   ,						UpperCamelCase__						:				Dict   ,						UpperCamelCase__						:				Any   ,						UpperCamelCase__						:				int							):




     """simple docstring"""


     a_    :       Optional[int]    	=     0.0
     for i in range(UpperCamelCase__							):
          # Select all data points that belong to cluster i. Fill in the blank (RHS only)
          a_    :       Any    	=     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_    :       Optional[int]    	=     pairwise_distances(
                   UpperCamelCase__   ,						[centroids[i]]   ,						metric="""euclidean"""							)
               a_    :       str    	=     distances**2
               heterogeneity += np.sum(UpperCamelCase__							)

     return heterogeneity



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Optional[Any]   ,						UpperCamelCase__						:				int							):




     """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    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Any   ,						UpperCamelCase__						:				Union[str, Any]   ,						UpperCamelCase__						:				List[str]   ,						UpperCamelCase__						:				Optional[Any]=500   ,						UpperCamelCase__						:				Dict=None   ,						UpperCamelCase__						:				int=False							):




     """simple docstring"""


     a_    :       List[str]    	=     initial_centroids[:]
     a_    :       str    	=     None

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

          # 1. Make cluster assignments using nearest centroids
          a_    :       str    	=     assign_clusters(UpperCamelCase__   ,						UpperCamelCase__							)

          # 2. Compute a new centroid for each of the k clusters, averaging all data
          #    points assigned to that cluster.
          a_    :       Union[str, Any]    	=     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_    :       Optional[Any]    	=     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_    :       List[Any]    	=     compute_heterogeneity(UpperCamelCase__   ,						UpperCamelCase__   ,						UpperCamelCase__   ,						UpperCamelCase__							)
               record_heterogeneity.append(UpperCamelCase__							)

          a_    :       str    	=     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_			:     Optional[Any]							    =     ds.load_iris()
 lowerCAmelCase_			:     Dict							    =     3
 lowerCAmelCase_			:     Dict							    =     []
 lowerCAmelCase_			:     Any							    =     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    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				pd.DataFrame   ,						UpperCamelCase__						:				np.ndarray   ,						UpperCamelCase__						:				List[Any]=None							):




     """simple docstring"""


     if fill_missing_report:
          a_    :       Optional[Any]    	=     df.fillna(value=UpperCamelCase__							)
     a_    :       Dict    	=     1
     a_    :       Dict    	=     df.select_dtypes(np.number							).columns
     a_    :       List[str]    	=     (
         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_    :       Union[str, Any]    	=     report[
         (report["""Features"""] == """dummy""") & (report["""Type"""] == """count""")
     ].copy()  # avoid SettingWithCopyWarning
     a_    :       int    	=     (
         """ClusterSize"""  # rename created cluster df to match report column names
     )
     a_    :       Tuple    	=     """# of Customers"""
     a_    :       str    	=     pd.DataFrame(
         clustersize.iloc[:, 2:].values
         / clustersize.iloc[:, 2:].values.sum()							)
     a_    :       Dict    	=     """% of Customers"""  # rename created cluster df to match report column names
     a_    :       List[Any]    	=     """ClusterProportion"""
     a_    :       Dict    	=     clusterproportion.columns.tolist()
     a_    :       Dict    	=     cols[-2:] + cols[:-2]
     a_    :       Any    	=     clusterproportion[cols]  # rearrange columns to match report
     a_    :       int    	=     report.columns
     a_    :       Optional[Any]    	=     pd.DataFrame(
         abs(
             report[report["""Type"""] == """count"""].iloc[:, 2:].values
             - clustersize.iloc[:, 2:].values							)							)  # generating df with count of nan values
     a_    :       Optional[Any]    	=     0
     a_    :       Union[str, Any]    	=     """# of nan"""
     a_    :       List[Any]    	=     report[
         report["""Type"""] == """count"""
     ].Features.tolist()  # filling values in order to match report
     a_    :       Union[str, Any]    	=     a.columns.tolist()
     a_    :       int    	=     cols[-2:] + cols[:-2]
     a_    :       Dict    	=     a[cols]  # rearrange columns to match report
     a_    :       Tuple    	=     report.columns  # rename columns to match report
     a_    :       str    	=     report.drop(
         report[report.Type == """count"""].index							)  # drop count values except cluster size
     a_    :       Optional[int]    	=     pd.concat(
         [report, a, clustersize, clusterproportion]   ,						axis=0							)  # concat report with clustert size and nan values
     a_    :       Any    	=     report["""Features"""].isin(UpperCamelCase__							)
     a_    :       Optional[int]    	=     report.columns.tolist()
     a_    :       Dict    	=     cols[0:2] + cols[-1:] + cols[2:-1]
     a_    :       List[str]    	=     report[cols]
     a_    :       Optional[Any]    	=     {
         """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_    :       int    	=     (
         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_    :       Optional[Any]    	=     """"""
     a_    :       Optional[Any]    	=     report.reset_index()
     a_    :       Union[str, Any]    	=     report.drop(columns=["""index"""]							)
     return report


if __name__ == "__main__":
 import doctest

 doctest.testmod()






'''simple docstring'''


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import CLIPImageProcessor







class     SCREAMING_SNAKE_CASE						(	unittest.TestCase							):






      def __init__(   self      :     Tuple	,	lowercase__      :     Union[str, Any]	,	lowercase__      :     Optional[Any]=7	,	lowercase__      :     str=3	,	lowercase__      :     Tuple=18	,	lowercase__      :     Dict=30	,	lowercase__      :     List[Any]=400	,	lowercase__      :     Optional[Any]=True	,	lowercase__      :     Union[str, Any]=None	,	lowercase__      :     Union[str, Any]=True	,	lowercase__      :     Tuple=None	,	lowercase__      :     str=True	,	lowercase__      :     List[Any]=[0.48145466, 0.4578275, 0.40821073]	,	lowercase__      :     Optional[Any]=[0.26862954, 0.26130258, 0.27577711]	,	lowercase__      :     str=True	,	):


           '''simple docstring'''

           a_    :       Dict    	=     size if size is not None else {"""shortest_edge""": 20}
           a_    :       Any    	=     crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
           a_    :       List[str]    	=     parent
           a_    :       Dict    	=     batch_size
           a_    :       Any    	=     num_channels
           a_    :       Optional[int]    	=     image_size
           a_    :       Union[str, Any]    	=     min_resolution
           a_    :       Tuple    	=     max_resolution
           a_    :       int    	=     do_resize
           a_    :       List[str]    	=     size
           a_    :       Optional[int]    	=     do_center_crop
           a_    :       str    	=     crop_size
           a_    :       str    	=     do_normalize
           a_    :       Any    	=     image_mean
           a_    :       List[str]    	=     image_std
           a_    :       Any    	=     do_convert_rgb






      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

           return {
               "do_resize": self.do_resize,
               "size": self.size,
               "do_center_crop": self.do_center_crop,
               "crop_size": self.crop_size,
               "do_normalize": self.do_normalize,
               "image_mean": self.image_mean,
               "image_std": self.image_std,
               "do_convert_rgb": self.do_convert_rgb,
           }






      def        lowercase_	(   self      :     str	,	lowercase__      :     Union[str, Any]=False	,	lowercase__      :     Union[str, Any]=False	,	lowercase__      :     Union[str, Any]=False					):


           '''simple docstring'''

           assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

           if equal_resolution:
                a_    :       Union[str, Any]    	=     []
                for i in range(self.batch_size					):
                     image_inputs.append(
                         np.random.randint(
                             255	,	size=(self.num_channels, self.max_resolution, self.max_resolution)	,	dtype=np.uinta					)					)
           else:
                a_    :       int    	=     []
                for i in range(self.batch_size					):
                     a_	,    a_    :       Any    	=     np.random.choice(np.arange(self.min_resolution	,	self.max_resolution					)	,	2					)
                     image_inputs.append(np.random.randint(255	,	size=(self.num_channels, width, height)	,	dtype=np.uinta					)					)

           if not numpify and not torchify:
                # PIL expects the channel dimension as last dimension
                a_    :       Union[str, Any]    	=     [Image.fromarray(np.moveaxis(lowercase__	,	0	,	-1					)					) for x in image_inputs]

           if torchify:
                a_    :       List[Any]    	=     [torch.from_numpy(lowercase__					) for x in image_inputs]

           return image_inputs







@require_torch
@require_vision
class     SCREAMING_SNAKE_CASE						(	snake_case_       ,			unittest.TestCase							):
      __magic_name__     :					int             =				CLIPImageProcessor if is_vision_available() else None






      def        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           a_    :       Dict    	=     CLIPImageProcessingTester(self					)






      @property
      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

           return self.image_processor_tester.prepare_image_processor_dict()






      def        lowercase_	(   self      :     Optional[Any]					):


           '''simple docstring'''

           a_    :       List[Any]    	=     self.image_processing_class(**self.image_processor_dict					)
           self.assertTrue(hasattr(lowercase__	,	"""do_resize"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""size"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_center_crop"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""center_crop"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_normalize"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""image_mean"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""image_std"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_convert_rgb"""					)					)






      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

           a_    :       Optional[Any]    	=     self.image_processing_class.from_dict(self.image_processor_dict					)
           self.assertEqual(image_processor.size	,	{"""shortest_edge""": 20}					)
           self.assertEqual(image_processor.crop_size	,	{"""height""": 18, """width""": 18}					)

           a_    :       Optional[int]    	=     self.image_processing_class.from_dict(self.image_processor_dict	,	size=42	,	crop_size=84					)
           self.assertEqual(image_processor.size	,	{"""shortest_edge""": 42}					)
           self.assertEqual(image_processor.crop_size	,	{"""height""": 84, """width""": 84}					)






      def        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           pass






      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           a_    :       str    	=     self.image_processing_class(**self.image_processor_dict					)
           # create random PIL images
           a_    :       str    	=     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__					)
           for image in image_inputs:
                self.assertIsInstance(lowercase__	,	Image.Image					)

           # Test not batched input
           a_    :       Optional[Any]    	=     image_processing(image_inputs[0]	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)

           # Test batched
           a_    :       str    	=     image_processing(lowercase__	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)






      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           a_    :       Optional[Any]    	=     self.image_processing_class(**self.image_processor_dict					)
           # create random numpy tensors
           a_    :       Optional[int]    	=     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__	,	numpify=lowercase__					)
           for image in image_inputs:
                self.assertIsInstance(lowercase__	,	np.ndarray					)

           # Test not batched input
           a_    :       Tuple    	=     image_processing(image_inputs[0]	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)

           # Test batched
           a_    :       str    	=     image_processing(lowercase__	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)






      def        lowercase_	(   self      :     str					):


           '''simple docstring'''

           a_    :       Dict    	=     self.image_processing_class(**self.image_processor_dict					)
           # create random PyTorch tensors
           a_    :       Optional[int]    	=     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__	,	torchify=lowercase__					)
           for image in image_inputs:
                self.assertIsInstance(lowercase__	,	torch.Tensor					)

           # Test not batched input
           a_    :       Optional[int]    	=     image_processing(image_inputs[0]	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)

           # Test batched
           a_    :       int    	=     image_processing(lowercase__	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)







@require_torch
@require_vision
class     SCREAMING_SNAKE_CASE						(	snake_case_       ,			unittest.TestCase							):
      __magic_name__     :					List[str]             =				CLIPImageProcessor if is_vision_available() else None






      def        lowercase_	(   self      :     str					):


           '''simple docstring'''

           a_    :       str    	=     CLIPImageProcessingTester(self	,	num_channels=4					)
           a_    :       Dict    	=     3






      @property
      def        lowercase_	(   self      :     Any					):


           '''simple docstring'''

           return self.image_processor_tester.prepare_image_processor_dict()






      def        lowercase_	(   self      :     List[Any]					):


           '''simple docstring'''

           a_    :       int    	=     self.image_processing_class(**self.image_processor_dict					)
           self.assertTrue(hasattr(lowercase__	,	"""do_resize"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""size"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_center_crop"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""center_crop"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_normalize"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""image_mean"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""image_std"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_convert_rgb"""					)					)






      def        lowercase_	(   self      :     int					):


           '''simple docstring'''

           pass






      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           a_    :       List[Any]    	=     self.image_processing_class(**self.image_processor_dict					)
           # create random PIL images
           a_    :       Union[str, Any]    	=     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__					)
           for image in image_inputs:
                self.assertIsInstance(lowercase__	,	Image.Image					)

           # Test not batched input
           a_    :       Optional[Any]    	=     image_processing(image_inputs[0]	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   1,
                   self.expected_encoded_image_num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)

           # Test batched
           a_    :       List[str]    	=     image_processing(lowercase__	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   self.image_processor_tester.batch_size,
                   self.expected_encoded_image_num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)
