Format: application/x-python-code
1 o
2
3 ?�h� � @ s d dl ZG dd� d�ZdS )� Nc @ sr e Zd Zdefdd�Zdejdejfdd�Zdejdejfdd �Zdejdejfd
4 d�Z dejdejfdd
5 �Z
6 dS )�Sampler�sampling_configc C s
7 || _ d S )N)r )�selfr � r �<C:\Users\shubh\Videos\recall\recall_audit_sampler\sampler.py�__init__ s
8 zSampler.__init__�df�returnc C sP | j �d�}|dkr| �|�S |dkr| �|�S |dkr!| �|�S td|� ���)N�type�random�
9 stratifiedzstratified-proportionalzUnsupported sampling strategy: )r �get�
10 random_sample�stratified_sample�stratified_proportional_sample�
11 ValueError)r r �strategyr r r �sample s
12
13
14 zSampler.samplec C s4 | j �d�}|d u rtd��|jt|t|��dd�S )N�sample_sizez4`sample_size` must be specified for random sampling.�* ��n�random_state)r r
15 r r �min�len)r r r r r r r s zSampler.random_samplec C s@ | j �d�}| j �d�}|std��|�|�}g }|d u rI| j �dd�}td|� d�� |D ]\}}t|t|��} | dkrG|�|j| d d
16 �� q.nG|D ]D\}}t |t
17 �r\d�tt
18 |��nt
19 |�}
20 t |t�rk|�|
21 d�n|}t|t|��} | dkr�|�|j| d d
22 �� td|
23 � d
24 |� d| � �� qK|r�tj|dd�S tj|jd�S )N�by�ratioz/`by` must be specified for stratified sampling.Zdefault_per_group�
25 z1[INFO] No ratio provided. Using default ratio of z per group.r r r �:z
26 [STRATIFIED] u → requested: z, sampled: T��ignore_index��columns)r r
27 r �groupby�printr r �appendr �
28 isinstance�tuple�join�map�str�dict�pd�concat� DataFramer"