new_class_counts [ ]*self num_classes feature_sum[ ][cis the sum of the values of feature for class new_feature_sum [[ ]*self num_classes for feat in self dataset input_featuresfor eg in self dataset traincl self class_of_eg(egnew_class_counts[cl+ for (ind,featin enumerate(self dataset input_features)new_feature_sum[ind][cl+feat(egstable (new_class_counts =self class_countsand (self feature_sum =new_feature_sumself class_counts new_class_counts self feature_sum new_feature_sum self num_iterations + return stable def learn(self, = )"""do steps of -meansor until convergence"" = stable false while < and not stablestable self k_means_step( + self display( ,"iteration",self num_iterations"class counts",self class_counts,stable=",stablereturn stable def show_classes(self)"""sorts the data by the class and prints in order for visualizing small data sets ""class_examples [[for in range(self num_classes)for eg in self dataset trainclass_examples[self class_of_eg(eg)append(egprint("class","example",sep='\ 'for cl in range(self num_classes)for eg in class_examples[cl]print(cl,*eg,sep='\ ' def plot_error(selfmaxstep= )"""plots the sum-of-suares error as function of the number of steps""plt ion(plt xlabel("step"plt ylabel("ave sum-of-squares error"train_errors [if self dataset testversion june