How to use one hot encoding of string categorical features in keras?












0












$begingroup$


I am dealing with a binary classification problem. The output column of my dataset is already encoded in 0/1. The problem is that I have many categorical features (columns), which are strings and I would like to one-hot encode them.



I have 18 features (few features are integers and others are strings, the categorical ones) and 1 output column.



I tried this:



dataframe = pd.read_csv('basic_df_export.csv', sep=';', encoding = 'ISO-8859-1', header=None) 

dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:17]
Y = dataset[:,17]

# define example
encoded = to_categorical(X)
print(encoded)


but it doesn't work, throwing me this error:



---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-41-318da09d7033> in <module>()
9
10 # define example
---> 11 encoded = to_categorical(X)
12 print(encoded)

~/anaconda/lib/python3.6/site-packages/keras/utils/np_utils.py in to_categorical(y, num_classes)
21 is placed last.
22 """
---> 23 y = np.array(y, dtype='int')
24 input_shape = y.shape
25 if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:

ValueError: invalid literal for int() with base 10: 'photo'









share|improve this question









$endgroup$




bumped to the homepage by Community 2 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    $begingroup$
    Look at this: pbpython.com/categorical-encoding.html
    $endgroup$
    – moh
    Jan 7 at 21:47










  • $begingroup$
    I suggest that you modify your question and provide some actual data points so that people can see what kind of variable are there. Clearly you do not know how to encode categorical features. You say you have a mix of categorical and numerical columns, but here "encoded = to_categorical(X)", you pass all your features to be encoded. Not to mention that this way of encoding categorical features is rather wrong as well! Otherwise you get not useful answers. A place to start understanding cat. encoding could be: towardsdatascience.com/…
    $endgroup$
    – Majid Mortazavi
    Jan 9 at 6:57


















0












$begingroup$


I am dealing with a binary classification problem. The output column of my dataset is already encoded in 0/1. The problem is that I have many categorical features (columns), which are strings and I would like to one-hot encode them.



I have 18 features (few features are integers and others are strings, the categorical ones) and 1 output column.



I tried this:



dataframe = pd.read_csv('basic_df_export.csv', sep=';', encoding = 'ISO-8859-1', header=None) 

dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:17]
Y = dataset[:,17]

# define example
encoded = to_categorical(X)
print(encoded)


but it doesn't work, throwing me this error:



---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-41-318da09d7033> in <module>()
9
10 # define example
---> 11 encoded = to_categorical(X)
12 print(encoded)

~/anaconda/lib/python3.6/site-packages/keras/utils/np_utils.py in to_categorical(y, num_classes)
21 is placed last.
22 """
---> 23 y = np.array(y, dtype='int')
24 input_shape = y.shape
25 if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:

ValueError: invalid literal for int() with base 10: 'photo'









share|improve this question









$endgroup$




bumped to the homepage by Community 2 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    $begingroup$
    Look at this: pbpython.com/categorical-encoding.html
    $endgroup$
    – moh
    Jan 7 at 21:47










  • $begingroup$
    I suggest that you modify your question and provide some actual data points so that people can see what kind of variable are there. Clearly you do not know how to encode categorical features. You say you have a mix of categorical and numerical columns, but here "encoded = to_categorical(X)", you pass all your features to be encoded. Not to mention that this way of encoding categorical features is rather wrong as well! Otherwise you get not useful answers. A place to start understanding cat. encoding could be: towardsdatascience.com/…
    $endgroup$
    – Majid Mortazavi
    Jan 9 at 6:57
















0












0








0





$begingroup$


I am dealing with a binary classification problem. The output column of my dataset is already encoded in 0/1. The problem is that I have many categorical features (columns), which are strings and I would like to one-hot encode them.



I have 18 features (few features are integers and others are strings, the categorical ones) and 1 output column.



I tried this:



dataframe = pd.read_csv('basic_df_export.csv', sep=';', encoding = 'ISO-8859-1', header=None) 

dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:17]
Y = dataset[:,17]

# define example
encoded = to_categorical(X)
print(encoded)


but it doesn't work, throwing me this error:



---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-41-318da09d7033> in <module>()
9
10 # define example
---> 11 encoded = to_categorical(X)
12 print(encoded)

~/anaconda/lib/python3.6/site-packages/keras/utils/np_utils.py in to_categorical(y, num_classes)
21 is placed last.
22 """
---> 23 y = np.array(y, dtype='int')
24 input_shape = y.shape
25 if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:

ValueError: invalid literal for int() with base 10: 'photo'









share|improve this question









$endgroup$




I am dealing with a binary classification problem. The output column of my dataset is already encoded in 0/1. The problem is that I have many categorical features (columns), which are strings and I would like to one-hot encode them.



I have 18 features (few features are integers and others are strings, the categorical ones) and 1 output column.



I tried this:



dataframe = pd.read_csv('basic_df_export.csv', sep=';', encoding = 'ISO-8859-1', header=None) 

dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:17]
Y = dataset[:,17]

# define example
encoded = to_categorical(X)
print(encoded)


but it doesn't work, throwing me this error:



---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-41-318da09d7033> in <module>()
9
10 # define example
---> 11 encoded = to_categorical(X)
12 print(encoded)

~/anaconda/lib/python3.6/site-packages/keras/utils/np_utils.py in to_categorical(y, num_classes)
21 is placed last.
22 """
---> 23 y = np.array(y, dtype='int')
24 input_shape = y.shape
25 if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:

ValueError: invalid literal for int() with base 10: 'photo'






classification keras feature-engineering encoding






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 7 at 20:11









ZelelBZelelB

1526




1526





bumped to the homepage by Community 2 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1




    $begingroup$
    Look at this: pbpython.com/categorical-encoding.html
    $endgroup$
    – moh
    Jan 7 at 21:47










  • $begingroup$
    I suggest that you modify your question and provide some actual data points so that people can see what kind of variable are there. Clearly you do not know how to encode categorical features. You say you have a mix of categorical and numerical columns, but here "encoded = to_categorical(X)", you pass all your features to be encoded. Not to mention that this way of encoding categorical features is rather wrong as well! Otherwise you get not useful answers. A place to start understanding cat. encoding could be: towardsdatascience.com/…
    $endgroup$
    – Majid Mortazavi
    Jan 9 at 6:57
















  • 1




    $begingroup$
    Look at this: pbpython.com/categorical-encoding.html
    $endgroup$
    – moh
    Jan 7 at 21:47










  • $begingroup$
    I suggest that you modify your question and provide some actual data points so that people can see what kind of variable are there. Clearly you do not know how to encode categorical features. You say you have a mix of categorical and numerical columns, but here "encoded = to_categorical(X)", you pass all your features to be encoded. Not to mention that this way of encoding categorical features is rather wrong as well! Otherwise you get not useful answers. A place to start understanding cat. encoding could be: towardsdatascience.com/…
    $endgroup$
    – Majid Mortazavi
    Jan 9 at 6:57










1




1




$begingroup$
Look at this: pbpython.com/categorical-encoding.html
$endgroup$
– moh
Jan 7 at 21:47




$begingroup$
Look at this: pbpython.com/categorical-encoding.html
$endgroup$
– moh
Jan 7 at 21:47












$begingroup$
I suggest that you modify your question and provide some actual data points so that people can see what kind of variable are there. Clearly you do not know how to encode categorical features. You say you have a mix of categorical and numerical columns, but here "encoded = to_categorical(X)", you pass all your features to be encoded. Not to mention that this way of encoding categorical features is rather wrong as well! Otherwise you get not useful answers. A place to start understanding cat. encoding could be: towardsdatascience.com/…
$endgroup$
– Majid Mortazavi
Jan 9 at 6:57






$begingroup$
I suggest that you modify your question and provide some actual data points so that people can see what kind of variable are there. Clearly you do not know how to encode categorical features. You say you have a mix of categorical and numerical columns, but here "encoded = to_categorical(X)", you pass all your features to be encoded. Not to mention that this way of encoding categorical features is rather wrong as well! Otherwise you get not useful answers. A place to start understanding cat. encoding could be: towardsdatascience.com/…
$endgroup$
– Majid Mortazavi
Jan 9 at 6:57












1 Answer
1






active

oldest

votes


















0












$begingroup$

Try:



X = dataset[:,0:17].astype(float).astype(int)


I think that if you have a string like '45.2', you will have to cast it as a floating-point first and from float you can cast them into integer.



I will be glad if an editor could corroborate/correct this answer.






share|improve this answer









$endgroup$













  • $begingroup$
    no it doesn't work. I dont have any strings like that. All my columns are integers or strings (categorical features, where for example, in the column 'industry' the values could take one of 3 values: 'IT', 'FoodTrade' or 'Automotive') and the column 'content_type' could take one of 4 values: 'photo', 'video', 'link', 'event')
    $endgroup$
    – ZelelB
    Jan 8 at 21:17










  • $begingroup$
    Ok, you can describe your "to_categorical" method together with a few rows of your data? You can't certainly convert 'photo' into a integer. Did you already transformed these strings 'photo', 'video', 'link', 'event' into 1,2,3,4?
    $endgroup$
    – aerijman
    Jan 8 at 22:04










  • $begingroup$
    got it.. I used get_dummies()
    $endgroup$
    – ZelelB
    Jan 10 at 0:22











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdatascience.stackexchange.com%2fquestions%2f43636%2fhow-to-use-one-hot-encoding-of-string-categorical-features-in-keras%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












$begingroup$

Try:



X = dataset[:,0:17].astype(float).astype(int)


I think that if you have a string like '45.2', you will have to cast it as a floating-point first and from float you can cast them into integer.



I will be glad if an editor could corroborate/correct this answer.






share|improve this answer









$endgroup$













  • $begingroup$
    no it doesn't work. I dont have any strings like that. All my columns are integers or strings (categorical features, where for example, in the column 'industry' the values could take one of 3 values: 'IT', 'FoodTrade' or 'Automotive') and the column 'content_type' could take one of 4 values: 'photo', 'video', 'link', 'event')
    $endgroup$
    – ZelelB
    Jan 8 at 21:17










  • $begingroup$
    Ok, you can describe your "to_categorical" method together with a few rows of your data? You can't certainly convert 'photo' into a integer. Did you already transformed these strings 'photo', 'video', 'link', 'event' into 1,2,3,4?
    $endgroup$
    – aerijman
    Jan 8 at 22:04










  • $begingroup$
    got it.. I used get_dummies()
    $endgroup$
    – ZelelB
    Jan 10 at 0:22
















0












$begingroup$

Try:



X = dataset[:,0:17].astype(float).astype(int)


I think that if you have a string like '45.2', you will have to cast it as a floating-point first and from float you can cast them into integer.



I will be glad if an editor could corroborate/correct this answer.






share|improve this answer









$endgroup$













  • $begingroup$
    no it doesn't work. I dont have any strings like that. All my columns are integers or strings (categorical features, where for example, in the column 'industry' the values could take one of 3 values: 'IT', 'FoodTrade' or 'Automotive') and the column 'content_type' could take one of 4 values: 'photo', 'video', 'link', 'event')
    $endgroup$
    – ZelelB
    Jan 8 at 21:17










  • $begingroup$
    Ok, you can describe your "to_categorical" method together with a few rows of your data? You can't certainly convert 'photo' into a integer. Did you already transformed these strings 'photo', 'video', 'link', 'event' into 1,2,3,4?
    $endgroup$
    – aerijman
    Jan 8 at 22:04










  • $begingroup$
    got it.. I used get_dummies()
    $endgroup$
    – ZelelB
    Jan 10 at 0:22














0












0








0





$begingroup$

Try:



X = dataset[:,0:17].astype(float).astype(int)


I think that if you have a string like '45.2', you will have to cast it as a floating-point first and from float you can cast them into integer.



I will be glad if an editor could corroborate/correct this answer.






share|improve this answer









$endgroup$



Try:



X = dataset[:,0:17].astype(float).astype(int)


I think that if you have a string like '45.2', you will have to cast it as a floating-point first and from float you can cast them into integer.



I will be glad if an editor could corroborate/correct this answer.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 8 at 2:56









aerijmanaerijman

285




285












  • $begingroup$
    no it doesn't work. I dont have any strings like that. All my columns are integers or strings (categorical features, where for example, in the column 'industry' the values could take one of 3 values: 'IT', 'FoodTrade' or 'Automotive') and the column 'content_type' could take one of 4 values: 'photo', 'video', 'link', 'event')
    $endgroup$
    – ZelelB
    Jan 8 at 21:17










  • $begingroup$
    Ok, you can describe your "to_categorical" method together with a few rows of your data? You can't certainly convert 'photo' into a integer. Did you already transformed these strings 'photo', 'video', 'link', 'event' into 1,2,3,4?
    $endgroup$
    – aerijman
    Jan 8 at 22:04










  • $begingroup$
    got it.. I used get_dummies()
    $endgroup$
    – ZelelB
    Jan 10 at 0:22


















  • $begingroup$
    no it doesn't work. I dont have any strings like that. All my columns are integers or strings (categorical features, where for example, in the column 'industry' the values could take one of 3 values: 'IT', 'FoodTrade' or 'Automotive') and the column 'content_type' could take one of 4 values: 'photo', 'video', 'link', 'event')
    $endgroup$
    – ZelelB
    Jan 8 at 21:17










  • $begingroup$
    Ok, you can describe your "to_categorical" method together with a few rows of your data? You can't certainly convert 'photo' into a integer. Did you already transformed these strings 'photo', 'video', 'link', 'event' into 1,2,3,4?
    $endgroup$
    – aerijman
    Jan 8 at 22:04










  • $begingroup$
    got it.. I used get_dummies()
    $endgroup$
    – ZelelB
    Jan 10 at 0:22
















$begingroup$
no it doesn't work. I dont have any strings like that. All my columns are integers or strings (categorical features, where for example, in the column 'industry' the values could take one of 3 values: 'IT', 'FoodTrade' or 'Automotive') and the column 'content_type' could take one of 4 values: 'photo', 'video', 'link', 'event')
$endgroup$
– ZelelB
Jan 8 at 21:17




$begingroup$
no it doesn't work. I dont have any strings like that. All my columns are integers or strings (categorical features, where for example, in the column 'industry' the values could take one of 3 values: 'IT', 'FoodTrade' or 'Automotive') and the column 'content_type' could take one of 4 values: 'photo', 'video', 'link', 'event')
$endgroup$
– ZelelB
Jan 8 at 21:17












$begingroup$
Ok, you can describe your "to_categorical" method together with a few rows of your data? You can't certainly convert 'photo' into a integer. Did you already transformed these strings 'photo', 'video', 'link', 'event' into 1,2,3,4?
$endgroup$
– aerijman
Jan 8 at 22:04




$begingroup$
Ok, you can describe your "to_categorical" method together with a few rows of your data? You can't certainly convert 'photo' into a integer. Did you already transformed these strings 'photo', 'video', 'link', 'event' into 1,2,3,4?
$endgroup$
– aerijman
Jan 8 at 22:04












$begingroup$
got it.. I used get_dummies()
$endgroup$
– ZelelB
Jan 10 at 0:22




$begingroup$
got it.. I used get_dummies()
$endgroup$
– ZelelB
Jan 10 at 0:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Data Science Stack Exchange!


  • 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.


Use MathJax to format equations. MathJax reference.


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%2fdatascience.stackexchange.com%2fquestions%2f43636%2fhow-to-use-one-hot-encoding-of-string-categorical-features-in-keras%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

Aikido

Tivadar Csontváry Kosztka

Metroo de Marsejlo