MCPcopy Create free account
hub / github.com/modAL-python/modAL / make_linear_combination

Function make_linear_combination

modAL/utils/combination.py:8–34  ·  view source on GitHub ↗

Takes the given functions and makes a function which returns the linear combination of the output of original functions. It works well with functions returning numpy arrays of the same shape. Args: *functions: Base functions for the linear combination.The functions shall have t

(*functions: Callable, weights: Optional[Sequence] = None)

Source from the content-addressed store, hash-verified

6
7
8def make_linear_combination(*functions: Callable, weights: Optional[Sequence] = None) -> Callable:
9 """
10 Takes the given functions and makes a function which returns the linear combination of the output of original
11 functions. It works well with functions returning numpy arrays of the same shape.
12
13 Args:
14 *functions: Base functions for the linear combination.The functions shall have the same argument and if they
15 return numpy arrays, the returned arrays shall have the same shape.
16 weights: Coefficients of the functions in the linear combination. The i-th given function will be multiplied
17 with weights[i].
18
19 Todo:
20 Doesn't it better to accept functions as a Collection explicitly?
21
22 Returns:
23 A function which returns the linear combination of the given functions output.
24 """
25 if weights is None:
26 weights = np.ones(shape=(len(functions)))
27 else:
28 assert len(functions) == len(weights), 'the length of weights must be the ' \
29 'same as the number of given functions'
30
31 def linear_combination(*args, **kwargs):
32 return sum((weights[i]*functions[i](*args, **kwargs) for i in range(len(weights))))
33
34 return linear_combination
35
36
37def make_product(*functions: Callable, exponents: Optional[Sequence] = None) -> Callable:

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…