machine-learning-articles icon indicating copy to clipboard operation
machine-learning-articles copied to clipboard

Create Sklearn model

Open alfredoarturo opened this issue 4 years ago • 1 comments

TL;DR

The basis to build a customized model in Scikit-learn, it is like writing a Python class

Article Link

https://towardsdatascience.com/building-a-custom-model-in-scikit-learn-b0da965a1299

Author

Tim Book

Key Takeaways

  • You can create you customized model, the methods that every Scikit-learn model has are:

    • .fit()
    • .predict()
    • .score()
    • .set_params()
    • .get_params()
  • You can add all other methods you can imagine.

Useful Code Snippets

from self.preprocessing import OneHotEncoder
class KMeansTransformer(TransformerMixin):
    def __init__(self, *args, **args):
        self.model = KMeans(*args, **args)
    def fit(self, X):
        self.X = X
        self.model.fit(X)
    def transform(self, X):
        # Need to reshape into a column vector in order to use
        # the onehot encoder.
        cl = self.model.predict(X).reshape(-1, 1)
        
        self.oh = OneHotEncoder(
            categories="auto", 
            sparse=False,
            drop="first"
        )
        cl_matrix = self.oh.fit_transform(cl)      
 
        return np.hstack([self.X, cl_matrix])
    def fit_transform(self, X, y=None):
        self.fit(X)
        return self.transform(X)

Useful Tools

Comments/ Questions

alfredoarturo avatar Apr 12 '20 02:04 alfredoarturo

Really interesting to know that we can create a custom model with Scikit-learn

khuyentran1401 avatar Apr 12 '20 02:04 khuyentran1401