Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Custom classifiers possible?

Hi Q-brain

After looking through the forum and trying to build my own, does anyone have a solution for how to build a custom classifier?
I know you can use the quantiles and other factor methods, but I need something a little more involved, so anyone has an idea on how to do that I would greatly appreciate some hints. So if I have a custom factor that outputs integers, can I use that as a classifier?

I guess my question is if there is anyway to force a factor into being a classifier object or any workaround?

Best,
Bjarke

5 responses

Yes, one can create custom classifiers. Here's an example

# Here is a simple custom classifier which randomly assigns each security to a sample 'universe'. 

from from quantopian.pipeline import  CustomClassifier  

class RandomUniverse(CustomClassifier):  
    inputs = []  
    window_length = 1  
    dtype = np.int64  
    missing_value = 9999  
    params = ('universes',)

    def compute(self, today, assets, out, universes):  
        out[:] = np.random.randint(universes, size=assets.size)

The key is to subclass it as "CustomClassifier" and then to specify the 'dtype' as integer or a category and then finally specify a 'missing_value' parameter (what to assign if the classifier doesn't assign anything). I've personally only ever outputted integers (as in the example above).

Attached is an example notebook.

Thanks a lot. I will look trough this tomorrow and get back to you if I made it work or not. Couldn't find that solution in the docs!

Hi Dan

Thanks a lot. I figured it out in the end, but wouldn't have been possible without your help. How would I know or find out that there is a CustomClassifier object class if I didn't ask and you were so helpful to show me it?

Good question about "How would I know...". The Quantopian documentation, while excelent, isn't complete and seems to really highlight only the things they assume most people will/should be digging into. The ultimate repository and documentation for the zipline code is on github (see https://www.zipline.io/index.html ). You can then get to the actual open source code from there and read the line comments etc.

I actually find google pretty good for searching. In this case, since there is a CustomFactor and a CustomFilter I wondered if there wasn't a CustomClassifier too. I simply searched for 'zipline customclassifier' and voila the first link is https://github.com/quantopian/zipline/blob/master/zipline/pipeline/classifiers/classifier.py . I scrolled through the code and on line 485 (if it hasn't been updated lately) I found the class definition for CustomClassifier. It took a bit of trial and error after that.

So, my advice. Use google to search for things and add the term zipline and/or quantopian. Google actually searches the Quantopian forums better than the built in search (go figure).

Hope that helps.

Hi Dan,

Could you provide please an example of CustomClassifier with Strings?
I notice that's necessary to use the class LabelArray but I cannot figure how.

Thanks,
Costantino