def test_mul(self): a, b, c, dl, dn = self.a, self.b, self.c, self.dl, self.dn self.assertEqual(a, a * 1) self.assertEqual(a, 1 * a) self.assertEqual(Dim(np.array(a) * 2), a * a) self.assertEqual(Dim(np.array([3, 4, 6, 8, 10, 12, 14])), a * b) self.assertNotEqual(Dim(np.array([2, 4, 6, 8, 10, 12, 14])), a * b) self.assertEqual(a, a * dl) self.assertEqual(a, dl * a) assert (a * dn).anyisnan() assert (dn * a).anyisnan() self.assertEqual(2, (1 * c).ndim) self.assertEqual(2, (c * 1).ndim) self.assertEqual(1, (1 * a).ndim) self.assertEqual(1, (a * 1).ndim) self.assertEqual(2, (a * c).ndim) self.assertEqual(1, (a * b).ndim) self.assertEqual(2, (c * dl).ndim) self.assertEqual(2, (c * b).ndim) self.assertEqual(2, (c * dn).ndim) self.assertEqual(2, (dn * c).ndim) self.assertEqual(1, (dn * dn).ndim) self.assertEqual(1, (dn * dl).ndim) self.assertEqual(1, (b * dl).ndim)
def test_comp(self): # my_funcs = {"Abs": my_abs, "exp": my_exp, "log": my_log, 'cos': my_cos, 'sin': my_sin, # 'sqrt': my_sqrt, "Flat": my_flat, "Comp": my_comp, "Diff": my_diff, # "Quot": my_quot, "Self": my_self} a, b, c, dl, dn, dl2, dn2 = self.a, self.b, self.c, self.dl, self.dn, self.dl2, self.dn2 c5 = a.copy() c5 = Dim(np.array([c5] * 5)) my_funcs = self.dim_map func = my_funcs["MMul"] self.assertEqual(a, func(a)) self.assertEqual(b, func(b)) self.assertEqual(dl, func(dl)) assert func(dn).anyisnan() assert func(dn2).anyisnan() self.assertEqual(dl2**2, func(dl2)) self.assertEqual(Dim(2 * np.array(c)), func(c)) self.assertEqual(Dim(5 * np.array(c)), func(c5)) self.assertNotEqual(dl, func(3)) self.assertEqual(1, func(a).ndim) self.assertEqual(1, func(b).ndim) self.assertEqual(1, func(dl).ndim) self.assertEqual(1, func(dn).ndim) self.assertEqual(1, func(c).ndim) self.assertEqual(1, func(c5).ndim)
def test_dim_func(): a = 1 b = Dim(np.array([1, 2, 3, 4, 5])) c = Dim(np.array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])) d = Dim(np.array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])) e = Dim(np.array([[1, 2, 3, 4, 5] * self_grpup])) dim_func = funcs["dim_func"] dim_func(a) dim_func(b) dim_func(c) s = dim_func(d) s = dim_func(e)
def setUp(self): self.a = Dim([1, 2, 3, 4, 5, 6, 7]) self.b = Dim([2, 2, 3, 4, 5, 6, 7]) self.dl = dless self.dn = dnan self.c = Dim([[1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7]]) self.dl2 = Dim([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) self.dn2 = Dim( [[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]]) self.dim_map = dim_map()
def tsum(*ters, name="gx0"): """ Parameters ---------- ters: tuple of SymbolTerminalDetail SymbolTerminalDetail name: str sepcific the name of results. Returns ------- SymbolTerminalDetail """ for i in ters: assert isinstance(i, SymbolTerminalDetail), "only the SymbolTerminals can be added" assert all(ters[0].dim == i.dim for i in ters) values = np.array([i.value for i in ters]) dim = Dim(np.array([i.dim for i in ters])) sym = NewArray([i.sym for i in ters], shape=(len(ters),)) name = str(name) prob = sum([i.prob for i in ters]) / len(ters) res = SymbolTerminalDetail(values, name, dim=dim, prob=prob, init_sym=sym, init_name=str(list(sym))) return res
def dimf(dim): if isinstance(dim, Dim): if dim.ndim == 1: return dim elif dim.shape[0] == 2: if keep: return Dim(operation(*dim)) else: return Dim(operation(*dim)) else: if is_jump: return dim else: return Dim(operation(*dim)) else: # number return dim
def test_pow(self): a, b, c, dl, dn = self.a, self.b, self.c, self.dl, self.dn self.assertEqual(Dim(np.array(a) * 2), a**2) self.assertEqual(Dim(np.array(a) / 2), a**0.5) assert (4**a).anyisnan() assert (a**b).anyisnan() self.assertEqual(1, (1**c).ndim) self.assertEqual(2, (c**1).ndim) self.assertEqual(1, (1**a).ndim) self.assertEqual(1, (a**1).ndim) self.assertEqual(1, (a**c).ndim) self.assertEqual(1, (a**b).ndim) self.assertEqual(1, (c**dl).ndim) self.assertEqual(1, (c**b).ndim) self.assertEqual(1, (c**dn).ndim) self.assertEqual(1, (dn**c).ndim) self.assertEqual(1, (dn**dn).ndim) self.assertEqual(1, (dn**dl).ndim) self.assertEqual(1, (b**dl).ndim)
def test_all(self): a, b, c, dl, dn, dl2 = self.a, self.b, self.c, self.dl, self.dn, self.dl2 c5 = a.copy() c5 = Dim(np.array([c5] * 5)) my_funcs = self.dim_map MMul = my_funcs["MMul"] MDiv = my_funcs["MDiv"] ln = my_funcs["ln"] assert ln(MDiv(dl2 + MMul(c))).anyisnan() assert MDiv(dl2 + MMul(c)).anyisnan() assert (dl2 + MMul(c)).anyisnan()
def test_div(self): a, b, c, dl, dn = self.a, self.b, self.c, self.dl, self.dn self.assertEqual(a, a / 1) self.assertEqual(Dim(-np.array(a)), 1 / a) self.assertEqual(dless, a / a) self.a = Dim([1, 2, 3, 4, 5, 6, 7]) self.b = Dim([2, 2, 3, 4, 5, 6, 7]) self.assertEqual(Dim(np.array([-1, 0, 0, 0, 0, 0, 0])), a / b) self.assertNotEqual(Dim(np.array([2, 4, 6, 8, 10, 12, 14])), a / b) self.assertEqual(a, a / dl) self.assertEqual(Dim(-np.array(a)), dl / a) assert (a / dn).anyisnan() assert (dn / a).anyisnan() assert (dn / dn).anyisnan() self.assertEqual(2, (1 / c).ndim) self.assertEqual(2, (c / 1).ndim) self.assertEqual(1, (1 / a).ndim) self.assertEqual(1, (a / 1).ndim) self.assertEqual(2, (a / c).ndim) self.assertEqual(1, (a / b).ndim) self.assertEqual(2, (c / dl).ndim) self.assertEqual(2, (c / b).ndim) self.assertEqual(2, (c / dn).ndim) self.assertEqual(2, (dn / c).ndim) self.assertEqual(1, (dn / dn).ndim) self.assertEqual(1, (dn / dl).ndim) self.assertEqual(1, (b / dl).ndim)
from featurebox.symbol.base import SymbolSet, SymbolTree if __name__ == "__main__": # data data = load_boston() x = data["data"] y = data["target"] c = [6, 3, 4] # unit from sympy.physics.units import kg x_u = [kg] * 13 y_u = kg c_u = [dless, dless, dless] x, x_dim = Dim.convert_x(x, x_u, target_units=None, unit_system="SI") y, y_dim = Dim.convert_xi(y, y_u) c, c_dim = Dim.convert_x(c, c_u) t = time.time() # symbolset pset0 = SymbolSet() pset0.add_features(x, y, x_dim=x_dim, y_dim=y_dim, x_group=[[1, 2], [3, 4, 5]]) pset0.add_constants(c, c_dim=c_dim, c_prob=None) pset0.add_operations(power_categories=(2, 3, 0.5), categories=("Add", "Mul", "Sub", "Div", "exp"),