bgp.calculation package

Submodules

bgp.calculation.coefficient module

vector coef and vector const, which is a UndefinedFunction to excape the auto calculation of numpy to sympy.

class bgp.calculation.coefficient.CheckCoef(cof_list, cof_dict)

Bases: object

group the coef and pack the calculate part out of loop.

Parameters
  • cof_list (Sized) –

  • cof_dict (dict) –

dec(ls)
group(p, decimals=False)

change the p to grpup

property ind
class bgp.calculation.coefficient.Coef(name, arr)

Bases: sympy.core.function.UndefinedFunction

generate metaclass, the type of identity is .arr,.tp ,rather isinstance.

class bgp.calculation.coefficient.Const(name, arr)

Bases: sympy.core.function.UndefinedFunction

generate metaclass, the type of identity is .arr,.tp ,rather isinstance

bgp.calculation.coefficient.add_coefficient(expr01, inter_add=True, inner_add=False, vector_add=False, out_add=False, flat_add=False)

Try add the placeholder coefficient to sympy expression. 1. Add Wi,A,B normal coefficients to expression. 2. Add V, Vi vector coefficients to expression, for this type of coefficient , there should be with expr01.conu for Function(“MAdd”), Function(“MSub”). more details can be found in ..translate.simple

Parameters
  • expr01 (Expr) – sympy expressions

  • inter_add (bool) – bool

  • inner_add (bool) – bool

  • vector_add (bool) – bool

  • flat_add (bool) – bool

  • out_add (bool) – bool

Returns

Return type

expr

bgp.calculation.coefficient.cla(pre_y, cl=True)
bgp.calculation.coefficient.find_args(expr_, patten)

find the term of patten, judge by hash rather type

bgp.calculation.coefficient.flatten_add_f(expr01, cof_list, cof_dict, vector_add)
bgp.calculation.coefficient.get_args(expr, sole=True)
Parameters
  • expr (sympy.Expr) –

  • sole (only find unique term) –

Returns

Return type

list

bgp.calculation.coefficient.inner_add_f(expr01, cof_list, cof_dict, vector_add)
bgp.calculation.coefficient.out_add_f(expr01, cof_list, cof_dict, vector_add)
bgp.calculation.coefficient.replace_args(expr_, old, new)

find the term of patten, judge by hash rather type

bgp.calculation.coefficient.replace_args_first(expr_, old, new)

a

bgp.calculation.coefficient.try_add_coef(expr01, x, y, terminals, grid_x=None, filter_warning=True, inter_add=True, inner_add=False, vector_add=False, out_add=False, flat_add=False, np_maps=None, classification=False)

Try calculate predict y by sympy expression with coefficients. if except error return expr itself.

Parameters
  • flat_add (bool) – add flat coefficient or not

  • out_add – add outcoefficientt or not

  • vector_add (bool) – add vectorcoefficientt or not

  • expr01 (sympy.Expr) – sympy expressions

  • x (list of np.ndarray) – list of xi

  • y (np.ndarray) – y value

  • grid_x – new x to predict

  • terminals (list of sympy.Symbol) – features and constants

  • filter_warning (bool) – bool

  • inter_add (bool) – bool

  • inner_add (bool) – bool

  • np_maps (Callable) – user np.ndarray function

Returns

  • pre_y – np.array or None

  • expr01 (Expr) – New expr.

bgp.calculation.coefficient.try_add_coef_times(expr01, x, y, terminals, grid_x=None, filter_warning=True, inter_add=True, inner_add=False, vector_add=False, out_add=False, flat_add=False, np_maps=None, classification=False, random_state=0, return_expr=False, resample_number=500)

bgp.calculation.scores module

Notes

score method.

bgp.calculation.scores.calcualte_dim(expr01, terminals, dim_list, dim_maps=None)
Parameters
  • expr01 (Expr) – sympy expression.

  • terminals (list of sympy.Symbol) – features and constants

  • dim_list (list of Dim) – dims of features and constants

  • dim_maps (Callable) – user dim_maps

Returns

  • Dim – dimension

  • dim_score – is target dim or not

bgp.calculation.scores.calcualte_dim_score(expr01, terminals, dim_list, dim_type, fuzzy, dim_maps=None)
Parameters
  • expr01 (Expr) – sympy expression.

  • terminals (list of sympy.Symbol) – features and constants

  • dim_list (list of Dim) – dims of features and constants

  • dim_maps (Callable) – user dim_maps

  • dim_type (list of Dim) – target dim

  • fuzzy – fuzzy dim or not

Returns

  • Dim – dimension

  • dim_score – is target dim or not

bgp.calculation.scores.calculate_collect_(ind, context, x, y, terminals_and_constants_repr, gro_ter_con, dim_ter_con_list, dim_type, fuzzy, cv=1, refit=True, scoring=None, score_pen=(1,), add_coef=True, filter_warning=True, inter_add=True, inner_add=False, vector_add=False, out_add=False, flat_add=False, np_maps=None, classification=False, dim_maps=None, cal_dim=True, score_object='y', details=False)
bgp.calculation.scores.calculate_cv_score(expr01, x, y, terminals, scoring=None, score_pen=(1,), cv=5, refit=True, add_coef=True, filter_warning=True, inter_add=True, inner_add=False, vector_add=False, out_add=False, flat_add=False, np_maps=None, classification=False, score_object='y', details=False)

Use cv spilt for score, return the mean_test_score. Use cv spilt for predict, return the cv_predict_y.(have not be used)

Notes

if cv and refit, all the data is refit to determine the coefficients. Thus the expression is not compact with the this scores, when re-calculated by this expression.

Parameters
  • score_object – score by y or delta y

  • classification – classification or not

  • refit (True:) – use forced, refit the coefficient use all data.

  • cv (sklearn.model_selection._split._BaseKFold,int) – the shuffler must be False

  • vector_add

  • expr01 (Expr) – sympy expression.

  • x (list of np.ndarray) – list of xi

  • y (np.ndarray) – y value

  • terminals (list of sympy.Symbol) – features and constants

  • scoring (list of Callbale, default is [sklearn.metrics.r2_score,]) – See Also sklearn.metrics

  • score_pen (tuple of 1 or -1) – 1 : best is positive, worse -np.inf -1 : best is negative, worse np.inf 0 : best is positive , worse 0

  • add_coef (bool) – bool

  • filter_warning (bool) – bool

  • inter_add (bool) – bool

  • inner_add (bool) – bool

  • flat_add (bool) – bool

  • out_add (bool) – bool

  • np_maps (Callable) – user np.ndarray function

Returns

  • score (float) – score

  • expr01 (Expr) – New expr.

  • pre_y (np.ndarray or float) – np.array or None

bgp.calculation.scores.calculate_derivative_y(expr01, x, terminals, np_maps=None)

Something error for reference:

M. Schmidt, H. Lipson, Distilling free-form natural laws from experimental data, Science, 324 (2009), 81–85.

Parameters
  • expr01 (Expr) – sympy expression.

  • x (list of np.ndarray) – list of xi

  • terminals (list of sympy.Symbol) – features and constants

  • np_maps (Callable) – user np.ndarray function

Returns

  • pre_dy_all (np.ndarray or float) – pre-dy

  • dy_all (np.ndarray or float) – dy

bgp.calculation.scores.calculate_score(expr01, x, y, terminals, scoring=None, score_pen=(1,), add_coef=True, filter_warning=True, inter_add=True, inner_add=False, vector_add=False, out_add=False, flat_add=False, np_maps=None, classification=False, score_object='y', details=False)
Parameters
  • vector_add

  • expr01 (Expr) – sympy expression.

  • x (list of np.ndarray) – list of xi

  • y (np.ndarray) – y value

  • terminals (list of sympy.Symbol) – features and constants

  • scoring (list of Callbale, default is [sklearn.metrics.r2_score,]) – See Also sklearn.metrics

  • score_pen (tuple of 1 or -1) – 1 : best is positive, worse -np.inf -1 : best is negative, worse np.inf 0 : best is positive , worse 0

  • add_coef (bool) – bool

  • filter_warning (bool) – bool

  • inter_add (bool) – bool

  • inner_add (bool) – bool

  • np_maps (Callable) – user np.ndarray function

Returns

  • score (float) – score

  • expr01 (Expr) – New expr.

  • pre_y (np.ndarray or float) – np.array or None

bgp.calculation.scores.calculate_y(expr01, x, y, terminals, add_coef=True, x_test=None, y_test=None, filter_warning=True, inter_add=True, inner_add=False, vector_add=False, out_add=False, flat_add=False, np_maps=None, classification=False)
bgp.calculation.scores.calculate_y_unpack(expr01, x, terminals, classification=False)
bgp.calculation.scores.score_dim(dim_, dim_type, fuzzy=False)
bgp.calculation.scores.uniform_score(score_pen=1)

return the worse score

bgp.calculation.translate module

bgp.calculation.translate.compile_(expr, pset)

Compile the expression expr.

Parameters
  • expr – Expression to compile. It can either be a PrimitiveTree, a string of Python code or any object that when converted into string produced a valid Python code expression.

  • pset – Primitive set against which the expression is compile.

Returns

a function if the primitive set has 1 or more arguments, or return the results produced by evaluating the tree.

bgp.calculation.translate.compile_context(expr, context, gro_ter_con, simplify=True)

Compile the expression expr.

Parameters
  • expr – Expression to compile. It can either be a PrimitiveTree, a string of Python code or any object that when converted into string produced a valid Python code expression.

  • context – dict

  • simplify – bool

  • gro_ter_con – list if group_size

Returns

a function if the primitive set has 1 or more arguments, or return the results produced by evaluating the tree.

bgp.calculation.translate.general_expr(self, pset, simplifying=False)
Parameters
  • simplifying (bool) –

  • self (sympy.Expr) –

  • pset (SymbolSet) –

bgp.calculation.translate.general_expr_dict(self, expr_init_map, free_symbol, gsym_map, simplifying=False)

gen expr

bgp.calculation.translate.group_str(self, pset, feature_name=False)

return expr just build by input feature name.

Parameters
bgp.calculation.translate.simple(expr01, groups)

str to sympy.Expr function. add conv to MMdd and MMul. the calcualte conv need conform with np_func()!!

is_jump: jump the calculate >= 3 (group_size). keep: the calculate is return then input group_size or 1.

Module contents