4 분 소요

sklearn.datasets


안녕하세요. 데이터 사이언티스트를 위한 정보를 공유하고 있습니다.

M1 Macbook Air를 사용하고 있으며, 블로그의 모든 글은 Mac을 기준으로 작성된 점 참고해주세요.


사이킷런에 내장된 샘플 데이터 세트

사이킷런에는 연습하기 좋은 수많은 샘플 데이터 세트가 내장 되어 있습니다.

데이터는 사이킷런의 datasets라는 모듈에서 여러 API를 호출하여 가져올 수 있습니다.


데이터 세트 종류

API 명 설명
datasets.load_breast_cancer() 위스콘신 유방암 피처를 가진 데이터 세트(분류)
datasets.load_iris() 붓꽃에 대한 피처를 가진 데이터 세트(분류)
datasets.load_digits() 0~9 숫자의 이미지 픽셀 데이터 세트(분류)
datasets.load_wine() 와인 등급에 대한 피처를 가진 데이터 세트(분류)
datasets.load_boston() 미국 보스턴 지역의 집값 데이터 세트(회귀)
datasets.load_linnerud() 신체 조건 및 운동 능력 데이터 세트(회귀)
datasets.load_diabetes() 당뇨 데이터 세트(회귀)

예시로 사이킷런의 붓꽃 데이터 세트를 확인해 보겠습니다.

In:

from sklearn.datasets import load_iris
# sklearn의 datasets 모듈로 부터 load_iris를 가져오기

iris_data = load_iris()
# 붓꽃 데이터를 iris_data라는 이름으로 저장
iris_data
# 붓꽃 데이터 확인

Out:

{'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n    :Number of Instances: 150 (50 in each of three classes)\n    :Number of Attributes: 4 numeric, predictive attributes and the class\n    :Attribute Information:\n        - sepal length in cm\n        - sepal width in cm\n        - petal length in cm\n        - petal width in cm\n        - class:\n                - Iris-Setosa\n                - Iris-Versicolour\n                - Iris-Virginica\n                \n    :Summary Statistics:\n\n    ============== ==== ==== ======= ===== ====================\n                    Min  Max   Mean    SD   Class Correlation\n    ============== ==== ==== ======= ===== ====================\n    sepal length:   4.3  7.9   5.84   0.83    0.7826\n    sepal width:    2.0  4.4   3.05   0.43   -0.4194\n    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)\n    petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)\n    ============== ==== ==== ======= ===== ====================\n\n    :Missing Attribute Values: None\n    :Class Distribution: 33.3% for each of 3 classes.\n    :Creator: R.A. Fisher\n    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n    :Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature.  Fisher\'s paper is a classic in the field and\nis referenced frequently to this day.  (See Duda & Hart, for example.)  The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant.  One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n.. topic:: References\n\n   - Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n     Mathematical Statistics" (John Wiley, NY, 1950).\n   - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.\n   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n     Structure and Classification Rule for Recognition in Partially Exposed\n     Environments".  IEEE Transactions on Pattern Analysis and Machine\n     Intelligence, Vol. PAMI-2, No. 1, 67-71.\n   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions\n     on Information Theory, May 1972, 431-433.\n   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II\n     conceptual clustering system finds 3 classes in the data.\n   - Many, many more ...',
 'data': array([[5.1, 3.5, 1.4, 0.2],
        [4.9, 3. , 1.4, 0.2],
        [4.7, 3.2, 1.3, 0.2],
          ~
        [6.7, 3. , 5.2, 2.3],
        [6.3, 2.5, 5. , 1.9],
        [6.5, 3. , 5.2, 2. ],
        [6.2, 3.4, 5.4, 2.3],
        [5.9, 3. , 5.1, 1.8]]),
 'data_module': 'sklearn.datasets.data',
 'feature_names': ['sepal length (cm)',
  'sepal width (cm)',
  'petal length (cm)',
  'petal width (cm)'],
 'filename': 'iris.csv',
 'frame': None,
 'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
 'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10')}

In:

type(iris_data)
# iris_data의 타입을 확인

Out:

sklearn.utils.Bunch

불러온 데이터의 타입을 확인하였을 때 sklearn.utils.Bunch라는 결과를 확인할 수 있는데,

이는 파이썬에서 딕셔너리 자료형과 유사합니다.

‘DESCR’, ‘data’, ‘feature_names’ 등의 keys 와 각 key 의 values로 구성되어 있습니다.


주요 keys가 나타내는 의미를 알아보겠습니다.

‘DESCR’: 데이터 세트에 대한 설명을 문자열로 나타낸 것입니다.

‘data’: 데이터의 피처를 나타내며 넘파이 타입으로 구성되어 있습니다.

‘feature_names’: 피처의 이름을 나타냅니다.

‘target’: 분류에서는 분류 레이블 값을 나타내고 회귀에서는 결과값의 수치들을 나타내며 넘파이 타입으로 구성되어 있습니다.

‘target_name’: 각 레이블의 이름을 나타냅니다.


읽어주셔서 감사합니다.

정보 공유의 목적으로 만들어진 블로그입니다.

미흡한 점은 언제든 댓글로 지적해주시면 감사하겠습니다.


업데이트:

댓글남기기