def get_samples_near_target()

in dynamicfilters/worddensityfiltering.py [0:0]


    def get_samples_near_target(df, target, range_width=0.1, num_samples=3):
        """
        Get samples from the DataFrame that have 'word_density' close to the target value.

        :param df: DataFrame to sample from.
        :param target: The target word density to find samples around.
        :param range_width: The width of the range around the target value.
        :param num_samples: Number of samples to return.
        :return: A DataFrame with samples close to the target density.
        """
        # Define the range around the target
        lower_bound = target - range_width
        upper_bound = target + range_width
        
        # Filter and sample
        samples = df[(df['word_density'] >= lower_bound) & (df['word_density'] <= upper_bound)].sample(n=num_samples, random_state=1)
        return samples