exceptions.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. """
  2. The :mod:`sklearn.exceptions` module includes all custom warnings and error
  3. classes used across scikit-learn.
  4. """
  5. __all__ = [
  6. "NotFittedError",
  7. "ConvergenceWarning",
  8. "DataConversionWarning",
  9. "DataDimensionalityWarning",
  10. "EfficiencyWarning",
  11. "FitFailedWarning",
  12. "SkipTestWarning",
  13. "UndefinedMetricWarning",
  14. "PositiveSpectrumWarning",
  15. "UnsetMetadataPassedError",
  16. ]
  17. class UnsetMetadataPassedError(ValueError):
  18. """Exception class to raise if a metadata is passed which is not explicitly \
  19. requested.
  20. .. versionadded:: 1.3
  21. Parameters
  22. ----------
  23. message : str
  24. The message
  25. unrequested_params : dict
  26. A dictionary of parameters and their values which are provided but not
  27. requested.
  28. routed_params : dict
  29. A dictionary of routed parameters.
  30. """
  31. def __init__(self, *, message, unrequested_params, routed_params):
  32. super().__init__(message)
  33. self.unrequested_params = unrequested_params
  34. self.routed_params = routed_params
  35. class NotFittedError(ValueError, AttributeError):
  36. """Exception class to raise if estimator is used before fitting.
  37. This class inherits from both ValueError and AttributeError to help with
  38. exception handling and backward compatibility.
  39. Examples
  40. --------
  41. >>> from sklearn.svm import LinearSVC
  42. >>> from sklearn.exceptions import NotFittedError
  43. >>> try:
  44. ... LinearSVC().predict([[1, 2], [2, 3], [3, 4]])
  45. ... except NotFittedError as e:
  46. ... print(repr(e))
  47. NotFittedError("This LinearSVC instance is not fitted yet. Call 'fit' with
  48. appropriate arguments before using this estimator."...)
  49. .. versionchanged:: 0.18
  50. Moved from sklearn.utils.validation.
  51. """
  52. class ConvergenceWarning(UserWarning):
  53. """Custom warning to capture convergence problems
  54. .. versionchanged:: 0.18
  55. Moved from sklearn.utils.
  56. """
  57. class DataConversionWarning(UserWarning):
  58. """Warning used to notify implicit data conversions happening in the code.
  59. This warning occurs when some input data needs to be converted or
  60. interpreted in a way that may not match the user's expectations.
  61. For example, this warning may occur when the user
  62. - passes an integer array to a function which expects float input and
  63. will convert the input
  64. - requests a non-copying operation, but a copy is required to meet the
  65. implementation's data-type expectations;
  66. - passes an input whose shape can be interpreted ambiguously.
  67. .. versionchanged:: 0.18
  68. Moved from sklearn.utils.validation.
  69. """
  70. class DataDimensionalityWarning(UserWarning):
  71. """Custom warning to notify potential issues with data dimensionality.
  72. For example, in random projection, this warning is raised when the
  73. number of components, which quantifies the dimensionality of the target
  74. projection space, is higher than the number of features, which quantifies
  75. the dimensionality of the original source space, to imply that the
  76. dimensionality of the problem will not be reduced.
  77. .. versionchanged:: 0.18
  78. Moved from sklearn.utils.
  79. """
  80. class EfficiencyWarning(UserWarning):
  81. """Warning used to notify the user of inefficient computation.
  82. This warning notifies the user that the efficiency may not be optimal due
  83. to some reason which may be included as a part of the warning message.
  84. This may be subclassed into a more specific Warning class.
  85. .. versionadded:: 0.18
  86. """
  87. class FitFailedWarning(RuntimeWarning):
  88. """Warning class used if there is an error while fitting the estimator.
  89. This Warning is used in meta estimators GridSearchCV and RandomizedSearchCV
  90. and the cross-validation helper function cross_val_score to warn when there
  91. is an error while fitting the estimator.
  92. .. versionchanged:: 0.18
  93. Moved from sklearn.cross_validation.
  94. """
  95. class SkipTestWarning(UserWarning):
  96. """Warning class used to notify the user of a test that was skipped.
  97. For example, one of the estimator checks requires a pandas import.
  98. If the pandas package cannot be imported, the test will be skipped rather
  99. than register as a failure.
  100. """
  101. class UndefinedMetricWarning(UserWarning):
  102. """Warning used when the metric is invalid
  103. .. versionchanged:: 0.18
  104. Moved from sklearn.base.
  105. """
  106. class PositiveSpectrumWarning(UserWarning):
  107. """Warning raised when the eigenvalues of a PSD matrix have issues
  108. This warning is typically raised by ``_check_psd_eigenvalues`` when the
  109. eigenvalues of a positive semidefinite (PSD) matrix such as a gram matrix
  110. (kernel) present significant negative eigenvalues, or bad conditioning i.e.
  111. very small non-zero eigenvalues compared to the largest eigenvalue.
  112. .. versionadded:: 0.22
  113. """
  114. class InconsistentVersionWarning(UserWarning):
  115. """Warning raised when an estimator is unpickled with a inconsistent version.
  116. Parameters
  117. ----------
  118. estimator_name : str
  119. Estimator name.
  120. current_sklearn_version : str
  121. Current scikit-learn version.
  122. original_sklearn_version : str
  123. Original scikit-learn version.
  124. """
  125. def __init__(
  126. self, *, estimator_name, current_sklearn_version, original_sklearn_version
  127. ):
  128. self.estimator_name = estimator_name
  129. self.current_sklearn_version = current_sklearn_version
  130. self.original_sklearn_version = original_sklearn_version
  131. def __str__(self):
  132. return (
  133. f"Trying to unpickle estimator {self.estimator_name} from version"
  134. f" {self.original_sklearn_version} when "
  135. f"using version {self.current_sklearn_version}. This might lead to breaking"
  136. " code or "
  137. "invalid results. Use at your own risk. "
  138. "For more info please refer to:\n"
  139. "https://scikit-learn.org/stable/model_persistence.html"
  140. "#security-maintainability-limitations"
  141. )