File size: 68,404 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 |
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
import numbers
import warnings
from numbers import Integral
import numpy as np
from scipy import sparse
from ..base import BaseEstimator, OneToOneFeatureMixin, TransformerMixin, _fit_context
from ..utils import _safe_indexing, check_array
from ..utils._encode import _check_unknown, _encode, _get_counts, _unique
from ..utils._mask import _get_mask
from ..utils._missing import is_scalar_nan
from ..utils._param_validation import Interval, RealNotInt, StrOptions
from ..utils._set_output import _get_output_config
from ..utils.validation import (
_check_feature_names,
_check_feature_names_in,
_check_n_features,
check_is_fitted,
)
__all__ = ["OneHotEncoder", "OrdinalEncoder"]
class _BaseEncoder(TransformerMixin, BaseEstimator):
"""
Base class for encoders that includes the code to categorize and
transform the input features.
"""
def _check_X(self, X, ensure_all_finite=True):
"""
Perform custom check_array:
- convert list of strings to object dtype
- check for missing values for object dtype data (check_array does
not do that)
- return list of features (arrays): this list of features is
constructed feature by feature to preserve the data types
of pandas DataFrame columns, as otherwise information is lost
and cannot be used, e.g. for the `categories_` attribute.
"""
if not (hasattr(X, "iloc") and getattr(X, "ndim", 0) == 2):
# if not a dataframe, do normal check_array validation
X_temp = check_array(X, dtype=None, ensure_all_finite=ensure_all_finite)
if not hasattr(X, "dtype") and np.issubdtype(X_temp.dtype, np.str_):
X = check_array(X, dtype=object, ensure_all_finite=ensure_all_finite)
else:
X = X_temp
needs_validation = False
else:
# pandas dataframe, do validation later column by column, in order
# to keep the dtype information to be used in the encoder.
needs_validation = ensure_all_finite
n_samples, n_features = X.shape
X_columns = []
for i in range(n_features):
Xi = _safe_indexing(X, indices=i, axis=1)
Xi = check_array(
Xi, ensure_2d=False, dtype=None, ensure_all_finite=needs_validation
)
X_columns.append(Xi)
return X_columns, n_samples, n_features
def _fit(
self,
X,
handle_unknown="error",
ensure_all_finite=True,
return_counts=False,
return_and_ignore_missing_for_infrequent=False,
):
self._check_infrequent_enabled()
_check_n_features(self, X, reset=True)
_check_feature_names(self, X, reset=True)
X_list, n_samples, n_features = self._check_X(
X, ensure_all_finite=ensure_all_finite
)
self.n_features_in_ = n_features
if self.categories != "auto":
if len(self.categories) != n_features:
raise ValueError(
"Shape mismatch: if categories is an array,"
" it has to be of shape (n_features,)."
)
self.categories_ = []
category_counts = []
compute_counts = return_counts or self._infrequent_enabled
for i in range(n_features):
Xi = X_list[i]
if self.categories == "auto":
result = _unique(Xi, return_counts=compute_counts)
if compute_counts:
cats, counts = result
category_counts.append(counts)
else:
cats = result
else:
if np.issubdtype(Xi.dtype, np.str_):
# Always convert string categories to objects to avoid
# unexpected string truncation for longer category labels
# passed in the constructor.
Xi_dtype = object
else:
Xi_dtype = Xi.dtype
cats = np.array(self.categories[i], dtype=Xi_dtype)
if (
cats.dtype == object
and isinstance(cats[0], bytes)
and Xi.dtype.kind != "S"
):
msg = (
f"In column {i}, the predefined categories have type 'bytes'"
" which is incompatible with values of type"
f" '{type(Xi[0]).__name__}'."
)
raise ValueError(msg)
# `nan` must be the last stated category
for category in cats[:-1]:
if is_scalar_nan(category):
raise ValueError(
"Nan should be the last element in user"
f" provided categories, see categories {cats}"
f" in column #{i}"
)
if cats.size != len(_unique(cats)):
msg = (
f"In column {i}, the predefined categories"
" contain duplicate elements."
)
raise ValueError(msg)
if Xi.dtype.kind not in "OUS":
sorted_cats = np.sort(cats)
error_msg = (
"Unsorted categories are not supported for numerical categories"
)
# if there are nans, nan should be the last element
stop_idx = -1 if np.isnan(sorted_cats[-1]) else None
if np.any(sorted_cats[:stop_idx] != cats[:stop_idx]):
raise ValueError(error_msg)
if handle_unknown == "error":
diff = _check_unknown(Xi, cats)
if diff:
msg = (
"Found unknown categories {0} in column {1}"
" during fit".format(diff, i)
)
raise ValueError(msg)
if compute_counts:
category_counts.append(_get_counts(Xi, cats))
self.categories_.append(cats)
output = {"n_samples": n_samples}
if return_counts:
output["category_counts"] = category_counts
missing_indices = {}
if return_and_ignore_missing_for_infrequent:
for feature_idx, categories_for_idx in enumerate(self.categories_):
if is_scalar_nan(categories_for_idx[-1]):
# `nan` values can only be placed in the latest position
missing_indices[feature_idx] = categories_for_idx.size - 1
output["missing_indices"] = missing_indices
if self._infrequent_enabled:
self._fit_infrequent_category_mapping(
n_samples,
category_counts,
missing_indices,
)
return output
def _transform(
self,
X,
handle_unknown="error",
ensure_all_finite=True,
warn_on_unknown=False,
ignore_category_indices=None,
):
X_list, n_samples, n_features = self._check_X(
X, ensure_all_finite=ensure_all_finite
)
_check_feature_names(self, X, reset=False)
_check_n_features(self, X, reset=False)
X_int = np.zeros((n_samples, n_features), dtype=int)
X_mask = np.ones((n_samples, n_features), dtype=bool)
columns_with_unknown = []
for i in range(n_features):
Xi = X_list[i]
diff, valid_mask = _check_unknown(Xi, self.categories_[i], return_mask=True)
if not np.all(valid_mask):
if handle_unknown == "error":
msg = (
"Found unknown categories {0} in column {1}"
" during transform".format(diff, i)
)
raise ValueError(msg)
else:
if warn_on_unknown:
columns_with_unknown.append(i)
# Set the problematic rows to an acceptable value and
# continue `The rows are marked `X_mask` and will be
# removed later.
X_mask[:, i] = valid_mask
# cast Xi into the largest string type necessary
# to handle different lengths of numpy strings
if (
self.categories_[i].dtype.kind in ("U", "S")
and self.categories_[i].itemsize > Xi.itemsize
):
Xi = Xi.astype(self.categories_[i].dtype)
elif self.categories_[i].dtype.kind == "O" and Xi.dtype.kind == "U":
# categories are objects and Xi are numpy strings.
# Cast Xi to an object dtype to prevent truncation
# when setting invalid values.
Xi = Xi.astype("O")
else:
Xi = Xi.copy()
Xi[~valid_mask] = self.categories_[i][0]
# We use check_unknown=False, since _check_unknown was
# already called above.
X_int[:, i] = _encode(Xi, uniques=self.categories_[i], check_unknown=False)
if columns_with_unknown:
warnings.warn(
(
"Found unknown categories in columns "
f"{columns_with_unknown} during transform. These "
"unknown categories will be encoded as all zeros"
),
UserWarning,
)
self._map_infrequent_categories(X_int, X_mask, ignore_category_indices)
return X_int, X_mask
@property
def infrequent_categories_(self):
"""Infrequent categories for each feature."""
# raises an AttributeError if `_infrequent_indices` is not defined
infrequent_indices = self._infrequent_indices
return [
None if indices is None else category[indices]
for category, indices in zip(self.categories_, infrequent_indices)
]
def _check_infrequent_enabled(self):
"""
This functions checks whether _infrequent_enabled is True or False.
This has to be called after parameter validation in the fit function.
"""
max_categories = getattr(self, "max_categories", None)
min_frequency = getattr(self, "min_frequency", None)
self._infrequent_enabled = (
max_categories is not None and max_categories >= 1
) or min_frequency is not None
def _identify_infrequent(self, category_count, n_samples, col_idx):
"""Compute the infrequent indices.
Parameters
----------
category_count : ndarray of shape (n_cardinality,)
Category counts.
n_samples : int
Number of samples.
col_idx : int
Index of the current category. Only used for the error message.
Returns
-------
output : ndarray of shape (n_infrequent_categories,) or None
If there are infrequent categories, indices of infrequent
categories. Otherwise None.
"""
if isinstance(self.min_frequency, numbers.Integral):
infrequent_mask = category_count < self.min_frequency
elif isinstance(self.min_frequency, numbers.Real):
min_frequency_abs = n_samples * self.min_frequency
infrequent_mask = category_count < min_frequency_abs
else:
infrequent_mask = np.zeros(category_count.shape[0], dtype=bool)
n_current_features = category_count.size - infrequent_mask.sum() + 1
if self.max_categories is not None and self.max_categories < n_current_features:
# max_categories includes the one infrequent category
frequent_category_count = self.max_categories - 1
if frequent_category_count == 0:
# All categories are infrequent
infrequent_mask[:] = True
else:
# stable sort to preserve original count order
smallest_levels = np.argsort(category_count, kind="mergesort")[
:-frequent_category_count
]
infrequent_mask[smallest_levels] = True
output = np.flatnonzero(infrequent_mask)
return output if output.size > 0 else None
def _fit_infrequent_category_mapping(
self, n_samples, category_counts, missing_indices
):
"""Fit infrequent categories.
Defines the private attribute: `_default_to_infrequent_mappings`. For
feature `i`, `_default_to_infrequent_mappings[i]` defines the mapping
from the integer encoding returned by `super().transform()` into
infrequent categories. If `_default_to_infrequent_mappings[i]` is None,
there were no infrequent categories in the training set.
For example if categories 0, 2 and 4 were frequent, while categories
1, 3, 5 were infrequent for feature 7, then these categories are mapped
to a single output:
`_default_to_infrequent_mappings[7] = array([0, 3, 1, 3, 2, 3])`
Defines private attribute: `_infrequent_indices`. `_infrequent_indices[i]`
is an array of indices such that
`categories_[i][_infrequent_indices[i]]` are all the infrequent category
labels. If the feature `i` has no infrequent categories
`_infrequent_indices[i]` is None.
.. versionadded:: 1.1
Parameters
----------
n_samples : int
Number of samples in training set.
category_counts: list of ndarray
`category_counts[i]` is the category counts corresponding to
`self.categories_[i]`.
missing_indices : dict
Dict mapping from feature_idx to category index with a missing value.
"""
# Remove missing value from counts, so it is not considered as infrequent
if missing_indices:
category_counts_ = []
for feature_idx, count in enumerate(category_counts):
if feature_idx in missing_indices:
category_counts_.append(
np.delete(count, missing_indices[feature_idx])
)
else:
category_counts_.append(count)
else:
category_counts_ = category_counts
self._infrequent_indices = [
self._identify_infrequent(category_count, n_samples, col_idx)
for col_idx, category_count in enumerate(category_counts_)
]
# compute mapping from default mapping to infrequent mapping
self._default_to_infrequent_mappings = []
for feature_idx, infreq_idx in enumerate(self._infrequent_indices):
cats = self.categories_[feature_idx]
# no infrequent categories
if infreq_idx is None:
self._default_to_infrequent_mappings.append(None)
continue
n_cats = len(cats)
if feature_idx in missing_indices:
# Missing index was removed from this category when computing
# infrequent indices, thus we need to decrease the number of
# total categories when considering the infrequent mapping.
n_cats -= 1
# infrequent indices exist
mapping = np.empty(n_cats, dtype=np.int64)
n_infrequent_cats = infreq_idx.size
# infrequent categories are mapped to the last element.
n_frequent_cats = n_cats - n_infrequent_cats
mapping[infreq_idx] = n_frequent_cats
frequent_indices = np.setdiff1d(np.arange(n_cats), infreq_idx)
mapping[frequent_indices] = np.arange(n_frequent_cats)
self._default_to_infrequent_mappings.append(mapping)
def _map_infrequent_categories(self, X_int, X_mask, ignore_category_indices):
"""Map infrequent categories to integer representing the infrequent category.
This modifies X_int in-place. Values that were invalid based on `X_mask`
are mapped to the infrequent category if there was an infrequent
category for that feature.
Parameters
----------
X_int: ndarray of shape (n_samples, n_features)
Integer encoded categories.
X_mask: ndarray of shape (n_samples, n_features)
Bool mask for valid values in `X_int`.
ignore_category_indices : dict
Dictionary mapping from feature_idx to category index to ignore.
Ignored indexes will not be grouped and the original ordinal encoding
will remain.
"""
if not self._infrequent_enabled:
return
ignore_category_indices = ignore_category_indices or {}
for col_idx in range(X_int.shape[1]):
infrequent_idx = self._infrequent_indices[col_idx]
if infrequent_idx is None:
continue
X_int[~X_mask[:, col_idx], col_idx] = infrequent_idx[0]
if self.handle_unknown == "infrequent_if_exist":
# All the unknown values are now mapped to the
# infrequent_idx[0], which makes the unknown values valid
# This is needed in `transform` when the encoding is formed
# using `X_mask`.
X_mask[:, col_idx] = True
# Remaps encoding in `X_int` where the infrequent categories are
# grouped together.
for i, mapping in enumerate(self._default_to_infrequent_mappings):
if mapping is None:
continue
if i in ignore_category_indices:
# Update rows that are **not** ignored
rows_to_update = X_int[:, i] != ignore_category_indices[i]
else:
rows_to_update = slice(None)
X_int[rows_to_update, i] = np.take(mapping, X_int[rows_to_update, i])
def __sklearn_tags__(self):
tags = super().__sklearn_tags__()
tags.input_tags.categorical = True
tags.input_tags.allow_nan = True
return tags
class OneHotEncoder(_BaseEncoder):
"""
Encode categorical features as a one-hot numeric array.
The input to this transformer should be an array-like of integers or
strings, denoting the values taken on by categorical (discrete) features.
The features are encoded using a one-hot (aka 'one-of-K' or 'dummy')
encoding scheme. This creates a binary column for each category and
returns a sparse matrix or dense array (depending on the ``sparse_output``
parameter).
By default, the encoder derives the categories based on the unique values
in each feature. Alternatively, you can also specify the `categories`
manually.
This encoding is needed for feeding categorical data to many scikit-learn
estimators, notably linear models and SVMs with the standard kernels.
Note: a one-hot encoding of y labels should use a LabelBinarizer
instead.
Read more in the :ref:`User Guide <preprocessing_categorical_features>`.
For a comparison of different encoders, refer to:
:ref:`sphx_glr_auto_examples_preprocessing_plot_target_encoder.py`.
Parameters
----------
categories : 'auto' or a list of array-like, default='auto'
Categories (unique values) per feature:
- 'auto' : Determine categories automatically from the training data.
- list : ``categories[i]`` holds the categories expected in the ith
column. The passed categories should not mix strings and numeric
values within a single feature, and should be sorted in case of
numeric values.
The used categories can be found in the ``categories_`` attribute.
.. versionadded:: 0.20
drop : {'first', 'if_binary'} or an array-like of shape (n_features,), \
default=None
Specifies a methodology to use to drop one of the categories per
feature. This is useful in situations where perfectly collinear
features cause problems, such as when feeding the resulting data
into an unregularized linear regression model.
However, dropping one category breaks the symmetry of the original
representation and can therefore induce a bias in downstream models,
for instance for penalized linear classification or regression models.
- None : retain all features (the default).
- 'first' : drop the first category in each feature. If only one
category is present, the feature will be dropped entirely.
- 'if_binary' : drop the first category in each feature with two
categories. Features with 1 or more than 2 categories are
left intact.
- array : ``drop[i]`` is the category in feature ``X[:, i]`` that
should be dropped.
When `max_categories` or `min_frequency` is configured to group
infrequent categories, the dropping behavior is handled after the
grouping.
.. versionadded:: 0.21
The parameter `drop` was added in 0.21.
.. versionchanged:: 0.23
The option `drop='if_binary'` was added in 0.23.
.. versionchanged:: 1.1
Support for dropping infrequent categories.
sparse_output : bool, default=True
When ``True``, it returns a :class:`scipy.sparse.csr_matrix`,
i.e. a sparse matrix in "Compressed Sparse Row" (CSR) format.
.. versionadded:: 1.2
`sparse` was renamed to `sparse_output`
dtype : number type, default=np.float64
Desired dtype of output.
handle_unknown : {'error', 'ignore', 'infrequent_if_exist', 'warn'}, \
default='error'
Specifies the way unknown categories are handled during :meth:`transform`.
- 'error' : Raise an error if an unknown category is present during transform.
- 'ignore' : When an unknown category is encountered during
transform, the resulting one-hot encoded columns for this feature
will be all zeros. In the inverse transform, an unknown category
will be denoted as None.
- 'infrequent_if_exist' : When an unknown category is encountered
during transform, the resulting one-hot encoded columns for this
feature will map to the infrequent category if it exists. The
infrequent category will be mapped to the last position in the
encoding. During inverse transform, an unknown category will be
mapped to the category denoted `'infrequent'` if it exists. If the
`'infrequent'` category does not exist, then :meth:`transform` and
:meth:`inverse_transform` will handle an unknown category as with
`handle_unknown='ignore'`. Infrequent categories exist based on
`min_frequency` and `max_categories`. Read more in the
:ref:`User Guide <encoder_infrequent_categories>`.
- 'warn' : When an unknown category is encountered during transform
a warning is issued, and the encoding then proceeds as described for
`handle_unknown="infrequent_if_exist"`.
.. versionchanged:: 1.1
`'infrequent_if_exist'` was added to automatically handle unknown
categories and infrequent categories.
.. versionadded:: 1.6
The option `"warn"` was added in 1.6.
min_frequency : int or float, default=None
Specifies the minimum frequency below which a category will be
considered infrequent.
- If `int`, categories with a smaller cardinality will be considered
infrequent.
- If `float`, categories with a smaller cardinality than
`min_frequency * n_samples` will be considered infrequent.
.. versionadded:: 1.1
Read more in the :ref:`User Guide <encoder_infrequent_categories>`.
max_categories : int, default=None
Specifies an upper limit to the number of output features for each input
feature when considering infrequent categories. If there are infrequent
categories, `max_categories` includes the category representing the
infrequent categories along with the frequent categories. If `None`,
there is no limit to the number of output features.
.. versionadded:: 1.1
Read more in the :ref:`User Guide <encoder_infrequent_categories>`.
feature_name_combiner : "concat" or callable, default="concat"
Callable with signature `def callable(input_feature, category)` that returns a
string. This is used to create feature names to be returned by
:meth:`get_feature_names_out`.
`"concat"` concatenates encoded feature name and category with
`feature + "_" + str(category)`.E.g. feature X with values 1, 6, 7 create
feature names `X_1, X_6, X_7`.
.. versionadded:: 1.3
Attributes
----------
categories_ : list of arrays
The categories of each feature determined during fitting
(in order of the features in X and corresponding with the output
of ``transform``). This includes the category specified in ``drop``
(if any).
drop_idx_ : array of shape (n_features,)
- ``drop_idx_[i]`` is the index in ``categories_[i]`` of the category
to be dropped for each feature.
- ``drop_idx_[i] = None`` if no category is to be dropped from the
feature with index ``i``, e.g. when `drop='if_binary'` and the
feature isn't binary.
- ``drop_idx_ = None`` if all the transformed features will be
retained.
If infrequent categories are enabled by setting `min_frequency` or
`max_categories` to a non-default value and `drop_idx[i]` corresponds
to a infrequent category, then the entire infrequent category is
dropped.
.. versionchanged:: 0.23
Added the possibility to contain `None` values.
infrequent_categories_ : list of ndarray
Defined only if infrequent categories are enabled by setting
`min_frequency` or `max_categories` to a non-default value.
`infrequent_categories_[i]` are the infrequent categories for feature
`i`. If the feature `i` has no infrequent categories
`infrequent_categories_[i]` is None.
.. versionadded:: 1.1
n_features_in_ : int
Number of features seen during :term:`fit`.
.. versionadded:: 1.0
feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
.. versionadded:: 1.0
feature_name_combiner : callable or None
Callable with signature `def callable(input_feature, category)` that returns a
string. This is used to create feature names to be returned by
:meth:`get_feature_names_out`.
.. versionadded:: 1.3
See Also
--------
OrdinalEncoder : Performs an ordinal (integer)
encoding of the categorical features.
TargetEncoder : Encodes categorical features using the target.
sklearn.feature_extraction.DictVectorizer : Performs a one-hot encoding of
dictionary items (also handles string-valued features).
sklearn.feature_extraction.FeatureHasher : Performs an approximate one-hot
encoding of dictionary items or strings.
LabelBinarizer : Binarizes labels in a one-vs-all
fashion.
MultiLabelBinarizer : Transforms between iterable of
iterables and a multilabel format, e.g. a (samples x classes) binary
matrix indicating the presence of a class label.
Examples
--------
Given a dataset with two features, we let the encoder find the unique
values per feature and transform the data to a binary one-hot encoding.
>>> from sklearn.preprocessing import OneHotEncoder
One can discard categories not seen during `fit`:
>>> enc = OneHotEncoder(handle_unknown='ignore')
>>> X = [['Male', 1], ['Female', 3], ['Female', 2]]
>>> enc.fit(X)
OneHotEncoder(handle_unknown='ignore')
>>> enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> enc.transform([['Female', 1], ['Male', 4]]).toarray()
array([[1., 0., 1., 0., 0.],
[0., 1., 0., 0., 0.]])
>>> enc.inverse_transform([[0, 1, 1, 0, 0], [0, 0, 0, 1, 0]])
array([['Male', 1],
[None, 2]], dtype=object)
>>> enc.get_feature_names_out(['gender', 'group'])
array(['gender_Female', 'gender_Male', 'group_1', 'group_2', 'group_3'], ...)
One can always drop the first column for each feature:
>>> drop_enc = OneHotEncoder(drop='first').fit(X)
>>> drop_enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> drop_enc.transform([['Female', 1], ['Male', 2]]).toarray()
array([[0., 0., 0.],
[1., 1., 0.]])
Or drop a column for feature only having 2 categories:
>>> drop_binary_enc = OneHotEncoder(drop='if_binary').fit(X)
>>> drop_binary_enc.transform([['Female', 1], ['Male', 2]]).toarray()
array([[0., 1., 0., 0.],
[1., 0., 1., 0.]])
One can change the way feature names are created.
>>> def custom_combiner(feature, category):
... return str(feature) + "_" + type(category).__name__ + "_" + str(category)
>>> custom_fnames_enc = OneHotEncoder(feature_name_combiner=custom_combiner).fit(X)
>>> custom_fnames_enc.get_feature_names_out()
array(['x0_str_Female', 'x0_str_Male', 'x1_int_1', 'x1_int_2', 'x1_int_3'],
dtype=object)
Infrequent categories are enabled by setting `max_categories` or `min_frequency`.
>>> import numpy as np
>>> X = np.array([["a"] * 5 + ["b"] * 20 + ["c"] * 10 + ["d"] * 3], dtype=object).T
>>> ohe = OneHotEncoder(max_categories=3, sparse_output=False).fit(X)
>>> ohe.infrequent_categories_
[array(['a', 'd'], dtype=object)]
>>> ohe.transform([["a"], ["b"]])
array([[0., 0., 1.],
[1., 0., 0.]])
"""
_parameter_constraints: dict = {
"categories": [StrOptions({"auto"}), list],
"drop": [StrOptions({"first", "if_binary"}), "array-like", None],
"dtype": "no_validation", # validation delegated to numpy
"handle_unknown": [
StrOptions({"error", "ignore", "infrequent_if_exist", "warn"})
],
"max_categories": [Interval(Integral, 1, None, closed="left"), None],
"min_frequency": [
Interval(Integral, 1, None, closed="left"),
Interval(RealNotInt, 0, 1, closed="neither"),
None,
],
"sparse_output": ["boolean"],
"feature_name_combiner": [StrOptions({"concat"}), callable],
}
def __init__(
self,
*,
categories="auto",
drop=None,
sparse_output=True,
dtype=np.float64,
handle_unknown="error",
min_frequency=None,
max_categories=None,
feature_name_combiner="concat",
):
self.categories = categories
self.sparse_output = sparse_output
self.dtype = dtype
self.handle_unknown = handle_unknown
self.drop = drop
self.min_frequency = min_frequency
self.max_categories = max_categories
self.feature_name_combiner = feature_name_combiner
def _map_drop_idx_to_infrequent(self, feature_idx, drop_idx):
"""Convert `drop_idx` into the index for infrequent categories.
If there are no infrequent categories, then `drop_idx` is
returned. This method is called in `_set_drop_idx` when the `drop`
parameter is an array-like.
"""
if not self._infrequent_enabled:
return drop_idx
default_to_infrequent = self._default_to_infrequent_mappings[feature_idx]
if default_to_infrequent is None:
return drop_idx
# Raise error when explicitly dropping a category that is infrequent
infrequent_indices = self._infrequent_indices[feature_idx]
if infrequent_indices is not None and drop_idx in infrequent_indices:
categories = self.categories_[feature_idx]
raise ValueError(
f"Unable to drop category {categories[drop_idx].item()!r} from"
f" feature {feature_idx} because it is infrequent"
)
return default_to_infrequent[drop_idx]
def _set_drop_idx(self):
"""Compute the drop indices associated with `self.categories_`.
If `self.drop` is:
- `None`, No categories have been dropped.
- `'first'`, All zeros to drop the first category.
- `'if_binary'`, All zeros if the category is binary and `None`
otherwise.
- array-like, The indices of the categories that match the
categories in `self.drop`. If the dropped category is an infrequent
category, then the index for the infrequent category is used. This
means that the entire infrequent category is dropped.
This methods defines a public `drop_idx_` and a private
`_drop_idx_after_grouping`.
- `drop_idx_`: Public facing API that references the drop category in
`self.categories_`.
- `_drop_idx_after_grouping`: Used internally to drop categories *after* the
infrequent categories are grouped together.
If there are no infrequent categories or drop is `None`, then
`drop_idx_=_drop_idx_after_grouping`.
"""
if self.drop is None:
drop_idx_after_grouping = None
elif isinstance(self.drop, str):
if self.drop == "first":
drop_idx_after_grouping = np.zeros(len(self.categories_), dtype=object)
elif self.drop == "if_binary":
n_features_out_no_drop = [len(cat) for cat in self.categories_]
if self._infrequent_enabled:
for i, infreq_idx in enumerate(self._infrequent_indices):
if infreq_idx is None:
continue
n_features_out_no_drop[i] -= infreq_idx.size - 1
drop_idx_after_grouping = np.array(
[
0 if n_features_out == 2 else None
for n_features_out in n_features_out_no_drop
],
dtype=object,
)
else:
drop_array = np.asarray(self.drop, dtype=object)
droplen = len(drop_array)
if droplen != len(self.categories_):
msg = (
"`drop` should have length equal to the number "
"of features ({}), got {}"
)
raise ValueError(msg.format(len(self.categories_), droplen))
missing_drops = []
drop_indices = []
for feature_idx, (drop_val, cat_list) in enumerate(
zip(drop_array, self.categories_)
):
if not is_scalar_nan(drop_val):
drop_idx = np.where(cat_list == drop_val)[0]
if drop_idx.size: # found drop idx
drop_indices.append(
self._map_drop_idx_to_infrequent(feature_idx, drop_idx[0])
)
else:
missing_drops.append((feature_idx, drop_val))
continue
# drop_val is nan, find nan in categories manually
if is_scalar_nan(cat_list[-1]):
drop_indices.append(
self._map_drop_idx_to_infrequent(feature_idx, cat_list.size - 1)
)
else: # nan is missing
missing_drops.append((feature_idx, drop_val))
if any(missing_drops):
msg = (
"The following categories were supposed to be "
"dropped, but were not found in the training "
"data.\n{}".format(
"\n".join(
[
"Category: {}, Feature: {}".format(c, v)
for c, v in missing_drops
]
)
)
)
raise ValueError(msg)
drop_idx_after_grouping = np.array(drop_indices, dtype=object)
# `_drop_idx_after_grouping` are the categories to drop *after* the infrequent
# categories are grouped together. If needed, we remap `drop_idx` back
# to the categories seen in `self.categories_`.
self._drop_idx_after_grouping = drop_idx_after_grouping
if not self._infrequent_enabled or drop_idx_after_grouping is None:
self.drop_idx_ = self._drop_idx_after_grouping
else:
drop_idx_ = []
for feature_idx, drop_idx in enumerate(drop_idx_after_grouping):
default_to_infrequent = self._default_to_infrequent_mappings[
feature_idx
]
if drop_idx is None or default_to_infrequent is None:
orig_drop_idx = drop_idx
else:
orig_drop_idx = np.flatnonzero(default_to_infrequent == drop_idx)[0]
drop_idx_.append(orig_drop_idx)
self.drop_idx_ = np.asarray(drop_idx_, dtype=object)
def _compute_transformed_categories(self, i, remove_dropped=True):
"""Compute the transformed categories used for column `i`.
1. If there are infrequent categories, the category is named
'infrequent_sklearn'.
2. Dropped columns are removed when remove_dropped=True.
"""
cats = self.categories_[i]
if self._infrequent_enabled:
infreq_map = self._default_to_infrequent_mappings[i]
if infreq_map is not None:
frequent_mask = infreq_map < infreq_map.max()
infrequent_cat = "infrequent_sklearn"
# infrequent category is always at the end
cats = np.concatenate(
(cats[frequent_mask], np.array([infrequent_cat], dtype=object))
)
if remove_dropped:
cats = self._remove_dropped_categories(cats, i)
return cats
def _remove_dropped_categories(self, categories, i):
"""Remove dropped categories."""
if (
self._drop_idx_after_grouping is not None
and self._drop_idx_after_grouping[i] is not None
):
return np.delete(categories, self._drop_idx_after_grouping[i])
return categories
def _compute_n_features_outs(self):
"""Compute the n_features_out for each input feature."""
output = [len(cats) for cats in self.categories_]
if self._drop_idx_after_grouping is not None:
for i, drop_idx in enumerate(self._drop_idx_after_grouping):
if drop_idx is not None:
output[i] -= 1
if not self._infrequent_enabled:
return output
# infrequent is enabled, the number of features out are reduced
# because the infrequent categories are grouped together
for i, infreq_idx in enumerate(self._infrequent_indices):
if infreq_idx is None:
continue
output[i] -= infreq_idx.size - 1
return output
@_fit_context(prefer_skip_nested_validation=True)
def fit(self, X, y=None):
"""
Fit OneHotEncoder to X.
Parameters
----------
X : array-like of shape (n_samples, n_features)
The data to determine the categories of each feature.
y : None
Ignored. This parameter exists only for compatibility with
:class:`~sklearn.pipeline.Pipeline`.
Returns
-------
self
Fitted encoder.
"""
self._fit(
X,
handle_unknown=self.handle_unknown,
ensure_all_finite="allow-nan",
)
self._set_drop_idx()
self._n_features_outs = self._compute_n_features_outs()
return self
def transform(self, X):
"""
Transform X using one-hot encoding.
If `sparse_output=True` (default), it returns an instance of
:class:`scipy.sparse._csr.csr_matrix` (CSR format).
If there are infrequent categories for a feature, set by specifying
`max_categories` or `min_frequency`, the infrequent categories are
grouped into a single category.
Parameters
----------
X : array-like of shape (n_samples, n_features)
The data to encode.
Returns
-------
X_out : {ndarray, sparse matrix} of shape \
(n_samples, n_encoded_features)
Transformed input. If `sparse_output=True`, a sparse matrix will be
returned.
"""
check_is_fitted(self)
transform_output = _get_output_config("transform", estimator=self)["dense"]
if transform_output != "default" and self.sparse_output:
capitalize_transform_output = transform_output.capitalize()
raise ValueError(
f"{capitalize_transform_output} output does not support sparse data."
f" Set sparse_output=False to output {transform_output} dataframes or"
f" disable {capitalize_transform_output} output via"
'` ohe.set_output(transform="default").'
)
# validation of X happens in _check_X called by _transform
if self.handle_unknown == "warn":
warn_on_unknown, handle_unknown = True, "infrequent_if_exist"
else:
warn_on_unknown = self.drop is not None and self.handle_unknown in {
"ignore",
"infrequent_if_exist",
}
handle_unknown = self.handle_unknown
X_int, X_mask = self._transform(
X,
handle_unknown=handle_unknown,
ensure_all_finite="allow-nan",
warn_on_unknown=warn_on_unknown,
)
n_samples, n_features = X_int.shape
if self._drop_idx_after_grouping is not None:
to_drop = self._drop_idx_after_grouping.copy()
# We remove all the dropped categories from mask, and decrement all
# categories that occur after them to avoid an empty column.
keep_cells = X_int != to_drop
for i, cats in enumerate(self.categories_):
# drop='if_binary' but feature isn't binary
if to_drop[i] is None:
# set to cardinality to not drop from X_int
to_drop[i] = len(cats)
to_drop = to_drop.reshape(1, -1)
X_int[X_int > to_drop] -= 1
X_mask &= keep_cells
mask = X_mask.ravel()
feature_indices = np.cumsum([0] + self._n_features_outs)
indices = (X_int + feature_indices[:-1]).ravel()[mask]
indptr = np.empty(n_samples + 1, dtype=int)
indptr[0] = 0
np.sum(X_mask, axis=1, out=indptr[1:], dtype=indptr.dtype)
np.cumsum(indptr[1:], out=indptr[1:])
data = np.ones(indptr[-1])
out = sparse.csr_matrix(
(data, indices, indptr),
shape=(n_samples, feature_indices[-1]),
dtype=self.dtype,
)
if not self.sparse_output:
return out.toarray()
else:
return out
def inverse_transform(self, X):
"""
Convert the data back to the original representation.
When unknown categories are encountered (all zeros in the
one-hot encoding), ``None`` is used to represent this category. If the
feature with the unknown category has a dropped category, the dropped
category will be its inverse.
For a given input feature, if there is an infrequent category,
'infrequent_sklearn' will be used to represent the infrequent category.
Parameters
----------
X : {array-like, sparse matrix} of shape \
(n_samples, n_encoded_features)
The transformed data.
Returns
-------
X_tr : ndarray of shape (n_samples, n_features)
Inverse transformed array.
"""
check_is_fitted(self)
X = check_array(X, accept_sparse="csr")
n_samples, _ = X.shape
n_features = len(self.categories_)
n_features_out = np.sum(self._n_features_outs)
# validate shape of passed X
msg = (
"Shape of the passed X data is not correct. Expected {0} columns, got {1}."
)
if X.shape[1] != n_features_out:
raise ValueError(msg.format(n_features_out, X.shape[1]))
transformed_features = [
self._compute_transformed_categories(i, remove_dropped=False)
for i, _ in enumerate(self.categories_)
]
# create resulting array of appropriate dtype
dt = np.result_type(*[cat.dtype for cat in transformed_features])
X_tr = np.empty((n_samples, n_features), dtype=dt)
j = 0
found_unknown = {}
if self._infrequent_enabled:
infrequent_indices = self._infrequent_indices
else:
infrequent_indices = [None] * n_features
for i in range(n_features):
cats_wo_dropped = self._remove_dropped_categories(
transformed_features[i], i
)
n_categories = cats_wo_dropped.shape[0]
# Only happens if there was a column with a unique
# category. In this case we just fill the column with this
# unique category value.
if n_categories == 0:
X_tr[:, i] = self.categories_[i][self._drop_idx_after_grouping[i]]
j += n_categories
continue
sub = X[:, j : j + n_categories]
# for sparse X argmax returns 2D matrix, ensure 1D array
labels = np.asarray(sub.argmax(axis=1)).flatten()
X_tr[:, i] = cats_wo_dropped[labels]
if self.handle_unknown == "ignore" or (
self.handle_unknown in ("infrequent_if_exist", "warn")
and infrequent_indices[i] is None
):
unknown = np.asarray(sub.sum(axis=1) == 0).flatten()
# ignored unknown categories: we have a row of all zero
if unknown.any():
# if categories were dropped then unknown categories will
# be mapped to the dropped category
if (
self._drop_idx_after_grouping is None
or self._drop_idx_after_grouping[i] is None
):
found_unknown[i] = unknown
else:
X_tr[unknown, i] = self.categories_[i][
self._drop_idx_after_grouping[i]
]
else:
dropped = np.asarray(sub.sum(axis=1) == 0).flatten()
if dropped.any():
if self._drop_idx_after_grouping is None:
all_zero_samples = np.flatnonzero(dropped)
raise ValueError(
f"Samples {all_zero_samples} can not be inverted "
"when drop=None and handle_unknown='error' "
"because they contain all zeros"
)
# we can safely assume that all of the nulls in each column
# are the dropped value
drop_idx = self._drop_idx_after_grouping[i]
X_tr[dropped, i] = transformed_features[i][drop_idx]
j += n_categories
# if ignored are found: potentially need to upcast result to
# insert None values
if found_unknown:
if X_tr.dtype != object:
X_tr = X_tr.astype(object)
for idx, mask in found_unknown.items():
X_tr[mask, idx] = None
return X_tr
def get_feature_names_out(self, input_features=None):
"""Get output feature names for transformation.
Parameters
----------
input_features : array-like of str or None, default=None
Input features.
- If `input_features` is `None`, then `feature_names_in_` is
used as feature names in. If `feature_names_in_` is not defined,
then the following input feature names are generated:
`["x0", "x1", ..., "x(n_features_in_ - 1)"]`.
- If `input_features` is an array-like, then `input_features` must
match `feature_names_in_` if `feature_names_in_` is defined.
Returns
-------
feature_names_out : ndarray of str objects
Transformed feature names.
"""
check_is_fitted(self)
input_features = _check_feature_names_in(self, input_features)
cats = [
self._compute_transformed_categories(i)
for i, _ in enumerate(self.categories_)
]
name_combiner = self._check_get_feature_name_combiner()
feature_names = []
for i in range(len(cats)):
names = [name_combiner(input_features[i], t) for t in cats[i]]
feature_names.extend(names)
return np.array(feature_names, dtype=object)
def _check_get_feature_name_combiner(self):
if self.feature_name_combiner == "concat":
return lambda feature, category: feature + "_" + str(category)
else: # callable
dry_run_combiner = self.feature_name_combiner("feature", "category")
if not isinstance(dry_run_combiner, str):
raise TypeError(
"When `feature_name_combiner` is a callable, it should return a "
f"Python string. Got {type(dry_run_combiner)} instead."
)
return self.feature_name_combiner
class OrdinalEncoder(OneToOneFeatureMixin, _BaseEncoder):
"""
Encode categorical features as an integer array.
The input to this transformer should be an array-like of integers or
strings, denoting the values taken on by categorical (discrete) features.
The features are converted to ordinal integers. This results in
a single column of integers (0 to n_categories - 1) per feature.
Read more in the :ref:`User Guide <preprocessing_categorical_features>`.
For a comparison of different encoders, refer to:
:ref:`sphx_glr_auto_examples_preprocessing_plot_target_encoder.py`.
.. versionadded:: 0.20
Parameters
----------
categories : 'auto' or a list of array-like, default='auto'
Categories (unique values) per feature:
- 'auto' : Determine categories automatically from the training data.
- list : ``categories[i]`` holds the categories expected in the ith
column. The passed categories should not mix strings and numeric
values, and should be sorted in case of numeric values.
The used categories can be found in the ``categories_`` attribute.
dtype : number type, default=np.float64
Desired dtype of output.
handle_unknown : {'error', 'use_encoded_value'}, default='error'
When set to 'error' an error will be raised in case an unknown
categorical feature is present during transform. When set to
'use_encoded_value', the encoded value of unknown categories will be
set to the value given for the parameter `unknown_value`. In
:meth:`inverse_transform`, an unknown category will be denoted as None.
.. versionadded:: 0.24
unknown_value : int or np.nan, default=None
When the parameter handle_unknown is set to 'use_encoded_value', this
parameter is required and will set the encoded value of unknown
categories. It has to be distinct from the values used to encode any of
the categories in `fit`. If set to np.nan, the `dtype` parameter must
be a float dtype.
.. versionadded:: 0.24
encoded_missing_value : int or np.nan, default=np.nan
Encoded value of missing categories. If set to `np.nan`, then the `dtype`
parameter must be a float dtype.
.. versionadded:: 1.1
min_frequency : int or float, default=None
Specifies the minimum frequency below which a category will be
considered infrequent.
- If `int`, categories with a smaller cardinality will be considered
infrequent.
- If `float`, categories with a smaller cardinality than
`min_frequency * n_samples` will be considered infrequent.
.. versionadded:: 1.3
Read more in the :ref:`User Guide <encoder_infrequent_categories>`.
max_categories : int, default=None
Specifies an upper limit to the number of output categories for each input
feature when considering infrequent categories. If there are infrequent
categories, `max_categories` includes the category representing the
infrequent categories along with the frequent categories. If `None`,
there is no limit to the number of output features.
`max_categories` do **not** take into account missing or unknown
categories. Setting `unknown_value` or `encoded_missing_value` to an
integer will increase the number of unique integer codes by one each.
This can result in up to `max_categories + 2` integer codes.
.. versionadded:: 1.3
Read more in the :ref:`User Guide <encoder_infrequent_categories>`.
Attributes
----------
categories_ : list of arrays
The categories of each feature determined during ``fit`` (in order of
the features in X and corresponding with the output of ``transform``).
This does not include categories that weren't seen during ``fit``.
n_features_in_ : int
Number of features seen during :term:`fit`.
.. versionadded:: 1.0
feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
.. versionadded:: 1.0
infrequent_categories_ : list of ndarray
Defined only if infrequent categories are enabled by setting
`min_frequency` or `max_categories` to a non-default value.
`infrequent_categories_[i]` are the infrequent categories for feature
`i`. If the feature `i` has no infrequent categories
`infrequent_categories_[i]` is None.
.. versionadded:: 1.3
See Also
--------
OneHotEncoder : Performs a one-hot encoding of categorical features. This encoding
is suitable for low to medium cardinality categorical variables, both in
supervised and unsupervised settings.
TargetEncoder : Encodes categorical features using supervised signal
in a classification or regression pipeline. This encoding is typically
suitable for high cardinality categorical variables.
LabelEncoder : Encodes target labels with values between 0 and
``n_classes-1``.
Notes
-----
With a high proportion of `nan` values, inferring categories becomes slow with
Python versions before 3.10. The handling of `nan` values was improved
from Python 3.10 onwards, (c.f.
`bpo-43475 <https://github.com/python/cpython/issues/87641>`_).
Examples
--------
Given a dataset with two features, we let the encoder find the unique
values per feature and transform the data to an ordinal encoding.
>>> from sklearn.preprocessing import OrdinalEncoder
>>> enc = OrdinalEncoder()
>>> X = [['Male', 1], ['Female', 3], ['Female', 2]]
>>> enc.fit(X)
OrdinalEncoder()
>>> enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> enc.transform([['Female', 3], ['Male', 1]])
array([[0., 2.],
[1., 0.]])
>>> enc.inverse_transform([[1, 0], [0, 1]])
array([['Male', 1],
['Female', 2]], dtype=object)
By default, :class:`OrdinalEncoder` is lenient towards missing values by
propagating them.
>>> import numpy as np
>>> X = [['Male', 1], ['Female', 3], ['Female', np.nan]]
>>> enc.fit_transform(X)
array([[ 1., 0.],
[ 0., 1.],
[ 0., nan]])
You can use the parameter `encoded_missing_value` to encode missing values.
>>> enc.set_params(encoded_missing_value=-1).fit_transform(X)
array([[ 1., 0.],
[ 0., 1.],
[ 0., -1.]])
Infrequent categories are enabled by setting `max_categories` or `min_frequency`.
In the following example, "a" and "d" are considered infrequent and grouped
together into a single category, "b" and "c" are their own categories, unknown
values are encoded as 3 and missing values are encoded as 4.
>>> X_train = np.array(
... [["a"] * 5 + ["b"] * 20 + ["c"] * 10 + ["d"] * 3 + [np.nan]],
... dtype=object).T
>>> enc = OrdinalEncoder(
... handle_unknown="use_encoded_value", unknown_value=3,
... max_categories=3, encoded_missing_value=4)
>>> _ = enc.fit(X_train)
>>> X_test = np.array([["a"], ["b"], ["c"], ["d"], ["e"], [np.nan]], dtype=object)
>>> enc.transform(X_test)
array([[2.],
[0.],
[1.],
[2.],
[3.],
[4.]])
"""
_parameter_constraints: dict = {
"categories": [StrOptions({"auto"}), list],
"dtype": "no_validation", # validation delegated to numpy
"encoded_missing_value": [Integral, type(np.nan)],
"handle_unknown": [StrOptions({"error", "use_encoded_value"})],
"unknown_value": [Integral, type(np.nan), None],
"max_categories": [Interval(Integral, 1, None, closed="left"), None],
"min_frequency": [
Interval(Integral, 1, None, closed="left"),
Interval(RealNotInt, 0, 1, closed="neither"),
None,
],
}
def __init__(
self,
*,
categories="auto",
dtype=np.float64,
handle_unknown="error",
unknown_value=None,
encoded_missing_value=np.nan,
min_frequency=None,
max_categories=None,
):
self.categories = categories
self.dtype = dtype
self.handle_unknown = handle_unknown
self.unknown_value = unknown_value
self.encoded_missing_value = encoded_missing_value
self.min_frequency = min_frequency
self.max_categories = max_categories
@_fit_context(prefer_skip_nested_validation=True)
def fit(self, X, y=None):
"""
Fit the OrdinalEncoder to X.
Parameters
----------
X : array-like of shape (n_samples, n_features)
The data to determine the categories of each feature.
y : None
Ignored. This parameter exists only for compatibility with
:class:`~sklearn.pipeline.Pipeline`.
Returns
-------
self : object
Fitted encoder.
"""
if self.handle_unknown == "use_encoded_value":
if is_scalar_nan(self.unknown_value):
if np.dtype(self.dtype).kind != "f":
raise ValueError(
"When unknown_value is np.nan, the dtype "
"parameter should be "
f"a float dtype. Got {self.dtype}."
)
elif not isinstance(self.unknown_value, numbers.Integral):
raise TypeError(
"unknown_value should be an integer or "
"np.nan when "
"handle_unknown is 'use_encoded_value', "
f"got {self.unknown_value}."
)
elif self.unknown_value is not None:
raise TypeError(
"unknown_value should only be set when "
"handle_unknown is 'use_encoded_value', "
f"got {self.unknown_value}."
)
# `_fit` will only raise an error when `self.handle_unknown="error"`
fit_results = self._fit(
X,
handle_unknown=self.handle_unknown,
ensure_all_finite="allow-nan",
return_and_ignore_missing_for_infrequent=True,
)
self._missing_indices = fit_results["missing_indices"]
cardinalities = [len(categories) for categories in self.categories_]
if self._infrequent_enabled:
# Cardinality decreases because the infrequent categories are grouped
# together
for feature_idx, infrequent in enumerate(self.infrequent_categories_):
if infrequent is not None:
cardinalities[feature_idx] -= len(infrequent)
# missing values are not considered part of the cardinality
# when considering unknown categories or encoded_missing_value
for cat_idx, categories_for_idx in enumerate(self.categories_):
if is_scalar_nan(categories_for_idx[-1]):
cardinalities[cat_idx] -= 1
if self.handle_unknown == "use_encoded_value":
for cardinality in cardinalities:
if 0 <= self.unknown_value < cardinality:
raise ValueError(
"The used value for unknown_value "
f"{self.unknown_value} is one of the "
"values already used for encoding the "
"seen categories."
)
if self._missing_indices:
if np.dtype(self.dtype).kind != "f" and is_scalar_nan(
self.encoded_missing_value
):
raise ValueError(
"There are missing values in features "
f"{list(self._missing_indices)}. For OrdinalEncoder to "
f"encode missing values with dtype: {self.dtype}, set "
"encoded_missing_value to a non-nan value, or "
"set dtype to a float"
)
if not is_scalar_nan(self.encoded_missing_value):
# Features are invalid when they contain a missing category
# and encoded_missing_value was already used to encode a
# known category
invalid_features = [
cat_idx
for cat_idx, cardinality in enumerate(cardinalities)
if cat_idx in self._missing_indices
and 0 <= self.encoded_missing_value < cardinality
]
if invalid_features:
# Use feature names if they are available
if hasattr(self, "feature_names_in_"):
invalid_features = self.feature_names_in_[invalid_features]
raise ValueError(
f"encoded_missing_value ({self.encoded_missing_value}) "
"is already used to encode a known category in features: "
f"{invalid_features}"
)
return self
def transform(self, X):
"""
Transform X to ordinal codes.
Parameters
----------
X : array-like of shape (n_samples, n_features)
The data to encode.
Returns
-------
X_out : ndarray of shape (n_samples, n_features)
Transformed input.
"""
check_is_fitted(self, "categories_")
X_int, X_mask = self._transform(
X,
handle_unknown=self.handle_unknown,
ensure_all_finite="allow-nan",
ignore_category_indices=self._missing_indices,
)
X_trans = X_int.astype(self.dtype, copy=False)
for cat_idx, missing_idx in self._missing_indices.items():
X_missing_mask = X_int[:, cat_idx] == missing_idx
X_trans[X_missing_mask, cat_idx] = self.encoded_missing_value
# create separate category for unknown values
if self.handle_unknown == "use_encoded_value":
X_trans[~X_mask] = self.unknown_value
return X_trans
def inverse_transform(self, X):
"""
Convert the data back to the original representation.
Parameters
----------
X : array-like of shape (n_samples, n_encoded_features)
The transformed data.
Returns
-------
X_tr : ndarray of shape (n_samples, n_features)
Inverse transformed array.
"""
check_is_fitted(self)
X = check_array(X, ensure_all_finite="allow-nan")
n_samples, _ = X.shape
n_features = len(self.categories_)
# validate shape of passed X
msg = (
"Shape of the passed X data is not correct. Expected {0} columns, got {1}."
)
if X.shape[1] != n_features:
raise ValueError(msg.format(n_features, X.shape[1]))
# create resulting array of appropriate dtype
dt = np.result_type(*[cat.dtype for cat in self.categories_])
X_tr = np.empty((n_samples, n_features), dtype=dt)
found_unknown = {}
infrequent_masks = {}
infrequent_indices = getattr(self, "_infrequent_indices", None)
for i in range(n_features):
labels = X[:, i]
# replace values of X[:, i] that were nan with actual indices
if i in self._missing_indices:
X_i_mask = _get_mask(labels, self.encoded_missing_value)
labels[X_i_mask] = self._missing_indices[i]
rows_to_update = slice(None)
categories = self.categories_[i]
if infrequent_indices is not None and infrequent_indices[i] is not None:
# Compute mask for frequent categories
infrequent_encoding_value = len(categories) - len(infrequent_indices[i])
infrequent_masks[i] = labels == infrequent_encoding_value
rows_to_update = ~infrequent_masks[i]
# Remap categories to be only frequent categories. The infrequent
# categories will be mapped to "infrequent_sklearn" later
frequent_categories_mask = np.ones_like(categories, dtype=bool)
frequent_categories_mask[infrequent_indices[i]] = False
categories = categories[frequent_categories_mask]
if self.handle_unknown == "use_encoded_value":
unknown_labels = _get_mask(labels, self.unknown_value)
found_unknown[i] = unknown_labels
known_labels = ~unknown_labels
if isinstance(rows_to_update, np.ndarray):
rows_to_update &= known_labels
else:
rows_to_update = known_labels
labels_int = labels[rows_to_update].astype("int64", copy=False)
X_tr[rows_to_update, i] = categories[labels_int]
if found_unknown or infrequent_masks:
X_tr = X_tr.astype(object, copy=False)
# insert None values for unknown values
if found_unknown:
for idx, mask in found_unknown.items():
X_tr[mask, idx] = None
if infrequent_masks:
for idx, mask in infrequent_masks.items():
X_tr[mask, idx] = "infrequent_sklearn"
return X_tr
|