From e7a23dafbffd43c880a8903863e51bde831cae86 Mon Sep 17 00:00:00 2001 From: wuzhe1234 <46434750+wuzhe1234@users.noreply.github.com> Date: Thu, 21 Apr 2022 20:50:16 +0800 Subject: [PATCH] Change Power to a NpPairOperator (#1052) * Change Power to a NpPairOperator * Change Power to pair operator and use black to format --- qlib/data/base.py | 5 +++++ qlib/data/ops.py | 46 ++++++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/qlib/data/base.py b/qlib/data/base.py index 953c222535..cf32d333f7 100644 --- a/qlib/data/base.py +++ b/qlib/data/base.py @@ -112,6 +112,11 @@ def __pow__(self, other): return Power(self, other) + def __rpow__(self, other): + from .ops import Power # pylint: disable=C0415 + + return Power(other, self) + def __and__(self, other): from .ops import And # pylint: disable=C0415 diff --git a/qlib/data/ops.py b/qlib/data/ops.py index 4fc18e4e50..682be7834f 100644 --- a/qlib/data/ops.py +++ b/qlib/data/ops.py @@ -150,32 +150,6 @@ def __init__(self, feature): super(Log, self).__init__(feature, "log") -class Power(NpElemOperator): - """Feature Power - - Parameters - ---------- - feature : Expression - feature instance - - Returns - ---------- - Expression - a feature instance with power - """ - - def __init__(self, feature, exponent): - super(Power, self).__init__(feature, "power") - self.exponent = exponent - - def __str__(self): - return "{}({},{})".format(type(self).__name__, self.feature, self.exponent) - - def _load_internal(self, instrument, start_index, end_index, *args): - series = self.feature.load(instrument, start_index, end_index, *args) - return getattr(np, self.func)(series, self.exponent) - - class Mask(NpElemOperator): """Feature Mask @@ -333,6 +307,26 @@ def _load_internal(self, instrument, start_index, end_index, *args): return res +class Power(NpPairOperator): + """Power Operator + + Parameters + ---------- + feature_left : Expression + feature instance + feature_right : Expression + feature instance + + Returns + ---------- + Feature: + The bases in feature_left raised to the exponents in feature_right + """ + + def __init__(self, feature_left, feature_right): + super(Power, self).__init__(feature_left, feature_right, "power") + + class Add(NpPairOperator): """Add Operator