File size: 2,301 Bytes
82bd48e
 
 
 
d39c0a6
 
 
 
82bd48e
 
 
d39c0a6
 
 
 
 
eb6195b
 
 
d39c0a6
 
 
 
 
 
 
 
82bd48e
 
 
d39c0a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82bd48e
 
 
 
3b15555
82bd48e
 
d05f2e2
82bd48e
 
d39c0a6
 
 
 
 
82bd48e
 
 
 
d05f2e2
3ae1f0b
 
82bd48e
3ae1f0b
d39c0a6
3ae1f0b
 
 
 
 
 
 
 
d39c0a6
 
 
 
 
 
1
2
3
4
5
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Operators

## Pre-defined

First, note that pretty much any valid Julia function which
takes one or two scalars as input, and returns on scalar as output,
is likely to be a valid operator[^1].
A selection of these and other valid operators are stated below.

**Binary**

- `+`
- `-`
- `*`
- `/`
- `^`
- `max`
- `min`
- `mod`
- `cond`
    - Equal to `(x, y) -> x > 0 ? y : 0`
- `greater`
    - Equal to `(x, y) -> x > y ? 1 : 0`
- `logical_or`
    - Equal to `(x, y) -> (x > 0 || y > 0) ? 1 : 0`
- `logical_and`
    - Equal to `(x, y) -> (x > 0 && y > 0) ? 1 : 0`

**Unary**

- `neg`
- `square`
- `cube`
- `exp`
- `abs`
- `log`
- `log10`
- `log2`
- `log1p`
- `sqrt`
- `sin`
- `cos`
- `tan`
- `sinh`
- `cosh`
- `tanh`
- `atan`
- `asinh`
- `acosh`
- `atanh_clip`
    - Equal to `atanh(mod(x + 1, 2) - 1)`
- `erf`
- `erfc`
- `gamma`
- `relu`
- `round`
- `floor`
- `ceil`
- `sign`

## Custom

Instead of passing a predefined operator as a string,
you can just define a custom function as Julia code. For example:

```python
    PySRRegressor(
        ...,
        unary_operators=["myfunction(x) = x^2"],
        binary_operators=["myotherfunction(x, y) = x^2*y"],
        extra_sympy_mappings={
            "myfunction": lambda x: x**2,
            "myotherfunction": lambda x, y: x**2 * y,
        },
    )
```


Make sure that it works with
`Float32` as a datatype (for default precision, or `Float64` if you set `precision=64`). That means you need to write `1.5f3`
instead of `1.5e3`, if you write any constant numbers, or simply convert a result to `Float64(...)`.

PySR expects that operators not throw an error for any input value over the entire real line from `-3.4e38` to `+3.4e38`.
Thus, for invalid inputs, such as negative numbers to a `sqrt` function, you may simply return a `NaN` of the same type as the input. For example,

```julia
my_sqrt(x) = x >= 0 ? sqrt(x) : convert(typeof(x), NaN)
```

would be a valid operator. The genetic algorithm
will preferentially selection expressions which avoid
any invalid values over the training dataset.


<!-- Footnote for 1: -->
<!-- (Will say "However, you may need to define a `extra_sympy_mapping`":) -->

[^1]: However, you will need to define a sympy equivalent in `extra_sympy_mapping` if you want to use a function not in the above list.