mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-26 13:18:31 +08:00
Classifiers are computations that represent grouping keys. They can be used in conjuction with normalization functions like ``zscore`` or ``demean`` to perform normalizations over subsets of a dataset. Notable changes: - Added ``demean()`` and ``zscore()`` methods to ``Factor``. - Added a classifier versions of ``Latest`` and ``CustomTermMixin``. The .latest attribute of int64 dataset columns no produces a classifier by default. - Added ``Everything``, a classifier that maps all data to the same value. - Added ``zipline.lib.normalize``, which implements a naive, pure-Python grouped normalize function. This will likely be moved to Cython in a subsequent PR.
55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
"""
|
|
classifier.py
|
|
"""
|
|
from numpy import zeros, where
|
|
|
|
from zipline.errors import UnsupportedDataType
|
|
from zipline.pipeline.term import ComputableTerm
|
|
from zipline.utils.numpy_utils import int64_dtype
|
|
|
|
from ..mixins import CustomTermMixin, PositiveWindowLengthMixin
|
|
|
|
|
|
class Classifier(ComputableTerm):
|
|
|
|
def _validate(self):
|
|
# Run superclass validation first so that we handle `dtype not passed`
|
|
# before this.
|
|
retval = super(Classifier, self)._validate()
|
|
# TODO: Support strings here.
|
|
if self.dtype != int64_dtype:
|
|
raise UnsupportedDataType(
|
|
typename=type(self).__name__,
|
|
dtype=self.dtype
|
|
)
|
|
return retval
|
|
|
|
|
|
class Everything(Classifier):
|
|
"""
|
|
A trivial classifier that classifies everything the same.
|
|
"""
|
|
dtype = int64_dtype
|
|
window_length = 0
|
|
inputs = ()
|
|
missing_value = -1
|
|
|
|
def _compute(self, arrays, dates, assets, mask):
|
|
return where(
|
|
mask,
|
|
zeros(shape=mask.shape, dtype=int64_dtype),
|
|
self.missing_value,
|
|
)
|
|
|
|
|
|
class CustomClassifier(PositiveWindowLengthMixin, CustomTermMixin, Classifier):
|
|
"""
|
|
Base class for user-defined Classifiers.
|
|
|
|
See Also
|
|
--------
|
|
zipline.pipeline.CustomFactor
|
|
zipline.pipeline.CustomFilter
|
|
"""
|
|
pass
|