Skip to content

Commit deed960

Browse files
github-actions[bot]FAQ Botalexeygrigorev
authored
[FAQ Bot] NEW: Why am I getting TypeError while creating OneHotEncoder Object? (#24)
* NEW: Why am I getting TypeError while creating OneHotEncoder Object? * Update OneHotEncoder documentation for parameter change --------- Co-authored-by: FAQ Bot <[email protected]> Co-authored-by: Alexey Grigorev <[email protected]>
1 parent 5c9521d commit deed960

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
id: 548dcc8a3c
3+
question: Why am I getting TypeError while creating OneHotEncoder Object?
4+
sort_order: 19
5+
---
6+
7+
In scikit-learn >= 1.2, `OneHotEncoder` no longer accepts the sparse parameter. It now uses the `sparse_output` parameter to control whether the output is sparse or dense.
8+
9+
What to do:
10+
- For a dense array: use `OneHotEncoder(sparse_output=False)`.
11+
- For a sparse matrix: use `OneHotEncoder(sparse_output=True)`.
12+
13+
If you previously wrote `ohe = OneHotEncoder(sparse=False)` and get a `TypeError`, replace it with `ohe = OneHotEncoder(sparse_output=False)`.
14+
15+
Example:
16+
17+
```python
18+
from sklearn.preprocessing import OneHotEncoder
19+
20+
ohe = OneHotEncoder(sparse_output=False)
21+
X_train_cat = ohe.fit_transform(df_train[categorical_columns].values)
22+
```
23+
24+
Notes:
25+
- A dense array will be returned if `sparse_output=False`; otherwise, you’ll get a scipy sparse matrix.
26+
- If you need a dense array from a sparse result, call `.toarray()` on the result.
27+
- This change ensures better memory efficiency and consistency in newer scikit-learn versions.

0 commit comments

Comments
 (0)