(self, data_instances, k)
return result
Find 1/k mode. If there is a mode that number of which is larger than 1/k of total nums, return this mode and
        its percentage. If there is not, return None, None.

        Parameters
        ----------
        data_instances: Table
            Original data

        k: int
Find 1/k mode. If there is a mode that number of which is larger than 1/k of total nums, return this mode and
        its percentage. If there is not, return None, None.
Find 1 / k mode . If there is a mode that number of which is larger than 1 / k of total nums return this mode and its percentage . If there is not return None None .
def _find_kth_mode(self, data_instances, k):
        """
        Find 1/k mode. If there is a mode that number of which is larger than 1/k of total nums, return this mode and
        its percentage. If there is not, return None, None.

        Parameters
        ----------
        data_instances: Table
            Original data

        k: int
        """
        is_sparse = is_sparse_data(data_instances)

        def find_mode_candidate(instances, select_cols):
            """
            Find at most k - 1 mode candidates.
            Parameters
            ----------
            instances: Data generator
                Original data
            k: int

            select_cols: list
                Indicates columns that need to be operated.

            is_sparse: bool
                Whether input data format is sparse

            Returns
            -------
            all_candidates: dict
                Each key is col_index and value is a list that contains mode candidates.
            """
            all_candidates = {}
            for col_index in select_cols:
                all_candidates[col_index] = {}

            for _, instant in instances:
                for col_index in select_cols:
                    candidate_dict = all_candidates[col_index]
                    if is_sparse:
                        feature_value = instant.features.get_data(col_index, 0)
                    else:
                        feature_value = instant.features[col_index]
                    if isinstance(feature_value, float):
                        feature_value = round(feature_value, 8)

                    if feature_value in candidate_dict:
                        candidate_dict[feature_value] += 1
                    elif len(candidate_dict) < k - 1:
                        candidate_dict[feature_value] = 1
                    else:
                        to_delete_col = []
                        for key in candidate_dict:
                            candidate_dict[key] -= 1
                            if candidate_dict[key] == 0:
                                to_delete_col.append(key)
                        for d_k in to_delete_col:
                            del candidate_dict[d_k]
            for col_index, candidate_dict in all_candidates.items():
                candidate_dict = {key: 0 for key, _ in candidate_dict.items()}
                all_candidates[col_index] = candidate_dict

            return all_candidates

        def merge_mode_candidate(d1, d2):
            assert len(d1) == len(d2)
            for col_idx, d in d1.items():
                d.update(d2[col_idx])
            return d1

        def merge_candidates_num(candi_1, candi_2):
            assert len(candi_1) == len(candi_2)
            for col_idx, candidate_dict in candi_1.items():
                candi_dict_2 = candi_2[col_idx]
                for feature_value, num in candi_dict_2.items():
                    if feature_value in candidate_dict:
                        candidate_dict[feature_value] += num
                    else:
                        candidate_dict[feature_value] = num
            return candi_1

        def static_candidates_num(instances, select_cols, all_candidates):
            """
            Static number of candidates
            Parameters
            ----------
            instances: Data generator
                Original data

            select_cols: list
                Indicates columns that need to be operated.

            all_candidates: dict
                Each key is col_index and value is a list that contains mode candidates.
            """

            for _, instant in instances:
                for col_index in select_cols:
                    candidate_dict = all_candidates[col_index]
                    if is_sparse:
                        feature_value = instant.features.get_data(col_index, NoneType())
                    else:
                        feature_value = instant.features[col_index]
                    if isinstance(feature_value, float):
                        feature_value = round(feature_value, 8)

                    if feature_value in candidate_dict:
                        candidate_dict[feature_value] += 1

            # mode_result = {}
            # for col_index, candidate_dict in all_candidates.items():
            #     feature_value, nums = sorted(candidate_dict.items(), key=operator.itemgetter(1), reverse=False)[0]
            #     mode_result[col_index] = (feature_value, nums)
            return all_candidates

        find_func = functools.partial(find_mode_candidate,
                                      select_cols=self.selection_properties.select_col_indexes)
        all_candidates = data_instances.applyPartitions(find_func).reduce(merge_mode_candidate)
        static_func = functools.partial(static_candidates_num,
                                        select_cols=self.selection_properties.select_col_indexes,
                                        all_candidates=all_candidates)
        mode_candidate_statics = data_instances.applyPartitions(static_func).reduce(merge_candidates_num)
        result = {}
        for col_index, candidate_dict in mode_candidate_statics.items():
            if len(candidate_dict) > 0:
                res = sorted(candidate_dict.items(), key=operator.itemgetter(1), reverse=True)[0]
            else:
                res = None
            result[col_index] = res

        return result
def _find_kth_mode ( self , data_instances , k ) : is_sparse = is_sparse_data ( data_instances ) def find_mode_candidate ( instances , select_cols ) : """
            Find at most k - 1 mode candidates.
            Parameters
            ----------
            instances: Data generator
                Original data
            k: int

            select_cols: list
                Indicates columns that need to be operated.

            is_sparse: bool
                Whether input data format is sparse

            Returns
            -------
            all_candidates: dict
                Each key is col_index and value is a list that contains mode candidates.
            """ all_candidates = { } for col_index in select_cols : all_candidates [ col_index ] = { } for _ , instant in instances : for col_index in select_cols : candidate_dict = all_candidates [ col_index ] if is_sparse : feature_value = instant . features . get_data ( col_index , 0 ) else : feature_value = instant . features [ col_index ] if isinstance ( feature_value , float ) : feature_value = round ( feature_value , 8 ) if feature_value in candidate_dict : candidate_dict [ feature_value ] += 1 elif len ( candidate_dict ) < k - 1 : candidate_dict [ feature_value ] = 1 else : to_delete_col = [ ] for key in candidate_dict : candidate_dict [ key ] -= 1 if candidate_dict [ key ] == 0 : to_delete_col . append ( key ) for d_k in to_delete_col : del candidate_dict [ d_k ] for col_index , candidate_dict in all_candidates . items ( ) : candidate_dict = { key : 0 for key , _ in candidate_dict . items ( ) } all_candidates [ col_index ] = candidate_dict return all_candidates def merge_mode_candidate ( d1 , d2 ) : assert len ( d1 ) == len ( d2 ) for col_idx , d in d1 . items ( ) : d . update ( d2 [ col_idx ] ) return d1 def merge_candidates_num ( candi_1 , candi_2 ) : assert len ( candi_1 ) == len ( candi_2 ) for col_idx , candidate_dict in candi_1 . items ( ) : candi_dict_2 = candi_2 [ col_idx ] for feature_value , num in candi_dict_2 . items ( ) : if feature_value in candidate_dict : candidate_dict [ feature_value ] += num else : candidate_dict [ feature_value ] = num return candi_1 def static_candidates_num ( instances , select_cols , all_candidates ) : """
            Static number of candidates
            Parameters
            ----------
            instances: Data generator
                Original data

            select_cols: list
                Indicates columns that need to be operated.

            all_candidates: dict
                Each key is col_index and value is a list that contains mode candidates.
            """ for _ , instant in instances : for col_index in select_cols : candidate_dict = all_candidates [ col_index ] if is_sparse : feature_value = instant . features . get_data ( col_index , NoneType ( ) ) else : feature_value = instant . features [ col_index ] if isinstance ( feature_value , float ) : feature_value = round ( feature_value , 8 ) if feature_value in candidate_dict : candidate_dict [ feature_value ] += 1 # mode_result = {} # for col_index, candidate_dict in all_candidates.items(): #     feature_value, nums = sorted(candidate_dict.items(), key=operator.itemgetter(1), reverse=False)[0] #     mode_result[col_index] = (feature_value, nums) return all_candidates find_func = functools . partial ( find_mode_candidate , select_cols = self . selection_properties . select_col_indexes ) all_candidates = data_instances . applyPartitions ( find_func ) . reduce ( merge_mode_candidate ) static_func = functools . partial ( static_candidates_num , select_cols = self . selection_properties . select_col_indexes , all_candidates = all_candidates ) mode_candidate_statics = data_instances . applyPartitions ( static_func ) . reduce ( merge_candidates_num ) result = { } for col_index , candidate_dict in mode_candidate_statics . items ( ) : if len ( candidate_dict ) > 0 : res = sorted ( candidate_dict . items ( ) , key = operator . itemgetter ( 1 ) , reverse = True ) [ 0 ] else : res = None result [ col_index ] = res return result