[ 머신러닝 ] 아이리스 데이터셋

2023. 6. 25. 23:37머신러닝

728x90

🌀 아이리스 데이터셋

         @ 데이터셋: 특정한 작업을 위해 데이터를 관련성 있게 모아놓은 것

         @ [사이킷런 데이터셋 페이지 ] 

(https://scikit-learn.org/stable/modules/classes.html?highlight=datasets#module-sklearn.datasets )

 

API Reference

This is the class and function reference of scikit-learn. Please refer to the full user guide for further details, as the class and function raw specifications may not be enough to give full guidel...

scikit-learn.org

 

from sklearn.datasets import load_iris
iris = load_iris() # 아이리스 붓꽃 데이터
  • sepal length: 꽃받침의 길이
  • sepal width: 꽃받침의 넓이
  • petal length: 꽃잎 길이
  • petal width: 꽃잎 넓이
print(iris['DESCR']) # DESCR이란 데이터 셋의 설명을 하는것
-------------------------------------------------------------------
# 결과
.. _iris_dataset:

Iris plants dataset
--------------------

**Data Set Characteristics:**

    :Number of Instances: 150 (50 in each of three classes)
    :Number of Attributes: 4 numeric, predictive attributes and the class
    :Attribute Information:
        - sepal length in cm
        - sepal width in cm
        - petal length in cm
        - petal width in cm
        - class:
                - Iris-Setosa
                - Iris-Versicolour
                - Iris-Virginica
                
    :Summary Statistics:

    ============== ==== ==== ======= ===== ====================
                    Min  Max   Mean    SD   Class Correlation
    ============== ==== ==== ======= ===== ====================
    sepal length:   4.3  7.9   5.84   0.83    0.7826
    sepal width:    2.0  4.4   3.05   0.43   -0.4194
    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)
    petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)
    ============== ==== ==== ======= ===== ====================

    :Missing Attribute Values: None
    :Class Distribution: 33.3% for each of 3 classes.
    :Creator: R.A. Fisher
    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
    :Date: July, 1988

The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fisher's paper. Note that it's the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.

This is perhaps the best known database to be found in the
pattern recognition literature.  Fisher's paper is a classic in the field and
is referenced frequently to this day.  (See Duda & Hart, for example.)  The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant.  One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.

.. topic:: References

   - Fisher, R.A. "The use of multiple measurements in taxonomic problems"
     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
     Mathematical Statistics" (John Wiley, NY, 1950).
   - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.
     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.
   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
     Structure and Classification Rule for Recognition in Partially Exposed
     Environments".  IEEE Transactions on Pattern Analysis and Machine
     Intelligence, Vol. PAMI-2, No. 1, 67-71.
   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions
     on Information Theory, May 1972, 431-433.
   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II
     conceptual clustering system finds 3 classes in the data.
   - Many, many more ...
data = iris['data'] # 샘플데이터, Numpy 배열로 이루어져 있음
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],
       [4.6, 3.1, 1.5, 0.2],
       [5. , 3.6, 1.4, 0.2],
       [5.4, 3.9, 1.7, 0.4],
       [4.6, 3.4, 1.4, 0.3],
       [5. , 3.4, 1.5, 0.2],
       [4.4, 2.9, 1.4, 0.2],
       [4.9, 3.1, 1.5, 0.1],
       [5.4, 3.7, 1.5, 0.2],
       [4.8, 3.4, 1.6, 0.2],
       [4.8, 3. , 1.4, 0.1],
       [4.3, 3. , 1.1, 0.1],
       [5.8, 4. , 1.2, 0.2],
       [5.7, 4.4, 1.5, 0.4],
       [5.4, 3.9, 1.3, 0.4],
       [5.1, 3.5, 1.4, 0.3],
       [5.7, 3.8, 1.7, 0.3],
       [5.1, 3.8, 1.5, 0.3],
       [5.4, 3.4, 1.7, 0.2],
       [5.1, 3.7, 1.5, 0.4],
       [4.6, 3.6, 1. , 0.2],
       [5.1, 3.3, 1.7, 0.5],
       [4.8, 3.4, 1.9, 0.2],
       [5. , 3. , 1.6, 0.2],
       [5. , 3.4, 1.6, 0.4],
       [5.2, 3.5, 1.5, 0.2],
       [5.2, 3.4, 1.4, 0.2],
       [4.7, 3.2, 1.6, 0.2],
       [4.8, 3.1, 1.6, 0.2],
       [5.4, 3.4, 1.5, 0.4],
       [5.2, 4.1, 1.5, 0.1],
       [5.5, 4.2, 1.4, 0.2],
       [4.9, 3.1, 1.5, 0.2],
       [5. , 3.2, 1.2, 0.2],
       [5.5, 3.5, 1.3, 0.2],
       [4.9, 3.6, 1.4, 0.1],
       [4.4, 3. , 1.3, 0.2],
       [5.1, 3.4, 1.5, 0.2],
       [5. , 3.5, 1.3, 0.3],
       [4.5, 2.3, 1.3, 0.3],
       [4.4, 3.2, 1.3, 0.2],
       [5. , 3.5, 1.6, 0.6],
       [5.1, 3.8, 1.9, 0.4],
       [4.8, 3. , 1.4, 0.3],
       [5.1, 3.8, 1.6, 0.2],
       [4.6, 3.2, 1.4, 0.2],
       [5.3, 3.7, 1.5, 0.2],
       [5. , 3.3, 1.4, 0.2],
       [7. , 3.2, 4.7, 1.4],
       [6.4, 3.2, 4.5, 1.5],
       [6.9, 3.1, 4.9, 1.5],
       [5.5, 2.3, 4. , 1.3],
       [6.5, 2.8, 4.6, 1.5],
       [5.7, 2.8, 4.5, 1.3],
       [6.3, 3.3, 4.7, 1.6],
       [4.9, 2.4, 3.3, 1. ],
       [6.6, 2.9, 4.6, 1.3],
       [5.2, 2.7, 3.9, 1.4],
       [5. , 2. , 3.5, 1. ],
       [5.9, 3. , 4.2, 1.5],
       [6. , 2.2, 4. , 1. ],
       [6.1, 2.9, 4.7, 1.4],
       [5.6, 2.9, 3.6, 1.3],
       [6.7, 3.1, 4.4, 1.4],
       [5.6, 3. , 4.5, 1.5],
       [5.8, 2.7, 4.1, 1. ],
       [6.2, 2.2, 4.5, 1.5],
       [5.6, 2.5, 3.9, 1.1],
       [5.9, 3.2, 4.8, 1.8],
       [6.1, 2.8, 4. , 1.3],
       [6.3, 2.5, 4.9, 1.5],
       [6.1, 2.8, 4.7, 1.2],
       [6.4, 2.9, 4.3, 1.3],
       [6.6, 3. , 4.4, 1.4],
       [6.8, 2.8, 4.8, 1.4],
       [6.7, 3. , 5. , 1.7],
       [6. , 2.9, 4.5, 1.5],
       [5.7, 2.6, 3.5, 1. ],
       [5.5, 2.4, 3.8, 1.1],
       [5.5, 2.4, 3.7, 1. ],
       [5.8, 2.7, 3.9, 1.2],
       [6. , 2.7, 5.1, 1.6],
       [5.4, 3. , 4.5, 1.5],
       [6. , 3.4, 4.5, 1.6],
       [6.7, 3.1, 4.7, 1.5],
       [6.3, 2.3, 4.4, 1.3],
       [5.6, 3. , 4.1, 1.3],
       [5.5, 2.5, 4. , 1.3],
       [5.5, 2.6, 4.4, 1.2],
       [6.1, 3. , 4.6, 1.4],
       [5.8, 2.6, 4. , 1.2],
       [5. , 2.3, 3.3, 1. ],
       [5.6, 2.7, 4.2, 1.3],
       [5.7, 3. , 4.2, 1.2],
       [5.7, 2.9, 4.2, 1.3],
       [6.2, 2.9, 4.3, 1.3],
       [5.1, 2.5, 3. , 1.1],
       [5.7, 2.8, 4.1, 1.3],
       [6.3, 3.3, 6. , 2.5],
       [5.8, 2.7, 5.1, 1.9],
       [7.1, 3. , 5.9, 2.1],
       [6.3, 2.9, 5.6, 1.8],
       [6.5, 3. , 5.8, 2.2],
       [7.6, 3. , 6.6, 2.1],
       [4.9, 2.5, 4.5, 1.7],
       [7.3, 2.9, 6.3, 1.8],
       [6.7, 2.5, 5.8, 1.8],
       [7.2, 3.6, 6.1, 2.5],
       [6.5, 3.2, 5.1, 2. ],
       [6.4, 2.7, 5.3, 1.9],
       [6.8, 3. , 5.5, 2.1],
       [5.7, 2.5, 5. , 2. ],
       [5.8, 2.8, 5.1, 2.4],
       [6.4, 3.2, 5.3, 2.3],
       [6.5, 3. , 5.5, 1.8],
       [7.7, 3.8, 6.7, 2.2],
       [7.7, 2.6, 6.9, 2.3],
       [6. , 2.2, 5. , 1.5],
       [6.9, 3.2, 5.7, 2.3],
       [5.6, 2.8, 4.9, 2. ],
       [7.7, 2.8, 6.7, 2. ],
       [6.3, 2.7, 4.9, 1.8],
       [6.7, 3.3, 5.7, 2.1],
       [7.2, 3.2, 6. , 1.8],
       [6.2, 2.8, 4.8, 1.8],
       [6.1, 3. , 4.9, 1.8],
       [6.4, 2.8, 5.6, 2.1],
       [7.2, 3. , 5.8, 1.6],
       [7.4, 2.8, 6.1, 1.9],
       [7.9, 3.8, 6.4, 2. ],
       [6.4, 2.8, 5.6, 2.2],
       [6.3, 2.8, 5.1, 1.5],
       [6.1, 2.6, 5.6, 1.4],
       [7.7, 3. , 6.1, 2.3],
       [6.3, 3.4, 5.6, 2.4],
       [6.4, 3.1, 5.5, 1.8],
       [6. , 3. , 4.8, 1.8],
       [6.9, 3.1, 5.4, 2.1],
       [6.7, 3.1, 5.6, 2.4],
       [6.9, 3.1, 5.1, 2.3],
       [5.8, 2.7, 5.1, 1.9],
       [6.8, 3.2, 5.9, 2.3],
       [6.7, 3.3, 5.7, 2.5],
       [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 = iris['target'] # Label데이터, Numpy 배열로 이루어져 있음
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])
feature_names = iris['feature_names'] # Feauture 데이터의 이름
feature_names
--------------------------------------------------------------
# 결과
['sepal length (cm)',
 'sepal width (cm)',
 'petal length (cm)',
 'petal width (cm)']
import pandas as pd
df_iris = pd.DataFrame(data,columns = feature_names)
df_iris.head()

df_iris.head() 결과

df_iris['target'] = target
df_iris

df_iris 결과

#scikit-learn(sklearn)라이브러리에서 제공하는 train_test_split 함수를 가져오는 구문
# train_test_split 함수는 기계 학습 모델을 훈련(학습) 하기 위해 사용하는 데이터를 훈련세트와 테스트 세트로 나누는데 
# 사용됨
# 일반적으로 데이터 세트를 훈련세트와 테스트세트로 분리하여 모델의 성능을 평가하고 검증하기 위해 사용됨
from sklearn.model_selection import train_test_split

💥훈련세트(Training Set) : 모델을 훈련시키는 데 사용되는 데이터의 부분집합

             @ 모델은 훈련세트를 사용하여 학습하고 입력 데이터와 해당 데이터의 출력(  라벨 또는 타깃 ) 간의 관계를 학습

             @ 모델은 훈련 세트에 적합한 패턴과 특징을 파악하고, 주어진 입력에 대한 올바른 출력을 예측하도록 조정됨

💥테스트세트(Test Set) : 모델이 훈련된 후에 모델의 성능을 평가하는 데 사용되는 데이터의 부분집합

            @ 훈련 과정에서 사용되지 않은 독립적인 데이터로 구성됨

            @ 모델이 테스트 세트에 대해 예측을 수행한 후, 실제 출력(라벨 또는 타깃)과 비교하여 모델의 정확도나 성능을 평

                가할 수 있음

           @ 모델의 일반화(generalization)능력을 측정하고 새로운 입력 데이터에 대한 예측 성능을 추정할 수 있음

→ 훈련세트와 테스트세트를 적절히 나누는 것은 모델의 신뢰도와 일반화 능력을 평가하는데 중요한 역할을 한다.

# train_test_split(독립변수,종속변수,테스트사이즈 = 0.25, 시드값)
# 데이터프레임 df_iris를 훈련 세트와 테스트 세트로 분할하는 코드
# df_iris.drop('target',1) = 독립변수로 구성된 데이터를 나타냄
# df_iris['target'] = 종속 변수(타깃)으로 구성된 데이터를 나타냄
# train_test_split = 독립변수와 종속변수를 주어진 비율에 따라 훈련 세트와 테스트 세트로 분할
# test_size = 0.2 # 테스트 세트의 크기를 전체 데이터의 20%로 지정하기 위해 사용
# random_state= 10 # 분할 과정에서 데이터를 섞기 위한 난수 발생기의 시드를 설정
# x_train = 훈련세트의 독립변수 x_test = 테스트세트의 독립변수 y_train = 훈련세트의 종속변수 y_test = 테스트세트의 종속변수를 나타냄 
X_train,X_test,y_train,y_test = train_test_split(df_iris.drop('target',1),df_iris['target'],test_size= 0.2, random_state=10)
--------------------------------------------------------------------------------------------------------------------------------
# 결과
<ipython-input-14-983b661006cd>:2: FutureWarning: In a future version of pandas all arguments of DataFrame.drop except for the argument 'labels' will be keyword-only.
  X_train,X_test,y_train,y_test = train_test_split(df_iris.drop('target',1),df_iris['target'],test_size= 0.2, random_state=10)
# shape는 numpy배열 또는 데이터프레임의 차원을 나타내는 속성
X_train.shape, X_test.shape
-------------------------------
# 결과
((120, 4), (30, 4))
# ((120,4),(30,4)) 라면 120개의 행과 4개의 열과 / 30개의 행과 4개의 열을 나타냄
y_train.shape, y_test.shape
---------------------------------
# 결과
# 이유 : 120개의 요소(데이터 포인트)가 있고 30개의 요소(데이터 포인트)가 있다는걸 의미
((120,), (30,))
X_train

X_train 결과

# svm = support vector Machine,SVM) scikit-learn(sklearn)라이브러리에서 제공하는 서포트 백터 머신
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
svc=SVC()
svc.fit(X_train, y_train )

결과

# svc.predict()메서드는 주어진 데이터에 대한 예측값을 반환함
y_pred = svc.predict(X_test)
print('정답률:',accuracy_score(y_test,y_pred))
----------------------------------------------
# 결과
정답률: 0.9666666666666667

728x90
반응형

'머신러닝' 카테고리의 다른 글

[ 머신러닝 ] 의사결정나무  (0) 2023.09.06
[ 머신러닝 ] 선형회귀  (0) 2023.09.06
[머신러닝] Titanic Dataset  (2) 2023.08.30
[ 머신러닝 ] 사이킷런 & Linear SVC  (0) 2023.06.25
[ 머신러닝 ] 머신러닝이란?  (0) 2023.06.25