partial fitting, how to ensure one hot captures all features consistently












0












$begingroup$


Doing some data science on ~4 million samples, with lots of columns being categorical.



One column has ~1000 categories and my boss insists on including it in the analysis.



My output is also predicting classes (I'll use gnb.predict_proba())



So, I'm taking a random subset of my data for partial fitting, and repeating.



# train = ~3 million rows of data as a dataframe
gnb = naive_bayes.GaussianNB()
for i in range(10):
dds = train.sample(n=10**4)
(dfX,dfY) = makeXY(dds) #gets one-hot- encoded X and Y dataframes
gnb.partial_fit(dfX,[getClass(x) for x in dfY.values],classes=np.unique([getClass(x) for x in dfY.values]))


How can I ensure I get all the possible classes AND that they are in the same order every time?










share|improve this question











$endgroup$




bumped to the homepage by Community 18 mins ago


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















  • $begingroup$
    Look here for stratified sampling: stackoverflow.com/questions/41035187/…
    $endgroup$
    – CrazyElf
    Jan 31 '18 at 14:33
















0












$begingroup$


Doing some data science on ~4 million samples, with lots of columns being categorical.



One column has ~1000 categories and my boss insists on including it in the analysis.



My output is also predicting classes (I'll use gnb.predict_proba())



So, I'm taking a random subset of my data for partial fitting, and repeating.



# train = ~3 million rows of data as a dataframe
gnb = naive_bayes.GaussianNB()
for i in range(10):
dds = train.sample(n=10**4)
(dfX,dfY) = makeXY(dds) #gets one-hot- encoded X and Y dataframes
gnb.partial_fit(dfX,[getClass(x) for x in dfY.values],classes=np.unique([getClass(x) for x in dfY.values]))


How can I ensure I get all the possible classes AND that they are in the same order every time?










share|improve this question











$endgroup$




bumped to the homepage by Community 18 mins ago


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















  • $begingroup$
    Look here for stratified sampling: stackoverflow.com/questions/41035187/…
    $endgroup$
    – CrazyElf
    Jan 31 '18 at 14:33














0












0








0





$begingroup$


Doing some data science on ~4 million samples, with lots of columns being categorical.



One column has ~1000 categories and my boss insists on including it in the analysis.



My output is also predicting classes (I'll use gnb.predict_proba())



So, I'm taking a random subset of my data for partial fitting, and repeating.



# train = ~3 million rows of data as a dataframe
gnb = naive_bayes.GaussianNB()
for i in range(10):
dds = train.sample(n=10**4)
(dfX,dfY) = makeXY(dds) #gets one-hot- encoded X and Y dataframes
gnb.partial_fit(dfX,[getClass(x) for x in dfY.values],classes=np.unique([getClass(x) for x in dfY.values]))


How can I ensure I get all the possible classes AND that they are in the same order every time?










share|improve this question











$endgroup$




Doing some data science on ~4 million samples, with lots of columns being categorical.



One column has ~1000 categories and my boss insists on including it in the analysis.



My output is also predicting classes (I'll use gnb.predict_proba())



So, I'm taking a random subset of my data for partial fitting, and repeating.



# train = ~3 million rows of data as a dataframe
gnb = naive_bayes.GaussianNB()
for i in range(10):
dds = train.sample(n=10**4)
(dfX,dfY) = makeXY(dds) #gets one-hot- encoded X and Y dataframes
gnb.partial_fit(dfX,[getClass(x) for x in dfY.values],classes=np.unique([getClass(x) for x in dfY.values]))


How can I ensure I get all the possible classes AND that they are in the same order every time?







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 31 '18 at 14:28









Stephen Rauch

1,53551330




1,53551330










asked Jan 31 '18 at 14:02









Hari PrasadHari Prasad

126124




126124





bumped to the homepage by Community 18 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 18 mins ago


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














  • $begingroup$
    Look here for stratified sampling: stackoverflow.com/questions/41035187/…
    $endgroup$
    – CrazyElf
    Jan 31 '18 at 14:33


















  • $begingroup$
    Look here for stratified sampling: stackoverflow.com/questions/41035187/…
    $endgroup$
    – CrazyElf
    Jan 31 '18 at 14:33
















$begingroup$
Look here for stratified sampling: stackoverflow.com/questions/41035187/…
$endgroup$
– CrazyElf
Jan 31 '18 at 14:33




$begingroup$
Look here for stratified sampling: stackoverflow.com/questions/41035187/…
$endgroup$
– CrazyElf
Jan 31 '18 at 14:33










1 Answer
1






active

oldest

votes


















0












$begingroup$

If you're using sklearn, this is a great use of CountVectorizer as a workaround since you can specify a vocabulary.



To start, get a list of all the 1000 categories and set that as the vocabulary in the transformer. Then convert the column to a string data type and apply this transformer to each batch:



from sklearn.feature_extraction.text import CountVectorizer

cv = CountVectorizer(vocabulary = pre_made_category_list)
endoded_variable_matrix = cv.fit_transform(dfX[categorical_column])


Even though it's technically a count vectorizor, since there is only one word in the string, the counts for each row will be 1 for the category it is and 0 for everything else, so its effectively one hot encoding the variable. The order of the columns will be the order of the vocabulary, so the matrix will be consistent between folds.






share|improve this answer









$endgroup$














    Your Answer








    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%2f27297%2fpartial-fitting-how-to-ensure-one-hot-captures-all-features-consistently%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$

    If you're using sklearn, this is a great use of CountVectorizer as a workaround since you can specify a vocabulary.



    To start, get a list of all the 1000 categories and set that as the vocabulary in the transformer. Then convert the column to a string data type and apply this transformer to each batch:



    from sklearn.feature_extraction.text import CountVectorizer

    cv = CountVectorizer(vocabulary = pre_made_category_list)
    endoded_variable_matrix = cv.fit_transform(dfX[categorical_column])


    Even though it's technically a count vectorizor, since there is only one word in the string, the counts for each row will be 1 for the category it is and 0 for everything else, so its effectively one hot encoding the variable. The order of the columns will be the order of the vocabulary, so the matrix will be consistent between folds.






    share|improve this answer









    $endgroup$


















      0












      $begingroup$

      If you're using sklearn, this is a great use of CountVectorizer as a workaround since you can specify a vocabulary.



      To start, get a list of all the 1000 categories and set that as the vocabulary in the transformer. Then convert the column to a string data type and apply this transformer to each batch:



      from sklearn.feature_extraction.text import CountVectorizer

      cv = CountVectorizer(vocabulary = pre_made_category_list)
      endoded_variable_matrix = cv.fit_transform(dfX[categorical_column])


      Even though it's technically a count vectorizor, since there is only one word in the string, the counts for each row will be 1 for the category it is and 0 for everything else, so its effectively one hot encoding the variable. The order of the columns will be the order of the vocabulary, so the matrix will be consistent between folds.






      share|improve this answer









      $endgroup$
















        0












        0








        0





        $begingroup$

        If you're using sklearn, this is a great use of CountVectorizer as a workaround since you can specify a vocabulary.



        To start, get a list of all the 1000 categories and set that as the vocabulary in the transformer. Then convert the column to a string data type and apply this transformer to each batch:



        from sklearn.feature_extraction.text import CountVectorizer

        cv = CountVectorizer(vocabulary = pre_made_category_list)
        endoded_variable_matrix = cv.fit_transform(dfX[categorical_column])


        Even though it's technically a count vectorizor, since there is only one word in the string, the counts for each row will be 1 for the category it is and 0 for everything else, so its effectively one hot encoding the variable. The order of the columns will be the order of the vocabulary, so the matrix will be consistent between folds.






        share|improve this answer









        $endgroup$



        If you're using sklearn, this is a great use of CountVectorizer as a workaround since you can specify a vocabulary.



        To start, get a list of all the 1000 categories and set that as the vocabulary in the transformer. Then convert the column to a string data type and apply this transformer to each batch:



        from sklearn.feature_extraction.text import CountVectorizer

        cv = CountVectorizer(vocabulary = pre_made_category_list)
        endoded_variable_matrix = cv.fit_transform(dfX[categorical_column])


        Even though it's technically a count vectorizor, since there is only one word in the string, the counts for each row will be 1 for the category it is and 0 for everything else, so its effectively one hot encoding the variable. The order of the columns will be the order of the vocabulary, so the matrix will be consistent between folds.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 31 '18 at 22:32









        dbagherndbaghern

        29115




        29115






























            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%2f27297%2fpartial-fitting-how-to-ensure-one-hot-captures-all-features-consistently%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

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana