MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / gaussian

Function gaussian

maths/gaussian.py:8–56  ·  view source on GitHub ↗

>>> float(gaussian(1)) 0.24197072451914337 >>> float(gaussian(24)) 3.342714441794458e-126 >>> float(gaussian(1, 4, 2)) 0.06475879783294587 >>> float(gaussian(1, 5, 3)) 0.05467002489199788 Supports NumPy Arrays Use numpy.meshgrid with this to generate gaus

(x, mu: float = 0.0, sigma: float = 1.0)

Source from the content-addressed store, hash-verified

6
7
8def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> float:
9 """
10 >>> float(gaussian(1))
11 0.24197072451914337
12
13 >>> float(gaussian(24))
14 3.342714441794458e-126
15
16 >>> float(gaussian(1, 4, 2))
17 0.06475879783294587
18
19 >>> float(gaussian(1, 5, 3))
20 0.05467002489199788
21
22 Supports NumPy Arrays
23 Use numpy.meshgrid with this to generate gaussian blur on images.
24 >>> import numpy as np
25 >>> x = np.arange(15)
26 >>> gaussian(x)
27 array([3.98942280e-01, 2.41970725e-01, 5.39909665e-02, 4.43184841e-03,
28 1.33830226e-04, 1.48671951e-06, 6.07588285e-09, 9.13472041e-12,
29 5.05227108e-15, 1.02797736e-18, 7.69459863e-23, 2.11881925e-27,
30 2.14638374e-32, 7.99882776e-38, 1.09660656e-43])
31
32 >>> float(gaussian(15))
33 5.530709549844416e-50
34
35 >>> gaussian([1,2, 'string'])
36 Traceback (most recent call last):
37 ...
38 TypeError: unsupported operand type(s) for -: 'list' and 'float'
39
40 >>> gaussian('hello world')
41 Traceback (most recent call last):
42 ...
43 TypeError: unsupported operand type(s) for -: 'str' and 'float'
44
45 >>> gaussian(10**234) # doctest: +IGNORE_EXCEPTION_DETAIL
46 Traceback (most recent call last):
47 ...
48 OverflowError: (34, 'Result too large')
49
50 >>> float(gaussian(10**-326))
51 0.3989422804014327
52
53 >>> float(gaussian(2523, mu=234234, sigma=3425))
54 0.0
55 """
56 return 1 / sqrt(2 * pi * sigma**2) * exp(-((x - mu) ** 2) / (2 * sigma**2))
57
58
59if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected