You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.2 KiB

4 years ago
  1. """
  2. The :mod:`sklearn.exceptions` module includes all custom warnings and error
  3. classes used across scikit-learn.
  4. """
  5. __all__ = ['NotFittedError',
  6. 'ChangedBehaviorWarning',
  7. 'ConvergenceWarning',
  8. 'DataConversionWarning',
  9. 'DataDimensionalityWarning',
  10. 'EfficiencyWarning',
  11. 'FitFailedWarning',
  12. 'NonBLASDotWarning',
  13. 'SkipTestWarning',
  14. 'UndefinedMetricWarning']
  15. class NotFittedError(ValueError, AttributeError):
  16. """Exception class to raise if estimator is used before fitting.
  17. This class inherits from both ValueError and AttributeError to help with
  18. exception handling and backward compatibility.
  19. Examples
  20. --------
  21. >>> from sklearn.svm import LinearSVC
  22. >>> from sklearn.exceptions import NotFittedError
  23. >>> try:
  24. ... LinearSVC().predict([[1, 2], [2, 3], [3, 4]])
  25. ... except NotFittedError as e:
  26. ... print(repr(e))
  27. ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
  28. NotFittedError('This LinearSVC instance is not fitted yet'...)
  29. .. versionchanged:: 0.18
  30. Moved from sklearn.utils.validation.
  31. """
  32. class ChangedBehaviorWarning(UserWarning):
  33. """Warning class used to notify the user of any change in the behavior.
  34. .. versionchanged:: 0.18
  35. Moved from sklearn.base.
  36. """
  37. class ConvergenceWarning(UserWarning):
  38. """Custom warning to capture convergence problems
  39. .. versionchanged:: 0.18
  40. Moved from sklearn.utils.
  41. """
  42. class DataConversionWarning(UserWarning):
  43. """Warning used to notify implicit data conversions happening in the code.
  44. This warning occurs when some input data needs to be converted or
  45. interpreted in a way that may not match the user's expectations.
  46. For example, this warning may occur when the user
  47. - passes an integer array to a function which expects float input and
  48. will convert the input
  49. - requests a non-copying operation, but a copy is required to meet the
  50. implementation's data-type expectations;
  51. - passes an input whose shape can be interpreted ambiguously.
  52. .. versionchanged:: 0.18
  53. Moved from sklearn.utils.validation.
  54. """
  55. class DataDimensionalityWarning(UserWarning):
  56. """Custom warning to notify potential issues with data dimensionality.
  57. For example, in random projection, this warning is raised when the
  58. number of components, which quantifies the dimensionality of the target
  59. projection space, is higher than the number of features, which quantifies
  60. the dimensionality of the original source space, to imply that the
  61. dimensionality of the problem will not be reduced.
  62. .. versionchanged:: 0.18
  63. Moved from sklearn.utils.
  64. """
  65. class EfficiencyWarning(UserWarning):
  66. """Warning used to notify the user of inefficient computation.
  67. This warning notifies the user that the efficiency may not be optimal due
  68. to some reason which may be included as a part of the warning message.
  69. This may be subclassed into a more specific Warning class.
  70. .. versionadded:: 0.18
  71. """
  72. class FitFailedWarning(RuntimeWarning):
  73. """Warning class used if there is an error while fitting the estimator.
  74. This Warning is used in meta estimators GridSearchCV and RandomizedSearchCV
  75. and the cross-validation helper function cross_val_score to warn when there
  76. is an error while fitting the estimator.
  77. Examples
  78. --------
  79. >>> from sklearn.model_selection import GridSearchCV
  80. >>> from sklearn.svm import LinearSVC
  81. >>> from sklearn.exceptions import FitFailedWarning
  82. >>> import warnings
  83. >>> warnings.simplefilter('always', FitFailedWarning)
  84. >>> gs = GridSearchCV(LinearSVC(), {'C': [-1, -2]}, error_score=0, cv=2)
  85. >>> X, y = [[1, 2], [3, 4], [5, 6], [7, 8]], [0, 0, 1, 1]
  86. >>> with warnings.catch_warnings(record=True) as w:
  87. ... try:
  88. ... gs.fit(X, y) # This will raise a ValueError since C is < 0
  89. ... except ValueError:
  90. ... pass
  91. ... print(repr(w[-1].message))
  92. ... # doctest: +NORMALIZE_WHITESPACE
  93. FitFailedWarning('Estimator fit failed. The score on this train-test
  94. partition for these parameters will be set to 0.000000.
  95. Details: \\nValueError: Penalty term must be positive; got (C=-2)\\n'...)
  96. .. versionchanged:: 0.18
  97. Moved from sklearn.cross_validation.
  98. """
  99. class NonBLASDotWarning(EfficiencyWarning):
  100. """Warning used when the dot operation does not use BLAS.
  101. This warning is used to notify the user that BLAS was not used for dot
  102. operation and hence the efficiency may be affected.
  103. .. versionchanged:: 0.18
  104. Moved from sklearn.utils.validation, extends EfficiencyWarning.
  105. """
  106. class SkipTestWarning(UserWarning):
  107. """Warning class used to notify the user of a test that was skipped.
  108. For example, one of the estimator checks requires a pandas import.
  109. If the pandas package cannot be imported, the test will be skipped rather
  110. than register as a failure.
  111. """
  112. class UndefinedMetricWarning(UserWarning):
  113. """Warning used when the metric is invalid
  114. .. versionchanged:: 0.18
  115. Moved from sklearn.base.
  116. """