Coverage for colour/algebra/regression.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-15 19:01 +1300

1""" 

2Regression 

3========== 

4 

5Various objects to perform regression: 

6 

7- :func:`colour.algebra.least_square_mapping_MoorePenrose`: *Least-squares* 

8 mapping using *Moore-Penrose* inverse. 

9 

10References 

11---------- 

12- :cite:`Finlayson2015` : Finlayson, G. D., MacKiewicz, M., & Hurlbert, A. 

13 (2015). Color Correction Using Root-Polynomial Regression. IEEE 

14 Transactions on Image Processing, 24(5), 1460-1470. 

15 doi:10.1109/TIP.2015.2405336 

16""" 

17 

18from __future__ import annotations 

19 

20import typing 

21 

22import numpy as np 

23 

24if typing.TYPE_CHECKING: 

25 from colour.hints import ArrayLike, NDArrayFloat 

26 

27__author__ = "Colour Developers" 

28__copyright__ = "Copyright 2013 Colour Developers" 

29__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" 

30__maintainer__ = "Colour Developers" 

31__email__ = "colour-developers@colour-science.org" 

32__status__ = "Production" 

33 

34__all__ = [ 

35 "least_square_mapping_MoorePenrose", 

36] 

37 

38 

39def least_square_mapping_MoorePenrose(y: ArrayLike, x: ArrayLike) -> NDArrayFloat: 

40 """ 

41 Compute the *least-squares* mapping from dependent variable :math:`y` to 

42 independent variable :math:`x` using *Moore-Penrose* inverse. 

43 

44 Parameters 

45 ---------- 

46 y 

47 Dependent and already known :math:`y` variable. 

48 x 

49 Independent :math:`x` variable(s) values corresponding with :math:`y` 

50 variable. 

51 

52 Returns 

53 ------- 

54 :class:`numpy.ndarray` 

55 *Least-squares* mapping matrix. 

56 

57 References 

58 ---------- 

59 :cite:`Finlayson2015` 

60 

61 Examples 

62 -------- 

63 >>> prng = np.random.RandomState(2) 

64 >>> y = prng.random_sample((24, 3)) 

65 >>> x = y + (prng.random_sample((24, 3)) - 0.5) * 0.5 

66 >>> least_square_mapping_MoorePenrose(y, x) # doctest: +ELLIPSIS 

67 array([[ 1.0526376..., 0.1378078..., -0.2276339...], 

68 [ 0.0739584..., 1.0293994..., -0.1060115...], 

69 [ 0.0572550..., -0.2052633..., 1.1015194...]]) 

70 """ 

71 

72 y = np.atleast_2d(y) 

73 x = np.atleast_2d(x) 

74 

75 return np.dot(np.transpose(x), np.linalg.pinv(np.transpose(y)))