




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_      						=   "K-MEANS-CLUST/ "




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_    ,   lowercase_=None					)				->		Tuple:

    """simple docstring"""


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

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

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

    return centroids




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		str:

    """simple docstring"""


    return pairwise_distances(lowercase_    ,   lowercase_    ,   metric='''euclidean'''					)




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		str:

    """simple docstring"""


    __UpperCamelCase     						=  centroid_pairwise_dist(lowercase_    ,   lowercase_					)

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

    return cluster_assignment




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_    ,   lowercase_					)				->		Dict:

    """simple docstring"""


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

    return new_centroids




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_    ,   lowercase_    ,   lowercase_					)				->		Dict:

    """simple docstring"""


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

    return heterogeneity




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		List[Any]:

    """simple docstring"""


    plt.figure(figsize=(7, 4)					)
    plt.plot(lowercase_    ,   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      (     lowercase_    ,   lowercase_    ,   lowercase_    ,   lowercase_=5_00    ,   lowercase_=None    ,   lowercase_=False					)				->		Optional[int]:

    """simple docstring"""


    __UpperCamelCase     						=  initial_centroids[:]
    __UpperCamelCase     						=  None

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

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

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

        # 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     						=  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     						=  compute_heterogeneity(lowercase_    ,   lowercase_    ,   lowercase_    ,   lowercase_					)
            record_heterogeneity.append(lowercase_					)

        __UpperCamelCase     						=  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_      						=   ds.load_iris()
 a_      						=   3
 a_      						=   []
 a_      						=   get_initial_centroids(dataset["data"], k, seed=0)
 a_						,			a_      						=   kmeans(
     dataset["data"],
     k,
     initial_centroids,
     maxiter=400,
     record_heterogeneity=heterogeneity,
     verbose=True,
 )
 plot_heterogeneity(heterogeneity, k)




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_    ,   lowercase_=None					)				->		pd.DataFrame:

    """simple docstring"""


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


if __name__ == "__main__":
 import doctest

 doctest.testmod()












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 TvltImageProcessor




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_=10    ,   lowercase_=10    ,   lowercase_=False    ,   lowercase_=False					)				->		Dict:

    """simple docstring"""


    __UpperCamelCase     						=  []
    for i in range(image_processor_tester.num_frames					):
        video.append(np.random.randint(2_55    ,   size=(image_processor_tester.num_channels, width, height)    ,   dtype=np.uinta					)					)

    if not numpify and not torchify:
        # PIL expects the channel dimension as last dimension
        __UpperCamelCase     						=  [Image.fromarray(np.moveaxis(lowercase_    ,   0    ,   -1					)					) for frame in video]

    if torchify:
        __UpperCamelCase     						=  [torch.from_numpy(lowercase_					) for frame in video]

    return video




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_=False    ,   lowercase_=False    ,   lowercase_=False					)				->		Tuple:

    """simple docstring"""


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

    __UpperCamelCase     						=  []
    for i in range(image_processor_tester.batch_size					):
        if equal_resolution:
            __UpperCamelCase     						=  __UpperCamelCase     						=  image_processor_tester.max_resolution
        else:
            __UpperCamelCase						,					__UpperCamelCase     						=  np.random.choice(
                np.arange(image_processor_tester.min_resolution    ,   image_processor_tester.max_resolution					)    ,   2					)
            __UpperCamelCase     						=  prepare_video(
                image_processor_tester=lowercase_    ,   width=lowercase_    ,   height=lowercase_    ,   numpify=lowercase_    ,   torchify=lowercase_    ,   )
        video_inputs.append(lowercase_					)

    return video_inputs

class        _lowerCamelCase  (	unittest.TestCase      ):

      """simple docstring"""

      def __init__(   self    :       List[str]       ,				snake_case    :       int       ,				snake_case    :       int=7       ,				snake_case    :       int=3       ,				snake_case    :       Any=4       ,				snake_case    :       Tuple=18       ,				snake_case    :       Optional[int]=30       ,				snake_case    :       List[Any]=400       ,				snake_case    :       str=True       ,				snake_case    :       str=None       ,				snake_case    :       Optional[Any]=True       ,				snake_case    :       str=[0.5, 0.5, 0.5]       ,				snake_case    :       Optional[int]=[0.5, 0.5, 0.5]       ,				snake_case    :       Any=True       ,				snake_case    :       str=None       ,				):
          __UpperCamelCase     						=  size if size is not None else {'''shortest_edge''': 18}
          __UpperCamelCase     						=  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

          __UpperCamelCase     						=  parent
          __UpperCamelCase     						=  batch_size
          __UpperCamelCase     						=  num_channels
          __UpperCamelCase     						=  num_frames
          __UpperCamelCase     						=  image_size
          __UpperCamelCase     						=  min_resolution
          __UpperCamelCase     						=  max_resolution
          __UpperCamelCase     						=  do_resize
          __UpperCamelCase     						=  size
          __UpperCamelCase     						=  do_normalize
          __UpperCamelCase     						=  image_mean
          __UpperCamelCase     						=  image_std
          __UpperCamelCase     						=  do_center_crop
          __UpperCamelCase     						=  crop_size







      def 					snake_case		(   self    :       Any ):
          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
              "do_center_crop": self.do_center_crop,
              "crop_size": self.crop_size,
          }







@require_torch
@require_vision
class        _lowerCamelCase  (	_SCREAMING_SNAKE_CASE							,	unittest.TestCase      ):

      """simple docstring"""

      lowerCAmelCase__	:       str			=    TvltImageProcessor if is_vision_available() else None







      def 					snake_case		(   self    :       Dict ):
          __UpperCamelCase     						=  TvltImageProcessorTester(self )







      @property
      def 					snake_case		(   self    :       Dict ):
          return self.image_processor_tester.prepare_image_processor_dict()







      def 					snake_case		(   self    :       int ):
          __UpperCamelCase     						=  self.image_processing_class(**self.image_processor_dict )
          self.assertTrue(hasattr(snake_case       ,				'''image_mean''' ) )
          self.assertTrue(hasattr(snake_case       ,				'''image_std''' ) )
          self.assertTrue(hasattr(snake_case       ,				'''do_normalize''' ) )
          self.assertTrue(hasattr(snake_case       ,				'''do_resize''' ) )
          self.assertTrue(hasattr(snake_case       ,				'''do_center_crop''' ) )
          self.assertTrue(hasattr(snake_case       ,				'''size''' ) )







      def 					snake_case		(   self    :       Union[str, Any] ):
          # Initialize image_processor
          __UpperCamelCase     						=  self.image_processing_class(**self.image_processor_dict )
          # create random PIL videos
          __UpperCamelCase     						=  prepare_video_inputs(self.image_processor_tester       ,				equal_resolution=snake_case )
          for video in video_inputs:
              self.assertIsInstance(snake_case       ,				snake_case )
              self.assertIsInstance(video[0]       ,				Image.Image )

          # Test not batched input
          __UpperCamelCase     						=  image_processor(video_inputs[0]       ,				return_tensors='''pt''' ).pixel_values
          self.assertEqual(
              encoded_videos.shape       ,				(
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )       ,				)

          # Test batched
          __UpperCamelCase     						=  image_processor(snake_case       ,				return_tensors='''pt''' ).pixel_values
          self.assertEqual(
              encoded_videos.shape       ,				(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )       ,				)







      def 					snake_case		(   self    :       List[Any] ):
          # Initialize image_processor
          __UpperCamelCase     						=  self.image_processing_class(**self.image_processor_dict )
          # create random numpy tensors
          __UpperCamelCase     						=  prepare_video_inputs(self.image_processor_tester       ,				equal_resolution=snake_case       ,				numpify=snake_case )
          for video in video_inputs:
              self.assertIsInstance(snake_case       ,				snake_case )
              self.assertIsInstance(video[0]       ,				np.ndarray )

          # Test not batched input
          __UpperCamelCase     						=  image_processor(video_inputs[0]       ,				return_tensors='''pt''' ).pixel_values
          self.assertEqual(
              encoded_videos.shape       ,				(
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )       ,				)

          # Test batched
          __UpperCamelCase     						=  image_processor(snake_case       ,				return_tensors='''pt''' ).pixel_values
          self.assertEqual(
              encoded_videos.shape       ,				(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )       ,				)







      def 					snake_case		(   self    :       Union[str, Any] ):
          # Initialize image_processor
          __UpperCamelCase     						=  self.image_processing_class(**self.image_processor_dict )
          # create random PyTorch tensors
          __UpperCamelCase     						=  prepare_video_inputs(self.image_processor_tester       ,				equal_resolution=snake_case       ,				torchify=snake_case )
          for video in video_inputs:
              self.assertIsInstance(snake_case       ,				snake_case )
              self.assertIsInstance(video[0]       ,				torch.Tensor )

          # Test not batched input
          __UpperCamelCase     						=  image_processor(video_inputs[0]       ,				return_tensors='''pt''' ).pixel_values
          self.assertEqual(
              encoded_videos.shape       ,				(
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )       ,				)

          # Test batched
          __UpperCamelCase     						=  image_processor(snake_case       ,				return_tensors='''pt''' ).pixel_values
          self.assertEqual(
              encoded_videos.shape       ,				(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )       ,				)






