question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OneHotEncoder - categories parameter not working as deprecated n_values parameter

See original GitHub issue

When using the ‘n_values’ parameter, the code works as expected and I get the deprecation warning. However, when the code is changed to what’s suggested in deprecation warning, getting a ValueError. This works:

from sklearn.preprocessing import OneHotEncoder ohe = OneHotEncoder(n_values= 8,sparse = False) o = ohe.fit_transform(np.array([[3, 5, 1]])) o = o.reshape(1,3,8) print(o)

[[[0. 0. 0. 1. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0.]]] C:\Anaconda3\lib\site-packages\sklearn\preprocessing\_encoders.py:331: DeprecationWarning: Passing 'n_values' is deprecated in version 0.20 and will be removed in 0.22. You can use the 'categories' keyword instead. 'n_values=n' corresponds to 'categories=[range(n)]'. warnings.warn(msg, DeprecationWarning)

When I remove the n_values with categories = [range(n)] and run code as below, I get the error:

from sklearn.preprocessing import OneHotEncoder ohe = OneHotEncoder(categories = [range(8)],sparse = False) o = ohe.fit_transform(np.array([[3, 5, 1]])) o = o.reshape(1,3,8) print(o)


``ValueError Traceback (most recent call last) <ipython-input-10-2c07126f3194> in <module> 2 import pandas as pd 3 ohe = OneHotEncoder(categories = [range(8)],sparse = False) ----> 4 o = ohe.fit_transform(np.array([[3, 5, 1]])) 5 # pd.get_dummies(np.array([3, 5, 1])) 6 o.reshape(1,3,8)

C:\Anaconda3\lib\site-packages\sklearn\preprocessing_encoders.py in fit_transform(self, X, y) 516 self._categorical_features, copy=True) 517 else: –> 518 return self.fit(X).transform(X) 519 520 def _legacy_transform(self, X):

C:\Anaconda3\lib\site-packages\sklearn\preprocessing_encoders.py in fit(self, X, y) 427 return self 428 else: –> 429 self._fit(X, handle_unknown=self.handle_unknown) 430 return self 431

C:\Anaconda3\lib\site-packages\sklearn\preprocessing_encoders.py in _fit(self, X, handle_unknown) 70 “supported for numerical categories”) 71 if len(self._categories) != n_features: —> 72 raise ValueError(“Shape mismatch: if n_values is an array,” 73 " it has to be of shape (n_features,).") 74

ValueError: Shape mismatch: if n_values is an array, it has to be of shape (n_features,).``

Is this a usage error or a bug?

System: python: 3.7.1 | packaged by conda-forge | (default, Nov 13 2018, 19:01:41) [MSC v.1900 64 bit (AMD64)] executable: C:\Anaconda3\python.exe machine: Windows-10-10.0.16299-SP0

BLAS: macros: lib_dirs: cblas_libs: cblas

Python deps: pip: 18.1 setuptools: 40.6.3 sklearn: 0.20.3 numpy: 1.15.4 scipy: 1.1.0 Cython: 0.29.2 pandas: 0.23.4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
subhashicommented, Dec 14, 2019

Something like this worked for me.

Here I am giving 0 and 1 for a column named countries.

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer

laberEncoder_X = LabelEncoder()
X[:,0] = laberEncoder_X.fit_transform(X[:,0])
transform = ColumnTransformer([("countries", OneHotEncoder(), [0])], remainder="passthrough")
X = transform.fit_transform(X)
print (X)
0reactions
NonlinearNimeshcommented, Feb 16, 2022

Hi, i am trying to implement this below link

https://github.com/Oulu-IMEDS/DeepKnee/blob/a541896fcd7fbe38eaa8d73fce498d608ed2b00e/ouludeepknee/own_codes/produce_gradcam.py

but at line number 259 i.e.

 ohe = OneHotEncoder(sparse=False, n_values=5)

i am getting this below error

  File "produce_gradcam.py", line 347, in <module>
    path_dir_out=config.path_output)
  File "produce_gradcam.py", line 285, in predict_save
    img, heatmap, probs = self.predict(x=fileobj_in, nbits=nbits)
  File "produce_gradcam.py", line 259, in predict
    ohe = OneHotEncoder(sparse=False, n_values=5)
  File "C:\Users\Nimesh\Anaconda3\envs\deep_knee\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
    return f(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'n_values'

can anyone please help me how i can get rid of this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sklearn.preprocessing.OneHotEncoder
This creates a binary column for each category and returns a sparse matrix or dense array (depending on the sparse_output parameter).
Read more >
OneHotEncoder categorical_features deprecated, how to ...
Now I'm getting the depreciation message to use categories='auto' . If I do so the transformation is being done for the all independent...
Read more >
DeprecationWarning: The 'categorical_features' keyword is ...
You can do this to get rid of the deprecation messages from sklearn.preprocessing import OneHotEncoder from sklearn.compose import ColumnTransformer ct ...
Read more >
sklearn.preprocessing.OneHotEncoder — scikit-learn 0.20.2 ...
When this parameter is set to 'ignore' and an unknown category is encountered during transform, the resulting one-hot encoded columns for this feature...
Read more >
cuML API Reference — cuml 22.12.00 documentation
Fit OneHotEncoder to X. Parameters. XcuDF.DataFrame or cupy.ndarray, shape = (n_samples, n_features). The data to determine the categories of each feature.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found