Specifying the order of encoding in Ordinal Encoder










1















I'm using OrdinalEncoder, and I cannot find how to specify the encoding order. I mean that I have categories like "bad", "average", "good" which naturally have an order. But I want to specify that order, since the encoder cannot know itself the meaning of categories. Indeed, with categories='auto', some categories are encoded in wrong direction with respect to some others and I do not want this because I know, at least for some of them, if the correlation is positive or negative.



But specifying the categories results in an error during fitting:




'OrdinalEncoder' object has no attribute 'handle_unknown'.




If I do not specify the categories, fitting process goes well, and I do not understand why (the attribute "categories_", after fitting, shows me the same categories I enter by hand when I try to specify them).



I specify the categories as a list of lists. Here what happens without specifying categories.



import pandas as pd
from sklearn.preprocessing import OrdinalEncoder

df = pd.DataFrame(np.array([['a','a','a'], ['b','c','c']]).transpose())
oE = OrdinalEncoder(categories='auto')
oE.fit(df)

print(oE.categories_)


Resulting in: [array(['a'], dtype=object), array(['b', 'c'], dtype=object)]



Specifying categories explicitely:



df = pd.DataFrame(np.array([['a','a','a'], ['b','c','c']]).transpose())
oE = OrdinalEncoder(categories=[['a'], ['b', 'c']])
oE.fit(df)


The result is this error:





Traceback (most recent call last):




File "", line 3, in
oE.fit(df)



File
"/home/alessio/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py",
line 774, in fit
self._fit(X)



File
"/home/alessio/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py",
line 85, in _fit
if self.handle_unknown == 'error':



AttributeError: 'OrdinalEncoder' object has no attribute
'handle_unknown'











share|improve this question
























  • From which line do you get this error? -> 'OrdinalEncoder' object has no attribute 'handle_unknown'.

    – Vivek Kumar
    Nov 14 '18 at 7:55











  • From ordEnc.fit(dataframe)

    – Alessio Giberti
    Nov 14 '18 at 8:07
















1















I'm using OrdinalEncoder, and I cannot find how to specify the encoding order. I mean that I have categories like "bad", "average", "good" which naturally have an order. But I want to specify that order, since the encoder cannot know itself the meaning of categories. Indeed, with categories='auto', some categories are encoded in wrong direction with respect to some others and I do not want this because I know, at least for some of them, if the correlation is positive or negative.



But specifying the categories results in an error during fitting:




'OrdinalEncoder' object has no attribute 'handle_unknown'.




If I do not specify the categories, fitting process goes well, and I do not understand why (the attribute "categories_", after fitting, shows me the same categories I enter by hand when I try to specify them).



I specify the categories as a list of lists. Here what happens without specifying categories.



import pandas as pd
from sklearn.preprocessing import OrdinalEncoder

df = pd.DataFrame(np.array([['a','a','a'], ['b','c','c']]).transpose())
oE = OrdinalEncoder(categories='auto')
oE.fit(df)

print(oE.categories_)


Resulting in: [array(['a'], dtype=object), array(['b', 'c'], dtype=object)]



Specifying categories explicitely:



df = pd.DataFrame(np.array([['a','a','a'], ['b','c','c']]).transpose())
oE = OrdinalEncoder(categories=[['a'], ['b', 'c']])
oE.fit(df)


The result is this error:





Traceback (most recent call last):




File "", line 3, in
oE.fit(df)



File
"/home/alessio/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py",
line 774, in fit
self._fit(X)



File
"/home/alessio/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py",
line 85, in _fit
if self.handle_unknown == 'error':



AttributeError: 'OrdinalEncoder' object has no attribute
'handle_unknown'











share|improve this question
























  • From which line do you get this error? -> 'OrdinalEncoder' object has no attribute 'handle_unknown'.

    – Vivek Kumar
    Nov 14 '18 at 7:55











  • From ordEnc.fit(dataframe)

    – Alessio Giberti
    Nov 14 '18 at 8:07














1












1








1








I'm using OrdinalEncoder, and I cannot find how to specify the encoding order. I mean that I have categories like "bad", "average", "good" which naturally have an order. But I want to specify that order, since the encoder cannot know itself the meaning of categories. Indeed, with categories='auto', some categories are encoded in wrong direction with respect to some others and I do not want this because I know, at least for some of them, if the correlation is positive or negative.



But specifying the categories results in an error during fitting:




'OrdinalEncoder' object has no attribute 'handle_unknown'.




If I do not specify the categories, fitting process goes well, and I do not understand why (the attribute "categories_", after fitting, shows me the same categories I enter by hand when I try to specify them).



I specify the categories as a list of lists. Here what happens without specifying categories.



import pandas as pd
from sklearn.preprocessing import OrdinalEncoder

df = pd.DataFrame(np.array([['a','a','a'], ['b','c','c']]).transpose())
oE = OrdinalEncoder(categories='auto')
oE.fit(df)

print(oE.categories_)


Resulting in: [array(['a'], dtype=object), array(['b', 'c'], dtype=object)]



Specifying categories explicitely:



df = pd.DataFrame(np.array([['a','a','a'], ['b','c','c']]).transpose())
oE = OrdinalEncoder(categories=[['a'], ['b', 'c']])
oE.fit(df)


The result is this error:





Traceback (most recent call last):




File "", line 3, in
oE.fit(df)



File
"/home/alessio/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py",
line 774, in fit
self._fit(X)



File
"/home/alessio/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py",
line 85, in _fit
if self.handle_unknown == 'error':



AttributeError: 'OrdinalEncoder' object has no attribute
'handle_unknown'











share|improve this question
















I'm using OrdinalEncoder, and I cannot find how to specify the encoding order. I mean that I have categories like "bad", "average", "good" which naturally have an order. But I want to specify that order, since the encoder cannot know itself the meaning of categories. Indeed, with categories='auto', some categories are encoded in wrong direction with respect to some others and I do not want this because I know, at least for some of them, if the correlation is positive or negative.



But specifying the categories results in an error during fitting:




'OrdinalEncoder' object has no attribute 'handle_unknown'.




If I do not specify the categories, fitting process goes well, and I do not understand why (the attribute "categories_", after fitting, shows me the same categories I enter by hand when I try to specify them).



I specify the categories as a list of lists. Here what happens without specifying categories.



import pandas as pd
from sklearn.preprocessing import OrdinalEncoder

df = pd.DataFrame(np.array([['a','a','a'], ['b','c','c']]).transpose())
oE = OrdinalEncoder(categories='auto')
oE.fit(df)

print(oE.categories_)


Resulting in: [array(['a'], dtype=object), array(['b', 'c'], dtype=object)]



Specifying categories explicitely:



df = pd.DataFrame(np.array([['a','a','a'], ['b','c','c']]).transpose())
oE = OrdinalEncoder(categories=[['a'], ['b', 'c']])
oE.fit(df)


The result is this error:





Traceback (most recent call last):




File "", line 3, in
oE.fit(df)



File
"/home/alessio/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py",
line 774, in fit
self._fit(X)



File
"/home/alessio/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py",
line 85, in _fit
if self.handle_unknown == 'error':



AttributeError: 'OrdinalEncoder' object has no attribute
'handle_unknown'








python-3.x scikit-learn categorical-data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 8:25







Alessio Giberti

















asked Nov 14 '18 at 7:41









Alessio GibertiAlessio Giberti

243




243












  • From which line do you get this error? -> 'OrdinalEncoder' object has no attribute 'handle_unknown'.

    – Vivek Kumar
    Nov 14 '18 at 7:55











  • From ordEnc.fit(dataframe)

    – Alessio Giberti
    Nov 14 '18 at 8:07


















  • From which line do you get this error? -> 'OrdinalEncoder' object has no attribute 'handle_unknown'.

    – Vivek Kumar
    Nov 14 '18 at 7:55











  • From ordEnc.fit(dataframe)

    – Alessio Giberti
    Nov 14 '18 at 8:07

















From which line do you get this error? -> 'OrdinalEncoder' object has no attribute 'handle_unknown'.

– Vivek Kumar
Nov 14 '18 at 7:55





From which line do you get this error? -> 'OrdinalEncoder' object has no attribute 'handle_unknown'.

– Vivek Kumar
Nov 14 '18 at 7:55













From ordEnc.fit(dataframe)

– Alessio Giberti
Nov 14 '18 at 8:07






From ordEnc.fit(dataframe)

– Alessio Giberti
Nov 14 '18 at 8:07













1 Answer
1






active

oldest

votes


















0














I had the same problem. This is bug in scikit-learn, already fixed and added to version 0.20.1, which is still not released.
https://github.com/scikit-learn/scikit-learn/issues/12365



I solved it temporarily by copying fixed _encoders.py to my project and using.



from _encoders import OrdinalEncoder





share|improve this answer























  • Thank you for your answer Tomas, at least I know it's not my fault! But copying _encoders.py in my folder does not work: ImportError: attempted relative import with no known parent package

    – Alessio Giberti
    Nov 14 '18 at 9:33











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295208%2fspecifying-the-order-of-encoding-in-ordinal-encoder%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I had the same problem. This is bug in scikit-learn, already fixed and added to version 0.20.1, which is still not released.
https://github.com/scikit-learn/scikit-learn/issues/12365



I solved it temporarily by copying fixed _encoders.py to my project and using.



from _encoders import OrdinalEncoder





share|improve this answer























  • Thank you for your answer Tomas, at least I know it's not my fault! But copying _encoders.py in my folder does not work: ImportError: attempted relative import with no known parent package

    – Alessio Giberti
    Nov 14 '18 at 9:33
















0














I had the same problem. This is bug in scikit-learn, already fixed and added to version 0.20.1, which is still not released.
https://github.com/scikit-learn/scikit-learn/issues/12365



I solved it temporarily by copying fixed _encoders.py to my project and using.



from _encoders import OrdinalEncoder





share|improve this answer























  • Thank you for your answer Tomas, at least I know it's not my fault! But copying _encoders.py in my folder does not work: ImportError: attempted relative import with no known parent package

    – Alessio Giberti
    Nov 14 '18 at 9:33














0












0








0







I had the same problem. This is bug in scikit-learn, already fixed and added to version 0.20.1, which is still not released.
https://github.com/scikit-learn/scikit-learn/issues/12365



I solved it temporarily by copying fixed _encoders.py to my project and using.



from _encoders import OrdinalEncoder





share|improve this answer













I had the same problem. This is bug in scikit-learn, already fixed and added to version 0.20.1, which is still not released.
https://github.com/scikit-learn/scikit-learn/issues/12365



I solved it temporarily by copying fixed _encoders.py to my project and using.



from _encoders import OrdinalEncoder






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 8:52









Tomáš PřindaTomáš Přinda

32327




32327












  • Thank you for your answer Tomas, at least I know it's not my fault! But copying _encoders.py in my folder does not work: ImportError: attempted relative import with no known parent package

    – Alessio Giberti
    Nov 14 '18 at 9:33


















  • Thank you for your answer Tomas, at least I know it's not my fault! But copying _encoders.py in my folder does not work: ImportError: attempted relative import with no known parent package

    – Alessio Giberti
    Nov 14 '18 at 9:33

















Thank you for your answer Tomas, at least I know it's not my fault! But copying _encoders.py in my folder does not work: ImportError: attempted relative import with no known parent package

– Alessio Giberti
Nov 14 '18 at 9:33






Thank you for your answer Tomas, at least I know it's not my fault! But copying _encoders.py in my folder does not work: ImportError: attempted relative import with no known parent package

– Alessio Giberti
Nov 14 '18 at 9:33




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295208%2fspecifying-the-order-of-encoding-in-ordinal-encoder%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo