Skip to content

factorial

factorial

Bases: designs

Super class for factorial designs.

Attributes:

Name Type Description
k int

The number of factors.

seed int

The seed for the random number generator.

Source code in spotpython/design/factorial.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class factorial(designs):
    """
    Super class for factorial designs.

    Attributes:
        k (int): The number of factors.
        seed (int): The seed for the random number generator.
    """

    def __init__(self, k: int = 2, seed: int = 123) -> None:
        """
        Initializes a factorial design object.

        Args:
            k (int): The number of factors. Defaults to 2.
            seed (int): The seed for the random number generator. Defaults to 123.
        """
        super().__init__(k, seed)

    def full_factorial(self, p: int) -> "np.ndarray":
        """
        Generates a full factorial design.

        Args:
            p (int): The number of levels for each factor.

        Returns:
            numpy.ndarray: A 2D array representing the full factorial design.

        Examples:
            >>> from spotpython.design.factorial import factorial
                factorial_design = factorial(k=2)
                factorial_design.full_factorial(p=2)
                array([[0., 0.],
                    [0., 1.],
                    [1., 0.],
                    [1., 1.]])
        """
        i = (slice(0, 1, p * 1j),) * self.k
        return mgrid[i].reshape(self.k, p**self.k).T

__init__(k=2, seed=123)

Initializes a factorial design object.

Parameters:

Name Type Description Default
k int

The number of factors. Defaults to 2.

2
seed int

The seed for the random number generator. Defaults to 123.

123
Source code in spotpython/design/factorial.py
15
16
17
18
19
20
21
22
23
def __init__(self, k: int = 2, seed: int = 123) -> None:
    """
    Initializes a factorial design object.

    Args:
        k (int): The number of factors. Defaults to 2.
        seed (int): The seed for the random number generator. Defaults to 123.
    """
    super().__init__(k, seed)

full_factorial(p)

Generates a full factorial design.

Parameters:

Name Type Description Default
p int

The number of levels for each factor.

required

Returns:

Type Description
ndarray

numpy.ndarray: A 2D array representing the full factorial design.

Examples:

>>> from spotpython.design.factorial import factorial
    factorial_design = factorial(k=2)
    factorial_design.full_factorial(p=2)
    array([[0., 0.],
        [0., 1.],
        [1., 0.],
        [1., 1.]])
Source code in spotpython/design/factorial.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def full_factorial(self, p: int) -> "np.ndarray":
    """
    Generates a full factorial design.

    Args:
        p (int): The number of levels for each factor.

    Returns:
        numpy.ndarray: A 2D array representing the full factorial design.

    Examples:
        >>> from spotpython.design.factorial import factorial
            factorial_design = factorial(k=2)
            factorial_design.full_factorial(p=2)
            array([[0., 0.],
                [0., 1.],
                [1., 0.],
                [1., 1.]])
    """
    i = (slice(0, 1, p * 1j),) * self.k
    return mgrid[i].reshape(self.k, p**self.k).T