Imitate
Imitate
Imitate generates points to mitigate a dataset's bias.
Imitate investigates the dataset's probability density, then adds generated points in order to smooth out the density and have it resemble a Gaussian, the most common density occurring in real-world applications. If the artificial points focus on certain areas and are not widespread, this could indicate a Selection Bias where these areas are underrepresented in the sample.
See our paper [1]_ for details.
Attributes:
Name | Type | Description |
---|---|---|
icas |
list(sklearn.decomposition.FastICA)
|
A list of |
grids |
dict(string or int or float: numpy.ndarray (2D))
|
A dictionary mapping a class label to its corresponding grids per dimension over which KDE was evaluated. |
vals |
dict(string or int or float: numpy.ndarray (2D))
|
A KDE density representation of the dataset evaluated over |
fitted |
dict(string or int or float: numpy.ndarray (2D))
|
Fitted Gaussian PDF evaluated over |
fill_up |
dict(string or int or float: numpy.ndarray (2D))
|
|
num_fill_up |
dict(string or int or float: numpy.array (1D))
|
The necessary number of points to add to mitigate the bias; per label and dimension. |
Methods
fit(data, labels=[], bounds={}, strength=1000) Fits the Imitate Gaussians to a dataset. score(data) Scores new data based on Imitate'd fitted Gaussians. augment() Augments the fitted dataset to mitigate its bias.
References
.. [1] Katharina Dost, Katerina Taskova, Patricia Riddle, and Jörg Wicker. "Your Best Guess When You Know Nothing: Identification and Mitigation of Selection Bias." In: 2020 IEEE International Conference on Data Mining (ICDM), pp. 996-1001, IEEE, 2020, ISSN: 2374-8486.
Examples:
>>> from imitatebias.generators import *
>>> from imitatebias.imitate import *
>>> import matplotlib.pyplot as plt
Generate a dataset.
>>> X, y = generateData(1000, 2, 2, seed=2210)
Generate a biased dataset.
>>> X_b, y_b, idcs_deleted = generateBias(X, y, 1, seed=2210)
initialize Imitate
>>> imi = Imitate()
fit Imitate to the biased dataset
>>> imi.fit(X_b, labels=y_b)
visualize data per cluster in ICA space
>>> for l in np.unique(y_b):
>>> data_transformed = imi.icas[l].transform(X_b[y_b == l])
>>> plt.scatter(data_transformed[:,0], data_transformed[:,1])
>>> plt.title('Class '+str(l))
>>> plt.show()
create some random points to score
>>> rnd_points = np.column_stack((np.random.uniform(min(X[:,0]), max(X[:,0]), size=1000), >>> np.random.uniform(min(X[:,1]), max(X[:,1]), size=1000)))
score the random points
>>> scores_fill = imi.score(rnd_points, score_type='fill')
>>> scores_balanced = imi.score(rnd_points, score_type='balanced')
visualize data per cluster in ICA space
>>> for l in np.unique(y_b):
>>> plt.scatter(rnd_points[:,0], rnd_points[:,1], c=scores_fill[:,int(l)])
>>> plt.title('Class '+str(l)+'; Score type = fill')
>>> plt.colorbar()
>>> plt.show()
>>> plt.scatter(rnd_points[:,0], rnd_points[:,1], c=scores_balanced[:,int(l)])
>>> plt.title('Class '+str(l)+'; Score type = balanced')
>>> plt.colorbar()
>>> plt.show()
augment the dataset
>>> X_gen, y_gen = imi.augment()
visualize data per cluster in ICA space
>>> plt.scatter(X_b[:,0], X_b[:,1], c=y_b)
>>> plt.scatter(X_gen[:,0], X_gen[:,1], c=y_gen, edgecolors='red')
>>> plt.title('Dataset with generated points (red)')
>>> plt.show()
Source code in imitatebias\imitate.py
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
|
__init__()
Imitate Constructor.
Source code in imitatebias\imitate.py
286 287 288 289 290 291 292 293 |
|
augment()
Augments the fitted dataset to mitigate its bias.
Generates points to mitigate the bias in the input dataset provided to the fit
method.
The number of generated points per label is determined by Imitate.num_fill_up
.
Returns:
Type | Description |
---|---|
numpy.ndarray (2D)
|
Generated points. |
numpy.array (1D)
|
Corresponding labels. |
Examples:
>>> from imitatebias.generators import *
>>> from imitatebias.imitate import *
>>> import matplotlib.pyplot as plt
Generate a dataset.
>>> X, y = generateData(1000, 2, 2, seed=2210)
Generate a biased dataset.
>>> X_b, y_b, idcs_deleted = generateBias(X, y, 1, seed=2210)
initialize Imitate
>>> imi = Imitate()
fit Imitate to the biased dataset
>>> imi.fit(X_b, labels=y_b)
augment the dataset
>>> X_gen, y_gen = imi.augment()
visualize data per cluster in ICA space
>>> plt.scatter(X_b[:,0], X_b[:,1], c=y_b)
>>> plt.scatter(X_gen[:,0], X_gen[:,1], c=y_gen, edgecolors='red')
>>> plt.title('Dataset with generated points (red)')
>>> plt.show()
Source code in imitatebias\imitate.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
|
fit(data, labels=[], bounds={}, bounds_set=None, strength=1000)
Fits a bias-aware multivariate Gaussian per label to the data.
Given a dataset and a potential label array, Imitate splits the data per
class and operates on each subset individually. For each of those labels,
fit fits a multivariate Gaussian to the subset that accounts for potential
biases. See our paper [1]_ for details.
Custom borders can be defined that constrain the fitting process. The strength
parameter controls how strongly these borders are enforced (the non-bounded
version uses strength=1
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
numpy.ndarray (2D)
|
Potentially biased input dataset. |
required |
labels |
numpy.array (1D), optional
|
Labels corresponding to the dataset if available. |
[]
|
bounds |
dict(string or int or float
|
Bounds Imitate if provided, for each label, in the shape
|
{}
|
bounds_set |
numpy.ndarray (2D)
|
If Imitate should be bounded to the ranges of a certain dataset, this set
can be passed to it directly. Will be overwritten by |
None
|
strength |
int, default
|
Controls how strongly the bounds are enforced. Will be ignored if no bounds are specified. |
1000
|
References
.. [1] Katharina Dost, Katerina Taskova, Patricia Riddle, and Jörg Wicker. "Your Best Guess When You Know Nothing: Identification and Mitigation of Selection Bias." In: 2020 IEEE International Conference on Data Mining (ICDM), pp. 996-1001, IEEE, 2020, ISSN: 2374-8486.
Examples:
>>> from imitatebias.generators import *
>>> from imitatebias.imitate import *
>>> import matplotlib.pyplot as plt
Generate a dataset.
>>> X, y = generateData(1000, 2, 2, seed=2210)
Generate a biased dataset.
>>> X_b, y_b, idcs_deleted = generateBias(X, y, 1, seed=2210)
initialize Imitate
>>> imi = Imitate()
fit Imitate to the biased dataset
>>> imi.fit(X_b, labels=y_b)
visualize data per cluster in ICA space
>>> for l in np.unique(y_b):
>>> data_transformed = imi.icas[l].transform(X_b[y_b == l])
>>> plt.scatter(data_transformed[:,0], data_transformed[:,1])
>>> plt.title('Class '+str(l))
>>> plt.show()
Source code in imitatebias\imitate.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
score(data, score_type='fill')
Scores new data based on Imitate's fitted Gaussian.
Imitate fits one multivariate Gaussian per label in a dataset. Scores are obtained via the difference of those Gaussians' PDFs and the input data (represented via a KDE estimate). See our paper [1]_ for details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
numpy.ndarray (2D)
|
Data that shall be scored. This dataset does not need to match the input data, but it is required to have the same dimensionality. |
required |
score_type |
Selects the type of score. |
'fill'
|
Returns:
Type | Description |
---|---|
np.ndarray (2D)
|
Score (i,j) corresponds to data point D_i and input data label j. |
References
.. [1] Katharina Dost, Katerina Taskova, Patricia Riddle, and Jörg Wicker. "Your Best Guess When You Know Nothing: Identification and Mitigation of Selection Bias." In: 2020 IEEE International Conference on Data Mining (ICDM), pp. 996-1001, IEEE, 2020, ISSN: 2374-8486.
.. [2] Katharina Dost, Hamish Duncanson, Ioannis Ziogas, Patricia Riddle, and Jörg Wicker. "Divide and Imitate: Multi-Cluster Identification and Mitigation of Selection Bias." In: Advances in Knowledge Discovery and Data Mining - 26th Pacific-Asia Conference, PAKDD 2022. Lecture Notes in Computer Science, vol. 13281, pp. 149-160. Springer, Cham (2022).
Examples:
>>> from imitatebias.generators import *
>>> from imitatebias.imitate import *
>>> import matplotlib.pyplot as plt
Generate a dataset.
>>> X, y = generateData(1000, 2, 2, seed=2210)
Generate a biased dataset.
>>> X_b, y_b, idcs_deleted = generateBias(X, y, 1, seed=2210)
initialize Imitate
>>> imi = Imitate()
fit Imitate to the biased dataset
>>> imi.fit(X_b, labels=y_b)
create some random points to score
>>> rnd_points = np.column_stack((np.random.uniform(min(X[:,0]), max(X[:,0]), size=1000), >>> np.random.uniform(min(X[:,1]), max(X[:,1]), size=1000)))
score the random points
>>> scores_fill = imi.score(rnd_points, score_type='fill')
>>> scores_balanced = imi.score(rnd_points, score_type='balanced')
visualize data per cluster in ICA space
>>> for l in np.unique(y_b):
>>> plt.scatter(rnd_points[:,0], rnd_points[:,1], c=scores_fill[:,int(l)])
>>> plt.title('Class '+str(l)+'; Score type = fill')
>>> plt.colorbar()
>>> plt.show()
>>> plt.scatter(rnd_points[:,0], rnd_points[:,1], c=scores_balanced[:,int(l)])
>>> plt.title('Class '+str(l)+'; Score type = balanced')
>>> plt.colorbar()
>>> plt.show()
Source code in imitatebias\imitate.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
|