Load multiple saved machine learning models with relative features sets in pythonic way










0















I have a directory structure on my laptop as shown.



/Directory
1. file1.model
2. file2.model
3. file3.model



/Directory2
1. features1.csv
2. features2.csv
3. features3.csv



`def LoadModelFiles(self):
model1 = Directory/file1.model
model2 = Directory/file2.model
model3 = Directory/file3.model
return model1,model2, model3

def LoadFeaturesets(self):
feature_set1 = Directory/features1.csv
feature_set2 = Directory/features2.csv
feature_set3 = Directory/feature3.csv
return feature_set1,feature_set2,feature_set3

def Classifier(self):
model1,model2,model3 = self.LoadModelFiles()
set1, set2, set3 = self.LoadFeaturesets()

pred1 = model1.predict(feature1)
pred2 = model2.predict(feature2)
pred3 = model3.predict(feature3)

return pred1, pred2, pred3`


what if I have multiple file. The above code is bad as I am new to python.



I want to load them into different variable, so I wrote the code as shown. I can use those model variables to pass different set of feature stored in those csv files.



So even in Classifier method I have to write 3 prediction statements, what if I have N number of models with relative feature set.



Can we write a pythonic or few lines of code to eliminate duplicated lines and load the files.



Added thing is I am writing a web service where 1st model predictions(based on its predictions) invokes the next models.



Currently I have 15 model files. Where 1st model file invokes the next 14 files.



Thanks in advance.










share|improve this question
























  • What about using a list of objects? Something like: [ 'model': model1, 'set': set1, 'model': model2, 'set': set2, ... ]

    – Spencer Wieczorek
    Nov 16 '18 at 3:45












  • I tried that too. But Currently I have 15 model files and 15 feature sets. I don't want to use this technique too. Because I building am web service where 1st models output goes to second model and so on.

    – Vamsi nimmala
    Nov 16 '18 at 3:56















0















I have a directory structure on my laptop as shown.



/Directory
1. file1.model
2. file2.model
3. file3.model



/Directory2
1. features1.csv
2. features2.csv
3. features3.csv



`def LoadModelFiles(self):
model1 = Directory/file1.model
model2 = Directory/file2.model
model3 = Directory/file3.model
return model1,model2, model3

def LoadFeaturesets(self):
feature_set1 = Directory/features1.csv
feature_set2 = Directory/features2.csv
feature_set3 = Directory/feature3.csv
return feature_set1,feature_set2,feature_set3

def Classifier(self):
model1,model2,model3 = self.LoadModelFiles()
set1, set2, set3 = self.LoadFeaturesets()

pred1 = model1.predict(feature1)
pred2 = model2.predict(feature2)
pred3 = model3.predict(feature3)

return pred1, pred2, pred3`


what if I have multiple file. The above code is bad as I am new to python.



I want to load them into different variable, so I wrote the code as shown. I can use those model variables to pass different set of feature stored in those csv files.



So even in Classifier method I have to write 3 prediction statements, what if I have N number of models with relative feature set.



Can we write a pythonic or few lines of code to eliminate duplicated lines and load the files.



Added thing is I am writing a web service where 1st model predictions(based on its predictions) invokes the next models.



Currently I have 15 model files. Where 1st model file invokes the next 14 files.



Thanks in advance.










share|improve this question
























  • What about using a list of objects? Something like: [ 'model': model1, 'set': set1, 'model': model2, 'set': set2, ... ]

    – Spencer Wieczorek
    Nov 16 '18 at 3:45












  • I tried that too. But Currently I have 15 model files and 15 feature sets. I don't want to use this technique too. Because I building am web service where 1st models output goes to second model and so on.

    – Vamsi nimmala
    Nov 16 '18 at 3:56













0












0








0








I have a directory structure on my laptop as shown.



/Directory
1. file1.model
2. file2.model
3. file3.model



/Directory2
1. features1.csv
2. features2.csv
3. features3.csv



`def LoadModelFiles(self):
model1 = Directory/file1.model
model2 = Directory/file2.model
model3 = Directory/file3.model
return model1,model2, model3

def LoadFeaturesets(self):
feature_set1 = Directory/features1.csv
feature_set2 = Directory/features2.csv
feature_set3 = Directory/feature3.csv
return feature_set1,feature_set2,feature_set3

def Classifier(self):
model1,model2,model3 = self.LoadModelFiles()
set1, set2, set3 = self.LoadFeaturesets()

pred1 = model1.predict(feature1)
pred2 = model2.predict(feature2)
pred3 = model3.predict(feature3)

return pred1, pred2, pred3`


what if I have multiple file. The above code is bad as I am new to python.



I want to load them into different variable, so I wrote the code as shown. I can use those model variables to pass different set of feature stored in those csv files.



So even in Classifier method I have to write 3 prediction statements, what if I have N number of models with relative feature set.



Can we write a pythonic or few lines of code to eliminate duplicated lines and load the files.



Added thing is I am writing a web service where 1st model predictions(based on its predictions) invokes the next models.



Currently I have 15 model files. Where 1st model file invokes the next 14 files.



Thanks in advance.










share|improve this question
















I have a directory structure on my laptop as shown.



/Directory
1. file1.model
2. file2.model
3. file3.model



/Directory2
1. features1.csv
2. features2.csv
3. features3.csv



`def LoadModelFiles(self):
model1 = Directory/file1.model
model2 = Directory/file2.model
model3 = Directory/file3.model
return model1,model2, model3

def LoadFeaturesets(self):
feature_set1 = Directory/features1.csv
feature_set2 = Directory/features2.csv
feature_set3 = Directory/feature3.csv
return feature_set1,feature_set2,feature_set3

def Classifier(self):
model1,model2,model3 = self.LoadModelFiles()
set1, set2, set3 = self.LoadFeaturesets()

pred1 = model1.predict(feature1)
pred2 = model2.predict(feature2)
pred3 = model3.predict(feature3)

return pred1, pred2, pred3`


what if I have multiple file. The above code is bad as I am new to python.



I want to load them into different variable, so I wrote the code as shown. I can use those model variables to pass different set of feature stored in those csv files.



So even in Classifier method I have to write 3 prediction statements, what if I have N number of models with relative feature set.



Can we write a pythonic or few lines of code to eliminate duplicated lines and load the files.



Added thing is I am writing a web service where 1st model predictions(based on its predictions) invokes the next models.



Currently I have 15 model files. Where 1st model file invokes the next 14 files.



Thanks in advance.







python machine-learning data-science






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 4:03







Vamsi nimmala

















asked Nov 16 '18 at 3:12









Vamsi nimmalaVamsi nimmala

265




265












  • What about using a list of objects? Something like: [ 'model': model1, 'set': set1, 'model': model2, 'set': set2, ... ]

    – Spencer Wieczorek
    Nov 16 '18 at 3:45












  • I tried that too. But Currently I have 15 model files and 15 feature sets. I don't want to use this technique too. Because I building am web service where 1st models output goes to second model and so on.

    – Vamsi nimmala
    Nov 16 '18 at 3:56

















  • What about using a list of objects? Something like: [ 'model': model1, 'set': set1, 'model': model2, 'set': set2, ... ]

    – Spencer Wieczorek
    Nov 16 '18 at 3:45












  • I tried that too. But Currently I have 15 model files and 15 feature sets. I don't want to use this technique too. Because I building am web service where 1st models output goes to second model and so on.

    – Vamsi nimmala
    Nov 16 '18 at 3:56
















What about using a list of objects? Something like: [ 'model': model1, 'set': set1, 'model': model2, 'set': set2, ... ]

– Spencer Wieczorek
Nov 16 '18 at 3:45






What about using a list of objects? Something like: [ 'model': model1, 'set': set1, 'model': model2, 'set': set2, ... ]

– Spencer Wieczorek
Nov 16 '18 at 3:45














I tried that too. But Currently I have 15 model files and 15 feature sets. I don't want to use this technique too. Because I building am web service where 1st models output goes to second model and so on.

– Vamsi nimmala
Nov 16 '18 at 3:56





I tried that too. But Currently I have 15 model files and 15 feature sets. I don't want to use this technique too. Because I building am web service where 1st models output goes to second model and so on.

– Vamsi nimmala
Nov 16 '18 at 3:56












1 Answer
1






active

oldest

votes


















0














def load_model(model_path):
pass

def load_feature(feature_path):
pass

def predict(idx):
model = load_model('Directory/file.model'.format(idx))
features = load_feature('Directory/feature.csv'.format(idx))
return model.predict(features)

predictions = [predict(i) for i in range(n)]


Hope this would help






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%2f53330892%2fload-multiple-saved-machine-learning-models-with-relative-features-sets-in-pytho%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














    def load_model(model_path):
    pass

    def load_feature(feature_path):
    pass

    def predict(idx):
    model = load_model('Directory/file.model'.format(idx))
    features = load_feature('Directory/feature.csv'.format(idx))
    return model.predict(features)

    predictions = [predict(i) for i in range(n)]


    Hope this would help






    share|improve this answer



























      0














      def load_model(model_path):
      pass

      def load_feature(feature_path):
      pass

      def predict(idx):
      model = load_model('Directory/file.model'.format(idx))
      features = load_feature('Directory/feature.csv'.format(idx))
      return model.predict(features)

      predictions = [predict(i) for i in range(n)]


      Hope this would help






      share|improve this answer

























        0












        0








        0







        def load_model(model_path):
        pass

        def load_feature(feature_path):
        pass

        def predict(idx):
        model = load_model('Directory/file.model'.format(idx))
        features = load_feature('Directory/feature.csv'.format(idx))
        return model.predict(features)

        predictions = [predict(i) for i in range(n)]


        Hope this would help






        share|improve this answer













        def load_model(model_path):
        pass

        def load_feature(feature_path):
        pass

        def predict(idx):
        model = load_model('Directory/file.model'.format(idx))
        features = load_feature('Directory/feature.csv'.format(idx))
        return model.predict(features)

        predictions = [predict(i) for i in range(n)]


        Hope this would help







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 3:47









        何俊烽何俊烽

        1




        1





























            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%2f53330892%2fload-multiple-saved-machine-learning-models-with-relative-features-sets-in-pytho%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線