Transformations
The transform
module provides utilities for preprocessing and transforming datasets for fairness research.
This module will only be interesting for advanced preprocessing needs / high configurability, as the transform()
method of the Dataset class provides an easier to use, but slightly less flexible interface.
Module Documentation
fairml_datasets.transform
Data transformation utilities for fairness datasets.
This module provides functions to transform datasets into formats suitable for fairness analysis, including handling of sensitive attributes, target variables, missing values, and categorical features.
Classes
PreprocessingInfo
dataclass
Class to store information about preprocessing steps applied to a dataset.
Attributes:
Name | Type | Description |
---|---|---|
sensitive_columns |
List[str]
|
List of column names identified as sensitive attributes |
col_na_indicator |
Series
|
Series indicating rows with missing values in the original dataset |
Source code in fairml_datasets/transform.py
Functions
filter_columns(df, sensitive_columns, feature_columns, target_column)
Filter a DataFrame to include only specified columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame to filter |
required |
sensitive_columns
|
List[str]
|
List of sensitive attribute column names to include |
required |
feature_columns
|
List[str]
|
List of feature column names to include |
required |
target_column
|
str
|
Target column name to include |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Filtered DataFrame with only the specified columns |
Source code in fairml_datasets/transform.py
limit_categorical_levels(df, columns, max_unique, other_value='OTHER')
Limit the number of unique values in categorical columns by combining less frequent values into an 'other' category.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame containing categorical columns |
required |
columns
|
List[str]
|
List of categorical column names to transform |
required |
max_unique
|
int
|
Maximum number of unique values to keep (most frequent) |
required |
other_value
|
str
|
Value to use for the combined less frequent categories |
'OTHER'
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame with limited categorical levels |
Source code in fairml_datasets/transform.py
transform(df, sensitive_columns, feature_columns, target_column, target_lvl_good_bad=None, select_columns='typical_only', transform_na_numerical='impute_median', transform_na_character='new_value', transform_target='auto', transform_sensitive_columns='none', transform_sensitive_values='none', transform_categorical='dummy', max_categorical_levels=200)
Transform a DataFrame for fairness analysis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame to transform |
required |
sensitive_columns
|
List[str]
|
List of sensitive attribute column names |
required |
feature_columns
|
List[str]
|
List of feature column names |
required |
target_column
|
str
|
Target column name |
required |
target_lvl_good_bad
|
Optional[str]
|
Optional string specifying the "good" level for the target column |
None
|
select_columns
|
Literal['keep_all', 'typical_only']
|
Strategy for selecting columns ("keep_all" or "typical_only") |
'typical_only'
|
transform_na_numerical
|
Literal['drop_columns', 'drop_rows', 'impute_median']
|
Strategy for handling missing values in numerical columns |
'impute_median'
|
transform_na_character
|
Literal['drop_columns', 'drop_rows', 'new_value']
|
Strategy for handling missing values in categorical columns |
'new_value'
|
transform_target
|
Literal['auto', 'good_bad', 'majority_minority']
|
Strategy for transforming the target column |
'auto'
|
transform_sensitive_columns
|
Literal['none', 'intersection_binary']
|
Strategy for handling multiple sensitive columns |
'none'
|
transform_sensitive_values
|
Literal['majority_minority', 'none']
|
Strategy for transforming sensitive values |
'none'
|
transform_categorical
|
Literal['dummy', 'none']
|
Strategy for transforming categorical columns |
'dummy'
|
max_categorical_levels
|
Optional[int]
|
Limit the number of unique values in categorical columns. Set to None to disable. |
200
|
Returns:
Type | Description |
---|---|
tuple[DataFrame, PreprocessingInfo]
|
tuple[pd.DataFrame, PreprocessingInfo]: Transformed DataFrame and preprocessing information |
Source code in fairml_datasets/transform.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|