Nodes and weights

EvoDAG.node

The individuals. Variable is an input variable scaled by a coefficient and Function is the base of the functions that combine other individuals. The coefficients of a node are found by ordinary least squares (compute_weight), except those of the Naive Bayes and Centroid nodes, which come from the class labels.

class Variable[source]

Bases: object

Terminal node: a variable of the input (or a stored node) times a least-squares weight; arity 0.

__init__(variable, weight=None, ytr=None, mask=None, height=0, finite=True, **kwargs)[source]
property height

Height of the node in the DAG; zero for the nodes created directly from the inputs.

tostore()[source]

Return a light copy that keeps the variable, weight, position, height and fitness but not the outputs.

w2dict()[source]

Return the weight with each entry converted to plain Python lists, ready to be serialized.

tojson()[source]

Return a JSON string with the node class, variable, weight, position, height and fitness.

property position

Position where this variable is in the history

property fitness

Stores the fitness on the training set

property fitness_vs

Stores the fitness on the validation set

property variable

The variable is indicated by the position in EvoDAG.X

property weight

The weight is obtained by OLS on RootGP._ytr

compute_weight(r, ytr=None, mask=None)[source]

Returns the weight (w) using OLS of r * w = gp._ytr

raw_outputs(X)[source]

Return the outputs of the referenced variable on the training and test sets, before the weight.

set_weight(r)[source]

Compute the weight by least squares on the training set if not set; False if it cannot be computed.

eval(X)[source]

Evaluate the node on X, setting hy and hy_test to the weighted outputs; False if no weight is found.

isfinite()[source]

Test whether the predicted values are finite

normalize()[source]

Do nothing; the subclasses that need to rescale their outputs override it.

property hy

Predicted values of the training and validation set

labels(classifier=True)[source]

Labels predicted by the node on the training examples, remembered until hy is replaced or released: argmax of the outputs in classification and sign + 1 of the output in regression. They are what the agreement heuristic compares, thousands of times per fit.

property density

Fraction of the entries of the predicted values that are not zero (average over the outputs); it drives the choice of functions

property hy_test

Predicted values of the test set

class Function[source]

Bases: Variable

Base class of the nodes that combine other nodes; two arguments by default.

tostore()[source]

Return a light copy of the node as in Variable, which also stores the number of arguments.

signature()[source]

Return a string made of the symbol and the argument positions, used to detect repeated nodes.

hy2listM(X)[source]

Return the training and test outputs of the argument nodes X as lists, one list per output if multiple.

hy2list(X)[source]

Return the training and test outputs of the single argument node X as lists of vectors.

return_r_hr(r, hr)[source]

Unwrap r and hr when they hold a single vector and turn an empty hr into None.

class Function1[source]

Bases: Function

Base class of the nodes that apply a function to their arguments and fit one weight per output.

set_weight(r)[source]

Compute one least-squares weight per output from the function result r if not set; False on failure.

raw_outputs(X)[source]

Apply the function named by the symbol to the training and test outputs of the argument.

class Add[source]

Bases: Function

Sum of its arguments, each times a least-squares coefficient; arity 5 by default (at least 2); symbol +.

__init__(*args, **kwargs)[source]
static cumsum(r)[source]

Element-wise sum of a list of vectors.

set_weight(hy)[source]

Compute the least-squares coefficients of the arguments hy on the training set; False on failure.

eval(X)[source]

Evaluate the node on X as the weighted sum of its arguments on the training and test sets.

class Mul[source]

Bases: Function1

Element-wise product of its arguments (two by default), scaled by a least-squares weight; symbol *.

__init__(*args, **kwargs)[source]
static cumprod(r)[source]

Element-wise product of a list of vectors.

raw_outputs(X)[source]

Return the element-wise product of the arguments on the training and test sets, before the weight.

class Div[source]

Bases: Function1

Element-wise division of the first argument by the second, scaled by a least-squares weight; symbol /.

raw_outputs(X)[source]

Return the element-wise division of the arguments on the training and test sets, before the weight.

class Atan2[source]

Bases: Function1

Element-wise two-argument arctangent of its two arguments, scaled by a least-squares weight; symbol atan2.

raw_outputs(X)[source]

Return the two-argument arctangent of the arguments on the training and test sets, before the weight.

class Hypot[source]

Bases: Function1

Element-wise square root of the sum of the squares of its two arguments, scaled by a least-squares weight.

raw_outputs(X)[source]

Return the hypotenuse of the arguments on the training and test sets, before the weight.

class OneArg[source]

Bases: Function1

Base class of the nodes with a single argument that apply a function element-wise and scale it by a weight.

class Acos[source]

Bases: OneArg

Element-wise arccosine of its single argument, scaled by a least-squares weight; arity 1.

class Asin[source]

Bases: OneArg

Element-wise arcsine of its single argument, scaled by a least-squares weight; arity 1.

class Atan[source]

Bases: OneArg

Element-wise arctangent of its single argument, scaled by a least-squares weight; arity 1.

class Cos[source]

Bases: OneArg

Element-wise cosine of its single argument, scaled by a least-squares weight; arity 1.

class Sin[source]

Bases: OneArg

Element-wise sine of its single argument, scaled by a least-squares weight; arity 1.

class Tan[source]

Bases: OneArg

Element-wise tangent of its single argument, scaled by a least-squares weight; arity 1.

class Cosh[source]

Bases: OneArg

Element-wise hyperbolic cosine of its single argument, scaled by a least-squares weight; arity 1.

class Sinh[source]

Bases: OneArg

Element-wise hyperbolic sine of its single argument, scaled by a least-squares weight; arity 1.

class Tanh[source]

Bases: OneArg

Element-wise hyperbolic tangent of its single argument, scaled by a least-squares weight; arity 1.

class Acosh[source]

Bases: OneArg

Element-wise inverse hyperbolic cosine of its single argument, scaled by a least-squares weight; arity 1.

class Asinh[source]

Bases: OneArg

Element-wise inverse hyperbolic sine of its single argument, scaled by a least-squares weight; arity 1.

class Atanh[source]

Bases: OneArg

Element-wise inverse hyperbolic tangent of its single argument, scaled by a least-squares weight; arity 1.

class Exp[source]

Bases: OneArg

Element-wise exponential of its single argument, scaled by a least-squares weight; arity 1.

class Expm1[source]

Bases: OneArg

Element-wise exponential minus one of its single argument, scaled by a least-squares weight; arity 1.

class Log[source]

Bases: OneArg

Element-wise natural logarithm of its single argument, scaled by a least-squares weight; arity 1.

class Log2[source]

Bases: OneArg

Element-wise base 2 logarithm of its single argument, scaled by a least-squares weight; arity 1.

class Log10[source]

Bases: OneArg

Element-wise base 10 logarithm of its single argument, scaled by a least-squares weight; arity 1.

class Log1p[source]

Bases: OneArg

Element-wise natural logarithm of one plus the value of its single argument, scaled by a least-squares weight; arity 1.

class Lgamma[source]

Bases: OneArg

Element-wise logarithm of the absolute value of the gamma function of its single argument, scaled by a least-squares weight; arity 1.

class Sqrt[source]

Bases: OneArg

Element-wise square root of its single argument, scaled by a least-squares weight; arity 1.

class Sq[source]

Bases: OneArg

Element-wise square of its single argument, scaled by a least-squares weight; arity 1.

class Sign[source]

Bases: OneArg

Element-wise sign (-1, 0 or 1) of its single argument, scaled by a least-squares weight; arity 1.

class Fabs[source]

Bases: OneArg

Element-wise absolute value of its single argument, scaled by a least-squares weight; arity 1.

class Ceil[source]

Bases: OneArg

Element-wise ceiling of its single argument, scaled by a least-squares weight; arity 1.

class Floor[source]

Bases: OneArg

Element-wise floor of its single argument, scaled by a least-squares weight; arity 1.

class Min[source]

Bases: Function1

Element-wise minimum of its arguments (two by default, at least 2), scaled by a least-squares weight.

__init__(*args, **kwargs)[source]
static cummin(r)[source]

Element-wise minimum of a list of vectors.

raw_outputs(X)[source]

Return the element-wise minimum of the arguments on the training and test sets, before the weight.

class Max[source]

Bases: Function1

Element-wise maximum of its arguments (two by default, at least 2), scaled by a least-squares weight.

__init__(*args, **kwargs)[source]
static cummax(r)[source]

Element-wise maximum of a list of vectors.

raw_outputs(X)[source]

Return the element-wise maximum of the arguments on the training and test sets, before the weight.

class Argmax[source]

Bases: Function1

Position of the argument with the largest value at each element, scaled by a least-squares weight.

static argop(r)[source]

Position in the list of the largest value at each element of the vectors.

raw_outputs(X)[source]

Return the position of the largest argument on the training and test sets, before the weight.

class Argmin[source]

Bases: Argmax

Position of the argument with the smallest value at each element, scaled by a least-squares weight.

static argop(r)[source]

Position in the list of the smallest value at each element of the vectors.

class NaiveBayes[source]

Bases: Function

Gaussian naive Bayes over its arguments, with one output per class; classification only; symbol NB.

__init__(variable, naive_bayes=None, **kwargs)[source]
hy2listM(X)[source]

Return the training and test outputs of the argument nodes X as flat lists of vectors.

w2dict()[source]

Return the weight (class coefficients, used arguments and number of classes) with plain Python lists.

set_weight(X)[source]

Compute the per-class mean, variance and prior of each argument from the training set; False on failure.

eval(X)[source]

Evaluate the node on X as the class log-likelihoods of a Gaussian naive Bayes classifier.

normalize()[source]

Replace the outputs by their softmax, rescaled to the range from -1 to 1.

class NaiveBayesMN[source]

Bases: NaiveBayes

Multinomial naive Bayes over its arguments, with one output per class; classification only; symbol MN.

set_weight(X)[source]

Compute the per-class log proportions and log priors of the arguments from the training set.

eval(X)[source]

Evaluate the node on X as the unnormalized class probabilities of a multinomial naive Bayes classifier.

normalize()[source]

Divide each output by the sum over the classes and rescale it to the range from -1 to 1.

class MultipleVariables[source]

Bases: Add

Sum of all the input variables, each times a least-squares coefficient fitted jointly; symbol X.

raw_outputs(X)[source]

Return the outputs of all the variables on the training and test sets, before the weights.

set_weight(r)[source]

Compute the joint least-squares coefficients of the variables r, per output if multiple.

eval(X)[source]

Evaluate the node on X as the weighted sum of all the variables on the training and test sets.

class Centroid[source]

Bases: NaiveBayes

Negative squared Euclidean distance to the class centroids of its arguments, one output per class; symbol Centroid.

__init__(variable, **kwargs)[source]
set_weight(hy)[source]

Compute, from the training set, the negative mean of each argument in each class.

w2dict()[source]

Return the weight, one array per class, as plain Python lists.

eval(X)[source]

Evaluate the node on X as minus the squared distance of the arguments to each class centroid.

normalize()[source]

Replace each output by its exponential rescaled to the range from -1 to 1.

EvoDAG.naive_bayes

class NaiveBayes[source]

Computes the class statistics used by the naive Bayes nodes from the training examples selected by the mask.

__init__(mask=None, nclass=None, klass=None)[source]
coef(var)[source]

Return the mean, variance and prior of each class for the variable var.

coef_MN(var)[source]

Return the log proportion of the mass of the variable var in each class and the log prior of each class.