Utilities & Notebook Helpers
Provides some utilities widely used by other modules
- utils.remove_all(item, seq)[source]
Return a copy of seq (or string) with all occurrences of item removed.
- utils.mode(data)[source]
Return the most common data item. If there are ties, return any one of them.
- utils.identity(x)
- utils.argmin_random_tie(seq, key=<function <lambda>>)[source]
Return a minimum element of seq; break ties at random.
- utils.argmax_random_tie(seq, key=<function <lambda>>)[source]
Return an element with highest fn(seq[i]) score; break ties at random.
- utils.histogram(values, mode=0, bin_function=None)[source]
Return a list of (value, count) pairs, summarizing the input values. Sorted by increasing value, or if mode=1, by decreasing count. If bin_function is given, map it over values first.
- utils.element_wise_product(x, y)[source]
Return vector as an element-wise product of vectors x and y.
- utils.matrix_multiplication(x, *y)[source]
Return a matrix as a matrix-multiplication of x and arbitrary number of matrices *y.
- utils.weighted_sample_with_replacement(n, seq, weights)[source]
Pick n samples from seq at random, with replacement, with the probability of each element in proportion to its corresponding weight.
- utils.weighted_sampler(seq, weights)[source]
Return a random-sample function that picks from seq weighted by weights.
- utils.rounder(numbers, d=4)[source]
Round a single number, or sequence of numbers, to d decimal places.
- utils.num_or_str(x: str) int | float | str[source]
The argument is a string; convert to a number if possible, or strip it.
- utils.euclidean_distance(x, y) float[source]
Return the Euclidean (L2) distance between vectors x and y.
- utils.manhattan_distance(x, y) float[source]
Return the Manhattan (L1) distance between vectors x and y.
- utils.hamming_distance(x, y) int[source]
Return the number of positions at which vectors x and y differ.
- utils.cross_entropy_loss(x, y) float[source]
Return the mean binary cross-entropy loss between targets x and predictions y.
- utils.mean_squared_error_loss(x, y) float[source]
Return the mean squared error loss between vectors x and y.
- utils.ms_error(x, y) float[source]
Return the mean of the squared differences between vectors x and y.
- utils.mean_error(x, y) float[source]
Return the mean of the absolute differences between vectors x and y.
- utils.mean_boolean_error(x, y) float[source]
Return the fraction of positions at which vectors x and y differ.
- utils.random_weights(min_value: float, max_value: float, num_weights: int) list[source]
Return a list of num_weights random floats drawn uniformly from [min_value, max_value].
- utils.sigmoid_derivative(value)[source]
Return the derivative of the sigmoid, given its already-computed output value.
- utils.elu(x: float, alpha: float = 0.01) float[source]
Return the Exponential Linear Unit activation of x.
- utils.elu_derivative(value: float, alpha: float = 0.01) float[source]
Return the derivative of the ELU activation, given its output value.
- utils.tanh_derivative(value)[source]
Return the derivative of tanh, given its already-computed output value.
- utils.leaky_relu(x: float, alpha: float = 0.01) float[source]
Return the Leaky ReLU activation of x (slope alpha for negative inputs).
- utils.leaky_relu_derivative(value: float, alpha: float = 0.01) float[source]
Return the derivative of the Leaky ReLU activation, given its output value.
- utils.relu(x: float) float[source]
Return the Rectified Linear Unit activation of x, i.e. max(0, x).
- utils.relu_derivative(value: float) int[source]
Return the derivative of the ReLU activation, given its output value.
- utils.gaussian(mean: float, st_dev: float, x: float) float[source]
Given the mean and standard deviation of a distribution, it returns the probability of x.
- utils.linear_kernel(x, y=None)[source]
Return the linear kernel (dot product) between x and y; defaults y to x.
- utils.polynomial_kernel(x, y=None, degree=2.0)[source]
Return the polynomial kernel (1 + x.y)**degree between x and y; defaults y to x.
- utils.rbf_kernel(x, y=None, gamma=None)[source]
Radial-basis function kernel (aka squared-exponential kernel).
- utils.turn_heading(heading, inc, headings=[(1, 0), (0, 1), (-1, 0), (0, -1)])[source]
Return the heading reached by turning inc steps around the list of headings.
- utils.turn_right(heading)[source]
Return the heading obtained by turning right (clockwise) from heading.
- utils.turn_left(heading)[source]
Return the heading obtained by turning left (counter-clockwise) from heading.
- class utils.injection(**kwds)[source]
Bases:
objectDependency injection of temporary values for global functions/classes/etc. E.g., with injection(DataBase=MockDataBase): …
- utils.memoize(fn, slot=None, maxsize=32)[source]
Memoize fn: make it remember the computed value for any argument list. If slot is specified, store result in that slot of first argument. If slot is false, use lru_cache for caching the values.
- utils.print_table(table, header=None, sep=' ', numfmt='{}')[source]
Print a list of lists as a table, so that columns line up nicely. header, if specified, will be printed as the first row. numfmt is the format for all numbers; you might want e.g. ‘{:.2f}’. (If you want different formats in different columns, don’t use print_table.) sep is the separator between columns.
- utils.open_data(name, mode='r')[source]
Open and return the file named name from the aima-data directory.
- utils.failure_test(algorithm, tests)[source]
Grades the given algorithm based on how many tests it passes. Most algorithms have arbitrary output on correct execution, which is difficult to check for correctness. On the other hand, a lot of algorithms output something particular on fail (for example, False, or None). tests is a list with each element in the form: (values, failure_output).
- class utils.Expr(op, *args)[source]
Bases:
objectA mathematical expression with an operator and 0 or more arguments. op is a str like ‘+’ or ‘sin’; args are Expressions. Expr(‘x’) or Symbol(‘x’) creates a symbol (a nullary Expr). Expr(‘-’, x) creates a unary; Expr(‘+’, x, 1) creates a binary.
- class utils.PartialExpr(op, lhs)[source]
Bases:
objectGiven ‘P |’==>’| Q, first form PartialExpr(‘==>’, P), then combine with Q.
- utils.expr(x)[source]
Shortcut to create an Expression. x is a str in which: - identifiers are automatically defined as Symbols. - ==> is treated as an infix |’==>’|, as are <== and <=>. If x is already an Expression, it is returned unchanged. Example:
>>> expr('P & Q ==> Q') ((P & Q) ==> Q)
- utils.expr_handle_infix_ops(x)[source]
Given a str, return a new str with ==> replaced by |’==>’|, etc.
>>> expr_handle_infix_ops('P ==> Q') "P |'==>'| Q"
- class utils.defaultkeydict[source]
Bases:
defaultdictLike defaultdict, but the default_factory is a function of the key. >>> d = defaultkeydict(len); d[‘four’] 4
- class utils.hashabledict[source]
Bases:
dictAllows hashing by representing a dictionary as tuple of key:value pairs. May cause problems as the hash value may change during runtime.
- class utils.PriorityQueue(order='min', f=<function PriorityQueue.<lambda>>)[source]
Bases:
objectA Queue in which the minimum (or maximum) element (as determined by f and order) is returned first. If order is ‘min’, the item with minimum f(x) is returned first; if order is ‘max’, then it is the item with maximum f(x). Also supports dict-like lookup.
- class utils.Bool[source]
Bases:
intJust like bool, except values display as ‘T’ and ‘F’ instead of ‘True’ and ‘False’.
Provides some utilities widely used by other modules
- class utils4e.PriorityQueue(order='min', f=<function PriorityQueue.<lambda>>)[source]
Bases:
objectA Queue in which the minimum (or maximum) element (as determined by f and order) is returned first. If order is ‘min’, the item with minimum f(x) is returned first; if order is ‘max’, then it is the item with maximum f(x). Also supports dict-like lookup.
- utils4e.remove_all(item, seq)[source]
Return a copy of seq (or string) with all occurrences of item removed.
- utils4e.mode(data)[source]
Return the most common data item. If there are ties, return any one of them.
- utils4e.identity(x)
- utils4e.argmin_random_tie(seq, key=<function <lambda>>)[source]
Return a minimum element of seq; break ties at random.
- utils4e.argmax_random_tie(seq, key=<function <lambda>>)[source]
Return an element with highest fn(seq[i]) score; break ties at random.
- utils4e.histogram(values, mode=0, bin_function=None)[source]
Return a list of (value, count) pairs, summarizing the input values. Sorted by increasing value, or if mode=1, by decreasing count. If bin_function is given, map it over values first.
- utils4e.element_wise_product(x, y)[source]
Return the element-wise product of x and y, recursing into nested iterables.
Scalars are multiplied directly; iterables must have matching lengths.
- utils4e.scalar_vector_product(x, y)[source]
Return vector as a product of a scalar and a vector recursively.
- utils4e.weighted_sample_with_replacement(n, seq, weights)[source]
Pick n samples from seq at random, with replacement, with the probability of each element in proportion to its corresponding weight.
- utils4e.weighted_sampler(seq, weights)[source]
Return a random-sample function that picks from seq weighted by weights.
- utils4e.rounder(numbers, d=4)[source]
Round a single number, or sequence of numbers, to d decimal places.
- utils4e.num_or_str(x: str) int | float | str[source]
The argument is a string; convert to a number if possible, or strip it.
- utils4e.euclidean_distance(x, y) float[source]
Return the Euclidean (L2) distance between vectors x and y.
- utils4e.manhattan_distance(x, y) float[source]
Return the Manhattan (L1) distance between vectors x and y.
- utils4e.hamming_distance(x, y) int[source]
Return the number of positions at which vectors x and y differ.
- utils4e.ms_error(x, y) float[source]
Return the mean of the squared differences between vectors x and y.
- utils4e.mean_error(x, y) float[source]
Return the mean of the absolute differences between vectors x and y.
- utils4e.mean_boolean_error(x, y) float[source]
Return the fraction of positions at which vectors x and y differ.
- utils4e.cross_entropy_loss(x, y) float[source]
Cross entropy loss function. x and y are 1D iterable objects.
- utils4e.mean_squared_error_loss(x, y) float[source]
Min square loss function. x and y are 1D iterable objects.
- utils4e.random_weights(min_value: float, max_value: float, num_weights: int) list[source]
Return a list of num_weights random floats drawn uniformly from [min_value, max_value].
- utils4e.gaussian_kernel(size=3)[source]
Return a length-size 1D Gaussian kernel centred at the middle (fixed st_dev 0.1).
- utils4e.gaussian_kernel_1D(size=3, sigma=0.5)[source]
Return a length-size 1D Gaussian kernel centred at the middle with st_dev sigma.
- utils4e.gaussian_kernel_2D(size=3, sigma=0.5)[source]
Return a size x size 2D Gaussian kernel with st_dev sigma, normalized to sum to 1.
- utils4e.gaussian(mean: float, st_dev: float, x: float) float[source]
Given the mean and standard deviation of a distribution, it returns the probability of x.
- utils4e.linear_kernel(x, y=None)[source]
Return the linear kernel (dot product) between x and y; defaults y to x.
- utils4e.polynomial_kernel(x, y=None, degree=2.0)[source]
Return the polynomial kernel (1 + x.y)**degree between x and y; defaults y to x.
- utils4e.rbf_kernel(x, y=None, gamma=None)[source]
Radial-basis function kernel (aka squared-exponential kernel).
- utils4e.turn_heading(heading, inc, headings=[(1, 0), (0, 1), (-1, 0), (0, -1)])[source]
Return the heading reached by turning inc steps around the list of headings.
- utils4e.turn_right(heading)[source]
Return the heading obtained by turning right (clockwise) from heading.
- utils4e.turn_left(heading)[source]
Return the heading obtained by turning left (counter-clockwise) from heading.
- class utils4e.injection(**kwds)[source]
Bases:
objectDependency injection of temporary values for global functions/classes/etc. E.g., with injection(DataBase=MockDataBase): …
- utils4e.memoize(fn, slot=None, maxsize=32)[source]
Memoize fn: make it remember the computed value for any argument list. If slot is specified, store result in that slot of first argument. If slot is false, use lru_cache for caching the values.
- utils4e.print_table(table, header=None, sep=' ', numfmt='{}')[source]
Print a list of lists as a table, so that columns line up nicely. header, if specified, will be printed as the first row. numfmt is the format for all numbers; you might want e.g. ‘{:.2f}’. (If you want different formats in different columns, don’t use print_table.) sep is the separator between columns.
- utils4e.open_data(name, mode='r')[source]
Open and return the file named name from the aima-data directory.
- utils4e.failure_test(algorithm, tests)[source]
Grades the given algorithm based on how many tests it passes. Most algorithms have arbitrary output on correct execution, which is difficult to check for correctness. On the other hand, a lot of algorithms output something particular on fail (for example, False, or None). tests is a list with each element in the form: (values, failure_output).
- class utils4e.Expr(op, *args)[source]
Bases:
objectA mathematical expression with an operator and 0 or more arguments. op is a str like ‘+’ or ‘sin’; args are Expressions. Expr(‘x’) or Symbol(‘x’) creates a symbol (a nullary Expr). Expr(‘-’, x) creates a unary; Expr(‘+’, x, 1) creates a binary.
- utils4e.symbols(names)[source]
Return a tuple of Symbols; names is a comma/whitespace delimited str.
- class utils4e.PartialExpr(op, lhs)[source]
Bases:
objectGiven ‘P |’==>’| Q, first form PartialExpr(‘==>’, P), then combine with Q.
- utils4e.expr(x)[source]
Shortcut to create an Expression. x is a str in which: - identifiers are automatically defined as Symbols. - ==> is treated as an infix |’==>’|, as are <== and <=>. If x is already an Expression, it is returned unchanged. Example:
>>> expr('P & Q ==> Q') ((P & Q) ==> Q)
- utils4e.expr_handle_infix_ops(x)[source]
Given a str, return a new str with ==> replaced by |’==>’|, etc.
>>> expr_handle_infix_ops('P ==> Q') "P |'==>'| Q"
- class utils4e.defaultkeydict[source]
Bases:
defaultdictLike defaultdict, but the default_factory is a function of the key. >>> d = defaultkeydict(len); d[‘four’] 4
- class utils4e.hashabledict[source]
Bases:
dictAllows hashing by representing a dictionary as tuple of key:value pairs. May cause problems as the hash value may change during runtime.
- class utils4e.MCT_Node(parent=None, state=None, U=0, N=0)[source]
Bases:
objectNode in the Monte Carlo search tree, keeps track of the children states.