파이썬(Python)/머신러닝(Machine Learning)

[머신러닝]데이터 세트(Dataset)란? sklearn iris 예제로 알아보기

끄적끄적아무거나 2023. 3. 12. 09:51
반응형

 

목차

     

     

     

     

     

    Machine Learning 데이터 세트(Dataset)란?

     

    기계학습의 핵심은 데이터를 통해 학습을 하기 때문에 데이터란 기계학습의 근간이라고 할 수 있습니다. 머신러닝을 사용하는 사용자들 간에 데이터의 통일을 위해 암묵적인 합의에 대해 얘기할까 합니다.

     

    데이터 셋은 아래의 2가지 요소로 크게 분류 할 수 있습니다. 

     

    • 행(Row) : Example
    • 열(Column) : Features

     

    아래의 간단한 예제를 통해 쉽게 이해할 수 있습니다.

     

     

    그림1

     

     

    녹색에 해당하는 행 부분이 예제(Example)이고 열로 표현되는 노란색 부분이 특징(Feature)을 의미합니다.

     

    일반적인 데이터 셋이 그림1과 같이 구성됩니다. 그렇지 않은 경우도 가끔 있으니 데이터셋을 사용하실때 항상 데이터를 확인하셔야 합니다. 

     

    그림2

     

    그림으로 표현하면 위와 같습니다. a 예제와 b 특성을 가진 데이터를 사용해서 a개의 예측 벡터를 구하는 것입니다.

     

     

     

     

     

     

     

     

     

    Sklearn 데이터 세트 IRIS 확인하기 실습

     

    Sklearn에서 사용자가 실습을 할 수 있게 데이터 셋을 제공하고 있습니다. 그중에서 가장 일반적으로 많이 사용하는 iris를 불러 오고 어떠한 데이터인지 알아 보겠습니다. 

     

    출처: nciku

     

    iris는 붓꽃이라고 합니다. 저도 처음 알았습니다.....

     

     

    코드는 머신러닝 라이브러리가 미리 설치가 되어있고 이미지를 시각화하기 편한 주피터(Jupyter)를 사용하겠습니다.

     

     

    예제 코드>>

    from sklearn import datasets
    iris_data = datasets.load_iris()
    x = iris_data.data
    y = iris_data.target
    iris_data

    x에는 데이터셋 값을 가져 옵니다. 

    y는 해당 목표 결과(Target)입니다.target는 아래 결과항목에서 볼 수 있는데 실제 결과로  'setosa', 'versicolor', 'virginica' 중 3가지로 분류된 값입니다.

    ML의 목적은 x라는 데이터를 이용해서 y라는 결과와 얼마나 유사하게 예측하는지를 진행하는 것입니다. 이번 실습에서는 데이터셋을 살펴보는 것만 진행하고 별도의 ML을 진행하지는 않습니다.

     

    앞서 그림2에서 dataset이 x이고 target이 y입니다.

     

     

    결과>>

    {'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]]),
     '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]),
     'frame': None,
     'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'),
     '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 ...',
     'feature_names': ['sepal length (cm)',
      'sepal width (cm)',
      'petal length (cm)',
      'petal width (cm)'],
     'filename': 'iris.csv',
     'data_module': 'sklearn.datasets.data'}

    결과 값이 많아서 일부는 제가 .... 으로 처리 했습니다.

     

     

    데이터는 Dictionary 형태로 되어 있습니다. 데이터 셋에 feature 부분도 위와 같이 따로 분류 되어 있습니다.

     

     

     

     

     

    예제 코드>>

    iris_data.feature_names

    앞서 데이터셋의 feature 이름을 위와 같이 가져 올 수 있습니다.

     

     

    결과>>

    ['sepal length (cm)',
     'sepal width (cm)',
     'petal length (cm)',
     'petal width (cm)']

    의미는 꽃입과 꽃봉우리의 너비와 길이 정보입니다. 즉, Dataset의 feature 부분입니다. 

     

     

     

    예제 코드>>

    x.shape
    y.shape

    앞서 그림2에서 설명했듯이 데이터 셋의 행과 열의 크기를 확인하는 것이 중요합니다.

     

     

    결과>>

    (150, 4)
    (150, )

     

     

    반응형