




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 							A__   (snake_case      :		Any		,  snake_case      :		Tuple		,  snake_case      :		Optional[int]=None							)    ->	Tuple:
					if seed is not None:  # useful for obtaining consistent results
										np.random.seed(snake_case							)
					__UpperCamelCase				:  List[str]								=			data.shape[0]  # number of data points

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

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

					return centroids





def 							A__   (snake_case      :		Optional[Any]		,  snake_case      :		str							)    ->	Optional[Any]:
					return pairwise_distances(snake_case		,  snake_case		,  metric="""euclidean"""							)





def 							A__   (snake_case      :		Tuple		,  snake_case      :		Tuple							)    ->	Optional[Any]:
					# Compute distances between each data point and the set of centroids:
					# Fill in the blank (RHS only)
					__UpperCamelCase				:  Optional[int]								=			centroid_pairwise_dist(snake_case		,  snake_case							)

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

					return cluster_assignment





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

					return new_centroids





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

					return heterogeneity





def 							A__   (snake_case      :		Optional[Any]		,  snake_case      :		Dict							)    ->	Dict:
					plt.figure(figsize=(7, 4)							)
					plt.plot(snake_case		,  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 							A__   (snake_case      :		Union[str, Any]		,  snake_case      :		Optional[Any]		,  snake_case      :		Dict		,  snake_case      :		str=5_00		,  snake_case      :		Union[str, Any]=None		,  snake_case      :		List[Any]=False							)    ->	Tuple:
					__UpperCamelCase				:  List[str]								=			initial_centroids[:]
					__UpperCamelCase				:  Union[str, Any]								=			None

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

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

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

										# Check for convergence: if none of the assignments changed, stop
										if (
										    prev_cluster_assignment is not None
										    and (prev_cluster_assignment == cluster_assignment).all()
										):
															break

										# Print number of new assignments
										if prev_cluster_assignment is not None:
															__UpperCamelCase				:  int								=			np.sum(prev_cluster_assignment != cluster_assignment							)
															if verbose:
																				print(
																				    F'''    {num_changed:5d} elements changed their cluster assignment.'''							)

        # Record heterogeneity convergence metric
										if record_heterogeneity is not None:
															# YOUR CODE HERE
															__UpperCamelCase				:  Any								=			compute_heterogeneity(snake_case		,  snake_case		,  snake_case		,  snake_case							)
															record_heterogeneity.append(snake_case							)

										__UpperCamelCase				:  List[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

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


if __name__ == "__main__":
	import doctest

	doctest.testmod()












import os





def 							A__   ()    ->	int:
					__UpperCamelCase				:  int								=			os.path.dirname(os.path.realpath(snake_case							)							)
					__UpperCamelCase				:  List[str]								=			os.path.join(snake_case		,  """triangle.txt"""							)

					with open(snake_case							) as in_file:
										__UpperCamelCase				:  Optional[Any]								=			[[int(snake_case							) for i in line.split()] for line in in_file]

					while len(snake_case							) != 1:
										__UpperCamelCase				:  Union[str, Any]								=			triangle.pop()
										__UpperCamelCase				:  List[str]								=			triangle[-1]
										for j in range(len(snake_case							) - 1							):
															curr_row[j] += max(last_row[j]		,  last_row[j + 1]							)
					return triangle[0][0]


if __name__ == "__main__":
	print(solution())






