spark-ml normalizer loses metadata










1















I'm using a dataset with categorical features in PySpark which are indexed and one-hot encoded. After fitting the pipeline I extract the encoded features by using the metadata of the features column. When I include a normalizer in my pipeline I lose the metadata of my categorical features. See example below:



train.show()
+-----+---+----+----+
|admit|gre| gpa|rank|
+-----+---+----+----+
| 0.0|380|3.61| 3|
| 1.0|660|3.67| 3|
| 1.0|800| 4.0| 1|
| 1.0|640|3.19| 4|
| 0.0|520|2.93| 4|
+-----+---+----+----+

from pyspark.ml.feature import StringIndexer, OneHotEncoder, VectorAssembler, Normalizer
#indexer for categorical features
rank_indexer = StringIndexer(inputCol = 'rank', outputCol = 'rank_ind', handleInvalid="skip")
#encoder for categorical features
rank_encoder = OneHotEncoder(inputCol = 'rank_ind', outputCol = 'rank_enc')
# assembler
assembler = VectorAssembler(inputCols=['gre','gpa','rank_enc'], outputCol="featuresVect")
# Create the normalizer
normalizer = Normalizer(inputCol="featuresVect", outputCol="features", p=1.0)

stages = [rank_indexer] + [rank_encoder] + [assembler] + [normalizer]

from pyspark.ml import Pipeline
final_pipeline = Pipeline(
stages = stages
)

pipelineModel = final_pipeline.fit(train)
data = pipelineModel.transform(train)

data.schema['features'].metadata

## empty dictionary

## excluding the normalizer results in this metadata:
u'ml_attr': u'attrs': u'binary': [u'idx': 2, u'name': u'rank_enc_2',
u'idx': 3, u'name': u'rank_enc_3',
u'idx': 4, u'name': u'rank_enc_4'],
u'numeric': [u'idx': 0, u'name': u'gre', u'idx': 1, u'name': u'gpa'],
u'num_attrs': 5


Is this normal behavior? How can I include a normalizer without losing this metadata?










share|improve this question




























    1















    I'm using a dataset with categorical features in PySpark which are indexed and one-hot encoded. After fitting the pipeline I extract the encoded features by using the metadata of the features column. When I include a normalizer in my pipeline I lose the metadata of my categorical features. See example below:



    train.show()
    +-----+---+----+----+
    |admit|gre| gpa|rank|
    +-----+---+----+----+
    | 0.0|380|3.61| 3|
    | 1.0|660|3.67| 3|
    | 1.0|800| 4.0| 1|
    | 1.0|640|3.19| 4|
    | 0.0|520|2.93| 4|
    +-----+---+----+----+

    from pyspark.ml.feature import StringIndexer, OneHotEncoder, VectorAssembler, Normalizer
    #indexer for categorical features
    rank_indexer = StringIndexer(inputCol = 'rank', outputCol = 'rank_ind', handleInvalid="skip")
    #encoder for categorical features
    rank_encoder = OneHotEncoder(inputCol = 'rank_ind', outputCol = 'rank_enc')
    # assembler
    assembler = VectorAssembler(inputCols=['gre','gpa','rank_enc'], outputCol="featuresVect")
    # Create the normalizer
    normalizer = Normalizer(inputCol="featuresVect", outputCol="features", p=1.0)

    stages = [rank_indexer] + [rank_encoder] + [assembler] + [normalizer]

    from pyspark.ml import Pipeline
    final_pipeline = Pipeline(
    stages = stages
    )

    pipelineModel = final_pipeline.fit(train)
    data = pipelineModel.transform(train)

    data.schema['features'].metadata

    ## empty dictionary

    ## excluding the normalizer results in this metadata:
    u'ml_attr': u'attrs': u'binary': [u'idx': 2, u'name': u'rank_enc_2',
    u'idx': 3, u'name': u'rank_enc_3',
    u'idx': 4, u'name': u'rank_enc_4'],
    u'numeric': [u'idx': 0, u'name': u'gre', u'idx': 1, u'name': u'gpa'],
    u'num_attrs': 5


    Is this normal behavior? How can I include a normalizer without losing this metadata?










    share|improve this question


























      1












      1








      1








      I'm using a dataset with categorical features in PySpark which are indexed and one-hot encoded. After fitting the pipeline I extract the encoded features by using the metadata of the features column. When I include a normalizer in my pipeline I lose the metadata of my categorical features. See example below:



      train.show()
      +-----+---+----+----+
      |admit|gre| gpa|rank|
      +-----+---+----+----+
      | 0.0|380|3.61| 3|
      | 1.0|660|3.67| 3|
      | 1.0|800| 4.0| 1|
      | 1.0|640|3.19| 4|
      | 0.0|520|2.93| 4|
      +-----+---+----+----+

      from pyspark.ml.feature import StringIndexer, OneHotEncoder, VectorAssembler, Normalizer
      #indexer for categorical features
      rank_indexer = StringIndexer(inputCol = 'rank', outputCol = 'rank_ind', handleInvalid="skip")
      #encoder for categorical features
      rank_encoder = OneHotEncoder(inputCol = 'rank_ind', outputCol = 'rank_enc')
      # assembler
      assembler = VectorAssembler(inputCols=['gre','gpa','rank_enc'], outputCol="featuresVect")
      # Create the normalizer
      normalizer = Normalizer(inputCol="featuresVect", outputCol="features", p=1.0)

      stages = [rank_indexer] + [rank_encoder] + [assembler] + [normalizer]

      from pyspark.ml import Pipeline
      final_pipeline = Pipeline(
      stages = stages
      )

      pipelineModel = final_pipeline.fit(train)
      data = pipelineModel.transform(train)

      data.schema['features'].metadata

      ## empty dictionary

      ## excluding the normalizer results in this metadata:
      u'ml_attr': u'attrs': u'binary': [u'idx': 2, u'name': u'rank_enc_2',
      u'idx': 3, u'name': u'rank_enc_3',
      u'idx': 4, u'name': u'rank_enc_4'],
      u'numeric': [u'idx': 0, u'name': u'gre', u'idx': 1, u'name': u'gpa'],
      u'num_attrs': 5


      Is this normal behavior? How can I include a normalizer without losing this metadata?










      share|improve this question
















      I'm using a dataset with categorical features in PySpark which are indexed and one-hot encoded. After fitting the pipeline I extract the encoded features by using the metadata of the features column. When I include a normalizer in my pipeline I lose the metadata of my categorical features. See example below:



      train.show()
      +-----+---+----+----+
      |admit|gre| gpa|rank|
      +-----+---+----+----+
      | 0.0|380|3.61| 3|
      | 1.0|660|3.67| 3|
      | 1.0|800| 4.0| 1|
      | 1.0|640|3.19| 4|
      | 0.0|520|2.93| 4|
      +-----+---+----+----+

      from pyspark.ml.feature import StringIndexer, OneHotEncoder, VectorAssembler, Normalizer
      #indexer for categorical features
      rank_indexer = StringIndexer(inputCol = 'rank', outputCol = 'rank_ind', handleInvalid="skip")
      #encoder for categorical features
      rank_encoder = OneHotEncoder(inputCol = 'rank_ind', outputCol = 'rank_enc')
      # assembler
      assembler = VectorAssembler(inputCols=['gre','gpa','rank_enc'], outputCol="featuresVect")
      # Create the normalizer
      normalizer = Normalizer(inputCol="featuresVect", outputCol="features", p=1.0)

      stages = [rank_indexer] + [rank_encoder] + [assembler] + [normalizer]

      from pyspark.ml import Pipeline
      final_pipeline = Pipeline(
      stages = stages
      )

      pipelineModel = final_pipeline.fit(train)
      data = pipelineModel.transform(train)

      data.schema['features'].metadata

      ## empty dictionary

      ## excluding the normalizer results in this metadata:
      u'ml_attr': u'attrs': u'binary': [u'idx': 2, u'name': u'rank_enc_2',
      u'idx': 3, u'name': u'rank_enc_3',
      u'idx': 4, u'name': u'rank_enc_4'],
      u'numeric': [u'idx': 0, u'name': u'gre', u'idx': 1, u'name': u'gpa'],
      u'num_attrs': 5


      Is this normal behavior? How can I include a normalizer without losing this metadata?







      apache-spark pyspark metadata apache-spark-ml normalize






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 23:08









      user6910411

      33.8k976100




      33.8k976100










      asked Aug 15 '17 at 9:23









      CherylCheryl

      325




      325






















          1 Answer
          1






          active

          oldest

          votes


















          1














          In my opinion it doesn't make much sense to use Normalizer on One-hot encoded data in the first place. In Spark, OHE is useful for two type models:



          • Multinomial Naive Bayes.

          • Linear models.

          In the first case normalization will render features completely useless (multinomial model can fully utilize only binary features). In the second case it will make interpretation of the model close to impossible.



          Even if you ignore the above normalized data cannot be interpreted as binary features anymore, therefore discarding metadata seems to be a valid behavior.



          Related to Why does StandardScaler not attach metadata to the output column?






          share|improve this answer
























            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%2f45690052%2fspark-ml-normalizer-loses-metadata%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









            1














            In my opinion it doesn't make much sense to use Normalizer on One-hot encoded data in the first place. In Spark, OHE is useful for two type models:



            • Multinomial Naive Bayes.

            • Linear models.

            In the first case normalization will render features completely useless (multinomial model can fully utilize only binary features). In the second case it will make interpretation of the model close to impossible.



            Even if you ignore the above normalized data cannot be interpreted as binary features anymore, therefore discarding metadata seems to be a valid behavior.



            Related to Why does StandardScaler not attach metadata to the output column?






            share|improve this answer





























              1














              In my opinion it doesn't make much sense to use Normalizer on One-hot encoded data in the first place. In Spark, OHE is useful for two type models:



              • Multinomial Naive Bayes.

              • Linear models.

              In the first case normalization will render features completely useless (multinomial model can fully utilize only binary features). In the second case it will make interpretation of the model close to impossible.



              Even if you ignore the above normalized data cannot be interpreted as binary features anymore, therefore discarding metadata seems to be a valid behavior.



              Related to Why does StandardScaler not attach metadata to the output column?






              share|improve this answer



























                1












                1








                1







                In my opinion it doesn't make much sense to use Normalizer on One-hot encoded data in the first place. In Spark, OHE is useful for two type models:



                • Multinomial Naive Bayes.

                • Linear models.

                In the first case normalization will render features completely useless (multinomial model can fully utilize only binary features). In the second case it will make interpretation of the model close to impossible.



                Even if you ignore the above normalized data cannot be interpreted as binary features anymore, therefore discarding metadata seems to be a valid behavior.



                Related to Why does StandardScaler not attach metadata to the output column?






                share|improve this answer















                In my opinion it doesn't make much sense to use Normalizer on One-hot encoded data in the first place. In Spark, OHE is useful for two type models:



                • Multinomial Naive Bayes.

                • Linear models.

                In the first case normalization will render features completely useless (multinomial model can fully utilize only binary features). In the second case it will make interpretation of the model close to impossible.



                Even if you ignore the above normalized data cannot be interpreted as binary features anymore, therefore discarding metadata seems to be a valid behavior.



                Related to Why does StandardScaler not attach metadata to the output column?







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 15 '17 at 10:14

























                answered Aug 15 '17 at 10:05









                user6910411user6910411

                33.8k976100




                33.8k976100



























                    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%2f45690052%2fspark-ml-normalizer-loses-metadata%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







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3