


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

A      :     Tuple										= '''K-MEANS-CLUST/ '''






def       lowerCAmelCase__       (				lowerCamelCase     :			Optional[int]  ,lowerCamelCase     :			List[Any]  ,lowerCamelCase     :			Tuple=None   ):
       if seed is not None:  # useful for obtaining consistent results
              np.random.seed(lowerCamelCase   )
       _A     :	List[str]      =					data.shape[0]  # number of data points

       # Pick K indices from range [0, N).
       _A     :	Any      =					np.random.randint(0  ,lowerCamelCase  ,lowerCamelCase   )

       # 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     :	Any      =					data[rand_indices, :]

       return centroids






def       lowerCAmelCase__       (				lowerCamelCase     :			Union[str, Any]  ,lowerCamelCase     :			Optional[int]   ):
       return pairwise_distances(lowerCamelCase  ,lowerCamelCase  ,metric='euclidean'   )






def       lowerCAmelCase__       (				lowerCamelCase     :			Tuple  ,lowerCamelCase     :			Tuple   ):
       # Compute distances between each data point and the set of centroids:
       # Fill in the blank (RHS only)
       _A     :	List[Any]      =					centroid_pairwise_dist(lowerCamelCase  ,lowerCamelCase   )

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

       return cluster_assignment






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

       return new_centroids






def       lowerCAmelCase__       (				lowerCamelCase     :			Tuple  ,lowerCamelCase     :			Any  ,lowerCamelCase     :			Dict  ,lowerCamelCase     :			Union[str, Any]   ):
       _A     :	Tuple      =					0.0
       for i in range(lowerCamelCase   ):
              # Select all data points that belong to cluster i. Fill in the blank (RHS only)
              _A     :	Optional[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)
                     _A     :	Union[str, Any]      =					pairwise_distances(
                         lowerCamelCase  ,[centroids[i]]  ,metric='euclidean'   )
                     _A     :	Tuple      =					distances**2
                     heterogeneity += np.sum(lowerCamelCase   )

       return heterogeneity






def       lowerCAmelCase__       (				lowerCamelCase     :			List[Any]  ,lowerCamelCase     :			Optional[Any]   ):
       plt.figure(figsize=(7, 4)   )
       plt.plot(lowerCamelCase  ,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__       (				lowerCamelCase     :			str  ,lowerCamelCase     :			Any  ,lowerCamelCase     :			Any  ,lowerCamelCase     :			Tuple=500  ,lowerCamelCase     :			Optional[int]=None  ,lowerCamelCase     :			int=False   ):
       _A     :	Optional[int]      =					initial_centroids[:]
       _A     :	int      =					None

       for itr in range(lowerCamelCase   ):
              if verbose:
                     print(lowerCamelCase  ,end=''   )

              # 1. Make cluster assignments using nearest centroids
              _A     :	Union[str, Any]      =					assign_clusters(lowerCamelCase  ,lowerCamelCase   )

              # 2. Compute a new centroid for each of the k clusters, averaging all data
              #    points assigned to that cluster.
              _A     :	Optional[int]      =					revise_centroids(lowerCamelCase  ,lowerCamelCase  ,lowerCamelCase   )

              # 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     :	List[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     :	Dict      =					compute_heterogeneity(lowerCamelCase  ,lowerCamelCase  ,lowerCamelCase  ,lowerCamelCase   )
                     record_heterogeneity.append(lowerCamelCase   )

              _A     :	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

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






def       lowerCAmelCase__       (				lowerCamelCase     :			pd.DataFrame  ,lowerCamelCase     :			np.ndarray  ,lowerCamelCase     :			Dict=None   ):
       # Fill missing values with given rules
       if fill_missing_report:
              _A     :	List[Any]      =					df.fillna(value=lowerCamelCase   )
       _A     :	Union[str, Any]      =					1
       _A     :	str      =					df.select_dtypes(np.number   ).columns
       _A     :	Dict      =					(
           df.groupby(['Cluster']   )[  # construct report dataframe
               numeric_cols
           ]  # group by cluster number
           .agg(
               [
                   ('sum', np.sum),
                   ('mean_with_zeros', lambda lowerCamelCase   : np.mean(np.nan_to_num(lowerCamelCase   )   )),
                   ('mean_without_zeros', lambda lowerCamelCase   : x.replace(0  ,np.NaN   ).mean()),
                   (
                       'mean_25-75',
                       lambda lowerCamelCase   : np.mean(
                           np.nan_to_num(
                               sorted(lowerCamelCase   )[
                                   round(len(lowerCamelCase   ) * 25 / 100   ) : round(len(lowerCamelCase   ) * 75 / 100   )
                               ]   )   ),
                   ),
                   ('mean_with_na', np.mean),
                   ('min', lambda lowerCamelCase   : x.min()),
                   ('5%', lambda lowerCamelCase   : x.quantile(0.05   )),
                   ('25%', lambda lowerCamelCase   : x.quantile(0.25   )),
                   ('50%', lambda lowerCamelCase   : x.quantile(0.50   )),
                   ('75%', lambda lowerCamelCase   : x.quantile(0.75   )),
                   ('95%', lambda lowerCamelCase   : x.quantile(0.95   )),
                   ('max', lambda lowerCamelCase   : x.max()),
                   ('count', lambda lowerCamelCase   : x.count()),
                   ('stdev', lambda lowerCamelCase   : x.std()),
                   ('mode', lambda lowerCamelCase   : x.mode()[0]),
                   ('median', lambda lowerCamelCase   : x.median()),
                   ('# > 0', lambda lowerCamelCase   : (x > 0).sum()),
               ]   )
           .T.reset_index()
           .rename(index=lowerCamelCase  ,columns={'level_0': 'Features', 'level_1': 'Type'}   )
       )  # rename columns
       # calculate the size of cluster(count of clientID's)
       _A     :	str      =					report[
           (report['Features'] == 'dummy') & (report['Type'] == 'count')
       ].copy()  # avoid SettingWithCopyWarning
       _A     :	List[str]      =					(
           'ClusterSize'  # rename created cluster df to match report column names
       )
       _A     :	Optional[Any]      =					'# of Customers'
       _A     :	Union[str, Any]      =					pd.DataFrame(
           clustersize.iloc[:, 2:].values
           / clustersize.iloc[:, 2:].values.sum()   )
       _A     :	str      =					'% of Customers'  # rename created cluster df to match report column names
       _A     :	List[str]      =					'ClusterProportion'
       _A     :	Union[str, Any]      =					clusterproportion.columns.tolist()
       _A     :	Union[str, Any]      =					cols[-2:] + cols[:-2]
       _A     :	int      =					clusterproportion[cols]  # rearrange columns to match report
       _A     :	int      =					report.columns
       _A     :	int      =					pd.DataFrame(
           abs(
               report[report['Type'] == 'count'].iloc[:, 2:].values
               - clustersize.iloc[:, 2:].values   )   )  # generating df with count of nan values
       _A     :	Dict      =					0
       _A     :	Any      =					'# of nan'
       _A     :	Tuple      =					report[
           report['Type'] == 'count'
       ].Features.tolist()  # filling values in order to match report
       _A     :	List[Any]      =					a.columns.tolist()
       _A     :	Dict      =					cols[-2:] + cols[:-2]
       _A     :	int      =					a[cols]  # rearrange columns to match report
       _A     :	Optional[int]      =					report.columns  # rename columns to match report
       _A     :	Tuple      =					report.drop(
           report[report.Type == 'count'].index   )  # drop count values except cluster size
       _A     :	Dict      =					pd.concat(
           [report, a, clustersize, clusterproportion]  ,axis=0   )  # concat report with clustert size and nan values
       _A     :	Any      =					report['Features'].isin(lowerCamelCase   )
       _A     :	str      =					report.columns.tolist()
       _A     :	Dict      =					cols[0:2] + cols[-1:] + cols[2:-1]
       _A     :	Optional[Any]      =					report[cols]
       _A     :	Optional[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,
       }
       _A     :	Optional[Any]      =					(
           report.assign(
               Sortera=lambda lowerCamelCase   : x.Type.map(lowerCamelCase   )  ,Sortera=lambda lowerCamelCase   : list(reversed(range(len(lowerCamelCase   )   )   )   )  ,)
           .sort_values(['Sorter1', 'Mark', 'Sorter2']  ,ascending=lowerCamelCase   )
           .drop(['Sorter1', 'Sorter2']  ,axis=1   )
       )
       _A     :	List[Any]      =					''
       _A     :	str      =					report.reset_index()
       _A     :	str      =					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  __lowerCamelCase (       unittest.TestCase       ):
    """simple docstring"""


    def __init__(  self			:							Union[str, Any]      ,							SCREAMING_SNAKE_CASE			:							Any      ,							SCREAMING_SNAKE_CASE			:							List[str]=7      ,							SCREAMING_SNAKE_CASE			:							List[Any]=3      ,							SCREAMING_SNAKE_CASE			:							Dict=18      ,							SCREAMING_SNAKE_CASE			:							List[str]=30      ,							SCREAMING_SNAKE_CASE			:							List[Any]=400      ,							SCREAMING_SNAKE_CASE			:							int=True      ,							SCREAMING_SNAKE_CASE			:							Any=None      ,							SCREAMING_SNAKE_CASE			:							str=True      ,							SCREAMING_SNAKE_CASE			:							List[Any]=None      ,							SCREAMING_SNAKE_CASE			:							List[str]=True      ,							SCREAMING_SNAKE_CASE			:							Optional[int]=[0.4814_5466, 0.457_8275, 0.4082_1073]      ,							SCREAMING_SNAKE_CASE			:							Optional[Any]=[0.2686_2954, 0.2613_0258, 0.2757_7711]      ,							SCREAMING_SNAKE_CASE			:							Union[str, Any]=True      ,							):
           _A     :	List[Any]      =					size if size is not None else {'shortest_edge': 20}
           _A     :	List[Any]      =					crop_size if crop_size is not None else {'height': 18, 'width': 18}
           _A     :	Union[str, Any]      =					parent
           _A     :	Dict      =					batch_size
           _A     :	str      =					num_channels
           _A     :	int      =					image_size
           _A     :	Dict      =					min_resolution
           _A     :	Optional[Any]      =					max_resolution
           _A     :	int      =					do_resize
           _A     :	List[Any]      =					size
           _A     :	Any      =					do_center_crop
           _A     :	List[str]      =					crop_size
           _A     :	List[str]      =					do_normalize
           _A     :	Optional[int]      =					image_mean
           _A     :	Dict      =					image_std
           _A     :	Any      =					do_convert_rgb


    def    A    (  self			:							Tuple):
           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    A    (  self			:							Any      ,							SCREAMING_SNAKE_CASE			:							str=False      ,							SCREAMING_SNAKE_CASE			:							str=False      ,							SCREAMING_SNAKE_CASE			:							List[Any]=False):
           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     :	Union[str, Any]      =					[]
                  for i in range(self.batch_size):
                         _A  , _A     :	Optional[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     :	List[str]      =					[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE      ,							0      ,							-1)) for x in image_inputs]

           if torchify:
                  _A     :	Optional[Any]      =					[torch.from_numpy(SCREAMING_SNAKE_CASE) for x in image_inputs]

           return image_inputs




@require_torch
@require_vision
class  __lowerCamelCase (       a_				,					unittest.TestCase       ):
    """simple docstring"""
    a													=    CLIPImageProcessor if is_vision_available() else None


    def    A    (  self			:							Dict):
           _A     :	Tuple      =					CLIPImageProcessingTester(self)


    @property
    def    A    (  self			:							Any):
           return self.image_processor_tester.prepare_image_processor_dict()


    def    A    (  self			:							List[str]):
           _A     :	Optional[int]      =					self.image_processing_class(**self.image_processor_dict)
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'do_resize'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'size'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'do_center_crop'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'center_crop'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'do_normalize'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'image_mean'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'image_std'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'do_convert_rgb'))


    def    A    (  self			:							Optional[int]):
           _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    A    (  self			:							List[str]):
           pass


    def    A    (  self			:							Optional[int]):
           # Initialize image_processing
           _A     :	Any      =					self.image_processing_class(**self.image_processor_dict)
           # create random PIL images
           _A     :	List[Any]      =					self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE)
           for image in image_inputs:
                  self.assertIsInstance(SCREAMING_SNAKE_CASE      ,							Image.Image)

           # Test not batched input
           _A     :	List[str]      =					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     :	List[str]      =					image_processing(SCREAMING_SNAKE_CASE      ,							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    A    (  self			:							Dict):
           # Initialize image_processing
           _A     :	Optional[int]      =					self.image_processing_class(**self.image_processor_dict)
           # create random numpy tensors
           _A     :	Tuple      =					self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE      ,							numpify=SCREAMING_SNAKE_CASE)
           for image in image_inputs:
                  self.assertIsInstance(SCREAMING_SNAKE_CASE      ,							np.ndarray)

           # Test not batched input
           _A     :	Dict      =					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     :	Tuple      =					image_processing(SCREAMING_SNAKE_CASE      ,							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    A    (  self			:							str):
           # Initialize image_processing
           _A     :	Optional[Any]      =					self.image_processing_class(**self.image_processor_dict)
           # create random PyTorch tensors
           _A     :	Tuple      =					self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE      ,							torchify=SCREAMING_SNAKE_CASE)
           for image in image_inputs:
                  self.assertIsInstance(SCREAMING_SNAKE_CASE      ,							torch.Tensor)

           # Test not batched input
           _A     :	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     :	Union[str, Any]      =					image_processing(SCREAMING_SNAKE_CASE      ,							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  __lowerCamelCase (       a_				,					unittest.TestCase       ):
    """simple docstring"""
    a													=    CLIPImageProcessor if is_vision_available() else None


    def    A    (  self			:							int):
           _A     :	Union[str, Any]      =					CLIPImageProcessingTester(self      ,							num_channels=4)
           _A     :	Dict      =					3


    @property
    def    A    (  self			:							Any):
           return self.image_processor_tester.prepare_image_processor_dict()


    def    A    (  self			:							str):
           _A     :	str      =					self.image_processing_class(**self.image_processor_dict)
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'do_resize'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'size'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'do_center_crop'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'center_crop'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'do_normalize'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'image_mean'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'image_std'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE      ,							'do_convert_rgb'))


    def    A    (  self			:							str):
           pass


    def    A    (  self			:							int):
           # Initialize image_processing
           _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=SCREAMING_SNAKE_CASE)
           for image in image_inputs:
                  self.assertIsInstance(SCREAMING_SNAKE_CASE      ,							Image.Image)

           # Test not batched input
           _A     :	int      =					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     :	str      =					image_processing(SCREAMING_SNAKE_CASE      ,							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'],
               )      ,							)






