tabular/src/autogluon/tabular/models/knn/knn_model.py
def _fit_with_samples(self, X_train, y_train, time_limit):
        """
        Fit model with samples of the data repeatedly, gradually increasing the amount of data until time_limit is reached or all data is used.

        X_train and y_train must already be preprocessed
        """
        time_start = time.time()

        sample_growth_factor = 2  # Growth factor of each sample in terms of row count
        sample_time_growth_factor = 8  # Assume next sample will take 8x longer than previous (Somewhat safe but there are datasets where it is even >8x.

        num_rows_samples = []
        num_rows_max = len(X_train)
        num_rows_cur = 10000
        while True:
            num_rows_cur = min(num_rows_cur, num_rows_max)
            num_rows_samples.append(num_rows_cur)
            if num_rows_cur == num_rows_max:
                break
            num_rows_cur *= sample_growth_factor
            num_rows_cur = math.ceil(num_rows_cur)
            if num_rows_cur * 1.5 >= num_rows_max:
                num_rows_cur = num_rows_max

        def sample_func(chunk, frac):
            # Guarantee at least 1 sample (otherwise log_loss would crash or model would return different column counts in pred_proba)
            n = max(math.ceil(len(chunk) * frac), 1)
            return chunk.sample(n=n, replace=False, random_state=0)

        if self.problem_type != REGRESSION:
            y_train_df = y_train.to_frame(name='label').reset_index(drop=True)
        else:
            y_train_df = None

        time_start_sample_loop = time.time()
        time_limit_left = time_limit - (time_start_sample_loop - time_start)
        for i, samples in enumerate(num_rows_samples):
            if samples != num_rows_max:
                if self.problem_type == REGRESSION:
                    idx = np.random.choice(num_rows_max, size=samples, replace=False)
                else:
                    idx = y_train_df.groupby('label', group_keys=False).apply(sample_func, frac=samples/num_rows_max).index
                X_train_samp = X_train[idx, :]
                y_train_samp = y_train.iloc[idx]
            else:
                X_train_samp = X_train
                y_train_samp = y_train
            self.model = self._model_type(**self.params).fit(X_train_samp, y_train_samp)
            time_limit_left_prior = time_limit_left
            time_fit_end_sample = time.time()
            time_limit_left = time_limit - (time_fit_end_sample - time_start)
            time_fit_sample = time_limit_left_prior - time_limit_left
            time_required_for_next = time_fit_sample * sample_time_growth_factor
            logger.log(15, f'\t{round(time_fit_sample, 2)}s \t= Train Time (Using {samples}/{num_rows_max} rows) ({round(time_limit_left, 2)}s remaining time)')
            if time_required_for_next > time_limit_left and i != len(num_rows_samples) - 1:
                logger.log(20, f'\tNot enough time to train KNN model on all training rows. Fit {samples}/{num_rows_max} rows. (Training KNN model on {num_rows_samples[i+1]} rows is expected to take {round(time_required_for_next, 2)}s)')
                break
        return self.model

        Fit model with samples of the data repeatedly, gradually increasing the amount of data until time_limit is reached or all data is used.

        X_train and y_train must already be preprocessed
        
Fit model with samples of the data repeatedly, gradually increasing the amount of data until time_limit is reached or all data is used.
X_train and y_train must already be preprocessed
Fit model with samples of the data repeatedly gradually increasing the amount of data until time_limit is reached or all data is used . X_train and y_train must already be preprocessed
def _fit_with_samples(self, X_train, y_train, time_limit):
        time_start = time.time()
        sample_growth_factor = 2  
        sample_time_growth_factor = 8  
        num_rows_samples = []
        num_rows_max = len(X_train)
        num_rows_cur = 10000
        while True:
            num_rows_cur = min(num_rows_cur, num_rows_max)
            num_rows_samples.append(num_rows_cur)
            if num_rows_cur == num_rows_max:
                break
            num_rows_cur *= sample_growth_factor
            num_rows_cur = math.ceil(num_rows_cur)
            if num_rows_cur * 1.5 >= num_rows_max:
                num_rows_cur = num_rows_max
        def sample_func(chunk, frac):
            n = max(math.ceil(len(chunk) * frac), 1)
            return chunk.sample(n=n, replace=False, random_state=0)
        if self.problem_type != REGRESSION:
            y_train_df = y_train.to_frame(name='label').reset_index(drop=True)
        else:
            y_train_df = None
        time_start_sample_loop = time.time()
        time_limit_left = time_limit - (time_start_sample_loop - time_start)
        for i, samples in enumerate(num_rows_samples):
            if samples != num_rows_max:
                if self.problem_type == REGRESSION:
                    idx = np.random.choice(num_rows_max, size=samples, replace=False)
                else:
                    idx = y_train_df.groupby('label', group_keys=False).apply(sample_func, frac=samples/num_rows_max).index
                X_train_samp = X_train[idx, :]
                y_train_samp = y_train.iloc[idx]
            else:
                X_train_samp = X_train
                y_train_samp = y_train
            self.model = self._model_type(**self.params).fit(X_train_samp, y_train_samp)
            time_limit_left_prior = time_limit_left
            time_fit_end_sample = time.time()
            time_limit_left = time_limit - (time_fit_end_sample - time_start)
            time_fit_sample = time_limit_left_prior - time_limit_left
            time_required_for_next = time_fit_sample * sample_time_growth_factor
            logger.log(15, f'\t{round(time_fit_sample, 2)}s \t= Train Time (Using {samples}/{num_rows_max} rows) ({round(time_limit_left, 2)}s remaining time)')
            if time_required_for_next > time_limit_left and i != len(num_rows_samples) - 1:
                logger.log(20, f'\tNot enough time to train KNN model on all training rows. Fit {samples}/{num_rows_max} rows. (Training KNN model on {num_rows_samples[i+1]} rows is expected to take {round(time_required_for_next, 2)}s)')
                break
        return self.model
def _fit_with_samples ( self , X_train , y_train , time_limit ) : time_start = time . time ( ) sample_growth_factor = 2 sample_time_growth_factor = 8 num_rows_samples = [ ] num_rows_max = len ( X_train ) num_rows_cur = 10000 while True : num_rows_cur = min ( num_rows_cur , num_rows_max ) num_rows_samples . append ( num_rows_cur ) if num_rows_cur == num_rows_max : break num_rows_cur *= sample_growth_factor num_rows_cur = math . ceil ( num_rows_cur ) if num_rows_cur * 1.5 >= num_rows_max : num_rows_cur = num_rows_max def sample_func ( chunk , frac ) : n = max ( math . ceil ( len ( chunk ) * frac ) , 1 ) return chunk . sample ( n = n , replace = False , random_state = 0 ) if self . problem_type != REGRESSION : y_train_df = y_train . to_frame ( name = 'label' ) . reset_index ( drop = True ) else : y_train_df = None time_start_sample_loop = time . time ( ) time_limit_left = time_limit - ( time_start_sample_loop - time_start ) for i , samples in enumerate ( num_rows_samples ) : if samples != num_rows_max : if self . problem_type == REGRESSION : idx = np . random . choice ( num_rows_max , size = samples , replace = False ) else : idx = y_train_df . groupby ( 'label' , group_keys = False ) . apply ( sample_func , frac = samples / num_rows_max ) . index X_train_samp = X_train [ idx , : ] y_train_samp = y_train . iloc [ idx ] else : X_train_samp = X_train y_train_samp = y_train self . model = self . _model_type ( ** self . params ) . fit ( X_train_samp , y_train_samp ) time_limit_left_prior = time_limit_left time_fit_end_sample = time . time ( ) time_limit_left = time_limit - ( time_fit_end_sample - time_start ) time_fit_sample = time_limit_left_prior - time_limit_left time_required_for_next = time_fit_sample * sample_time_growth_factor logger . log ( 15 , f'\t{round(time_fit_sample, 2)}s \t= Train Time (Using {samples}/{num_rows_max} rows) ({round(time_limit_left, 2)}s remaining time)' ) if time_required_for_next > time_limit_left and i != len ( num_rows_samples ) - 1 : logger . log ( 20 , f'\tNot enough time to train KNN model on all training rows. Fit {samples}/{num_rows_max} rows. (Training KNN model on {num_rows_samples[i+1]} rows is expected to take {round(time_required_for_next, 2)}s)' ) break return self . model
Fit model with samples of the data repeatedly, gradually increasing the amount of data until time_limit is reached or all data is used.
Fit model with samples of the data repeatedly gradually increasing the amount of data until time_limit is reached or all data is used .
"""
        Fit model with samples of the data repeatedly, gradually increasing the amount of data until time_limit is reached or all data is used.

        X_train and y_train must already be preprocessed
        """ # Growth factor of each sample in terms of row count # Assume next sample will take 8x longer than previous (Somewhat safe but there are datasets where it is even >8x. # Guarantee at least 1 sample (otherwise log_loss would crash or model would return different column counts in pred_proba)
self
X_train
y_train
time_limit