Giter Club home page Giter Club logo

my_py_tools's Introduction

Example.py

from sklearn.ensemble import RandomForestClassifier
from utils import *

All custom model must have methods:

  • fit(...)
  • predict(...)

You can add custom features like what I do below by making new class which inherited some class.

If your model doesn't have the required methods, create custom ones for it.

class MyRandomForestClf(RandomForestClassifier):
    def __init__(self, n_estimators_added_before_each_fit, **kwargs):
        self.n_estimators_added_before_each_fit = n_estimators_added_before_each_fit
        super(MyRandomForestClf, self).__init__(**kwargs)

    def fit(self, x, y, **kwargs):
        self.n_estimators += self.n_estimators_added_before_each_fit
        super(MyRandomForestClf, self).fit(x, y, **kwargs)

Tutorial

To use the following tutorial, please set the "attributes" array in configure yaml file contains string "hp".

Make an new model

init_model = MyRandomForestClf(n_estimators_added_before_each_fit=1)

Call this function, you'll get a dict of: key -> value: str(attribute) -> MyModel

attr_models = read_config_and_train(model=init_model, img_dir='data/images')

How to save model?

MyModel.save_model(filename)

Note: Not sure whether using this function to save model that is not from sklearn is appropriate.

example:

attr_models['hp'].save_model('filename_you_want')

How to load model?

  • Existing MyModel load other one: MyModel.load_model(file_path='filename_you_want')
  • Creating new MyModel object: MyModel(model_path_to_load='model_name_you_want_to_load')

Note: Not sure whether using this function to save model that is not from sklearn is appropriate.

example:

  • For existing MyModel:
attr_models['hp'].load_model('filename_you_want') # It'll cover your old model
  • For creating MyModel:
new_my_model = MyModel(model_path_to_load='filename_you_want')

Predict an image

     MyModel.predict_one_image(
         filename= ???(str),
         show_original_img= ???(bool),
         show_preprocessed_img= ???(bool,
     )

example:

pred = attr_models['hp'].predict_one_image(filename=filename,
                                  show_original_img=True,
                                  show_preprocessed_img=True)
print(pred)

my_py_tools's People

Contributors

liaowc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.