QuantRocket logo

Disclaimer

Classifiers

A classifier is a function from an asset and a moment in time to a categorical output such as a string or integer label:

F(asset, timestamp) -> category

An example of a classifier producing a string output is the exchange of a security. To create this classifier, we'll have to import master.SecuritiesMaster.Exchange and use the latest attribute to instantiate our classifier:

Previously, we saw that the latest attribute produced an instance of a Factor. In this case, since the underlying data is of type string, latest produces a Classifier.

Similarly, a computation producing the sector of a security is a Classifier. To get the sector, we can again use the SecuritiesMaster dataset.

Building Filters from Classifiers

Classifiers can also be used to produce filters with methods like isnull, eq, and startswith. The full list of Classifier methods producing Filters can be found in the API Reference.

As an example, if we wanted a filter to select for securities trading on the New York Stock Exchange, we can use the eq method of our exchange classifier.

This filter will return True for securities having 'XNYS' as their Exchange.

Quantiles

Classifiers can also be produced from various Factor methods. The most general of these is the quantiles method which accepts a bin count as an argument. The quantiles method assigns a label from 0 to (bins - 1) to every non-NaN data point in the factor output and returns a Classifier with these labels. NaNs are labeled with -1. Aliases are available for quartiles (quantiles(4)), quintiles (quantiles(5)), and deciles (quantiles(10)). As an example, this is what a filter for the top decile of a factor might look like:

Let's put each of our classifiers into a pipeline and run it to see what they look like.

Classifiers are also useful for describing grouping keys for complex transformations on Factor outputs. Grouping operations such as demean are outside the scope of this tutorial.


Next Lesson: Datasets