__init__.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. The :mod:`sklearn.datasets` module includes utilities to load datasets,
  3. including methods to load and fetch popular reference datasets. It also
  4. features some artificial data generators.
  5. """
  6. import textwrap
  7. from ._base import (
  8. clear_data_home,
  9. get_data_home,
  10. load_breast_cancer,
  11. load_diabetes,
  12. load_digits,
  13. load_files,
  14. load_iris,
  15. load_linnerud,
  16. load_sample_image,
  17. load_sample_images,
  18. load_wine,
  19. )
  20. from ._california_housing import fetch_california_housing
  21. from ._covtype import fetch_covtype
  22. from ._kddcup99 import fetch_kddcup99
  23. from ._lfw import fetch_lfw_pairs, fetch_lfw_people
  24. from ._olivetti_faces import fetch_olivetti_faces
  25. from ._openml import fetch_openml
  26. from ._rcv1 import fetch_rcv1
  27. from ._samples_generator import (
  28. make_biclusters,
  29. make_blobs,
  30. make_checkerboard,
  31. make_circles,
  32. make_classification,
  33. make_friedman1,
  34. make_friedman2,
  35. make_friedman3,
  36. make_gaussian_quantiles,
  37. make_hastie_10_2,
  38. make_low_rank_matrix,
  39. make_moons,
  40. make_multilabel_classification,
  41. make_regression,
  42. make_s_curve,
  43. make_sparse_coded_signal,
  44. make_sparse_spd_matrix,
  45. make_sparse_uncorrelated,
  46. make_spd_matrix,
  47. make_swiss_roll,
  48. )
  49. from ._species_distributions import fetch_species_distributions
  50. from ._svmlight_format_io import (
  51. dump_svmlight_file,
  52. load_svmlight_file,
  53. load_svmlight_files,
  54. )
  55. from ._twenty_newsgroups import fetch_20newsgroups, fetch_20newsgroups_vectorized
  56. __all__ = [
  57. "clear_data_home",
  58. "dump_svmlight_file",
  59. "fetch_20newsgroups",
  60. "fetch_20newsgroups_vectorized",
  61. "fetch_lfw_pairs",
  62. "fetch_lfw_people",
  63. "fetch_olivetti_faces",
  64. "fetch_species_distributions",
  65. "fetch_california_housing",
  66. "fetch_covtype",
  67. "fetch_rcv1",
  68. "fetch_kddcup99",
  69. "fetch_openml",
  70. "get_data_home",
  71. "load_diabetes",
  72. "load_digits",
  73. "load_files",
  74. "load_iris",
  75. "load_breast_cancer",
  76. "load_linnerud",
  77. "load_sample_image",
  78. "load_sample_images",
  79. "load_svmlight_file",
  80. "load_svmlight_files",
  81. "load_wine",
  82. "make_biclusters",
  83. "make_blobs",
  84. "make_circles",
  85. "make_classification",
  86. "make_checkerboard",
  87. "make_friedman1",
  88. "make_friedman2",
  89. "make_friedman3",
  90. "make_gaussian_quantiles",
  91. "make_hastie_10_2",
  92. "make_low_rank_matrix",
  93. "make_moons",
  94. "make_multilabel_classification",
  95. "make_regression",
  96. "make_s_curve",
  97. "make_sparse_coded_signal",
  98. "make_sparse_spd_matrix",
  99. "make_sparse_uncorrelated",
  100. "make_spd_matrix",
  101. "make_swiss_roll",
  102. ]
  103. def __getattr__(name):
  104. if name == "load_boston":
  105. msg = textwrap.dedent("""
  106. `load_boston` has been removed from scikit-learn since version 1.2.
  107. The Boston housing prices dataset has an ethical problem: as
  108. investigated in [1], the authors of this dataset engineered a
  109. non-invertible variable "B" assuming that racial self-segregation had a
  110. positive impact on house prices [2]. Furthermore the goal of the
  111. research that led to the creation of this dataset was to study the
  112. impact of air quality but it did not give adequate demonstration of the
  113. validity of this assumption.
  114. The scikit-learn maintainers therefore strongly discourage the use of
  115. this dataset unless the purpose of the code is to study and educate
  116. about ethical issues in data science and machine learning.
  117. In this special case, you can fetch the dataset from the original
  118. source::
  119. import pandas as pd
  120. import numpy as np
  121. data_url = "http://lib.stat.cmu.edu/datasets/boston"
  122. raw_df = pd.read_csv(data_url, sep="\\s+", skiprows=22, header=None)
  123. data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
  124. target = raw_df.values[1::2, 2]
  125. Alternative datasets include the California housing dataset and the
  126. Ames housing dataset. You can load the datasets as follows::
  127. from sklearn.datasets import fetch_california_housing
  128. housing = fetch_california_housing()
  129. for the California housing dataset and::
  130. from sklearn.datasets import fetch_openml
  131. housing = fetch_openml(name="house_prices", as_frame=True)
  132. for the Ames housing dataset.
  133. [1] M Carlisle.
  134. "Racist data destruction?"
  135. <https://medium.com/@docintangible/racist-data-destruction-113e3eff54a8>
  136. [2] Harrison Jr, David, and Daniel L. Rubinfeld.
  137. "Hedonic housing prices and the demand for clean air."
  138. Journal of environmental economics and management 5.1 (1978): 81-102.
  139. <https://www.researchgate.net/publication/4974606_Hedonic_housing_prices_and_the_demand_for_clean_air>
  140. """)
  141. raise ImportError(msg)
  142. try:
  143. return globals()[name]
  144. except KeyError:
  145. # This is turned into the appropriate ImportError
  146. raise AttributeError