แ„‚ ๐Ÿ˜„ [8 ์ผ์ฐจ] : EXPLORATION 02. Iris ํ’ˆ์ข… ๋ถ„๋ฅ˜

๋ฐฑ๊ฑดยท2022๋…„ 1์›” 21์ผ
0

Iris์˜ ์„ธ ๊ฐ€์ง€ ํ’ˆ์ข…, ๋ถ„๋ฅ˜ํ•ด๋ณผ ์ˆ˜ ์žˆ๊ฒ ์–ด์š”?

๋“ค์–ด๊ฐ€๋ฉฐ

ํ•™์Šต ๋ชฉํ‘œ


  • scikit-learn์— ๋‚ด์žฅ๋œ ์˜ˆ์ œ ๋ฐ์ดํ„ฐ์…‹์˜ ์ข…๋ฅ˜๋ฅผ ์•Œ๊ณ  ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • scikit-learn์— ๋‚ด์žฅ๋œ ๋ถ„๋ฅ˜ ๋ชจ๋ธ๋“ค์„ ํ•™์Šต์‹œํ‚ค๊ณ  ์˜ˆ์ธกํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ์ง€ํ‘œ์˜ ์ข…๋ฅ˜์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ณ , ํ™œ์šฉ ๋ฐ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • Decision Tree, XGBoost, RandomForest, ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจ๋ธ์„ ํ™œ์šฉํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•™์Šต ๋ฐ ์˜ˆ์ธกํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ์…‹์„ ์‚ฌ์šฉํ•ด์„œ ์Šค์Šค๋กœ ๋ถ„๋ฅ˜ ๊ธฐ์ดˆ ์‹ค์Šต์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•™์Šต ์ „์ œ


  • scikit-learn์„ ํ™œ์šฉํ•ด์„œ ๋จธ์‹ ๋Ÿฌ๋‹์„ ์‹œ๋„ํ•ด๋ณธ ์ ์ด ์—†๋‹ค.
  • scikit-learn์— ๋‚ด์žฅ๋œ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ํ™œ์šฉํ•ด๋ณธ ์ ์ด ์—†๋‹ค.
  • ์ง€๋„ํ•™์Šต์˜ ๋ถ„๋ฅ˜ ์‹ค์Šต์„ ํ•ด ๋ณธ ์ ์ด ์—†๋‹ค.
  • ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šต์‹œ์ผœ๋ณด๊ณ , ๊ทธ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•ด๋ณธ ์ ์ด ์—†๋‹ค.

Iris์˜ ์„ธ ๊ฐ€์ง€ ํ’ˆ์ข…, ๋ถ„๋ฅ˜ํ•ด ๋ณผ๊นŒ์š”?

๋ถ“๊ฝƒ ๋ถ„๋ฅ˜ ๋ฌธ์ œ

pip install scikit-learn
pip install matplotlib

๋ถ„๋ฅ˜ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ


  • ์‚ฌ์ดํ‚ท๋Ÿฐ(scikit-learn)์— ๋‚ด์žฅ๋œ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ
  • ๋ถ“๊ฝƒ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚ด์žฅ๋˜์–ด ์žˆ์Œ.(๊ทธ๋ž˜์„œ ์“ด๋‹ค๊ณ ..ใ…‹)

์‚ฌ์ดํ‚ท๋Ÿฐ(scikit-learn)


  • Toy datasets : 7๊ฐ€์ง€ , ๊ฐ„๋‹จํ•˜๊ณ  ์ž‘์€ ๋ฐ์ดํ„ฐ์…‹
    • boston
    • iris
    • diabetes
    • digits
    • linnerrud
    • wine
    • breast
    • cancer

  • Real world datasets : 7๊ฐ€์ง€ , ๋ณต์žกํ•˜๊ณ  ํ˜„์‹ค ์„ธ๊ณ„ ๋ฐ˜์˜ํ•œ ๋ฐ์ดํ„ฐ์…‹
    • olivetti faces
    • 20 newsgroups
    • labeled faces
    • forest covertype
    • RCV1
    • Kddcup 99
    • California housin

petal์€ ๊ฝƒ์žŽ, sepal์€ ๊ฝƒ๋ฐ›์นจ


์‹œ์ž‘์ „ ๋ฐ์ดํ„ฐ๋ฅผ ์ •ํ™•ํžˆ ์ž์„ธ ํ™•์ธ

  • Total data : 150(Instance)
  • Attributes : 4
    • sepal
      • length
      • width
    • petal
      • length
      • width
  • Class
    • Iris
      • Setosa
      • Versicolour
      • virginica

๋ฐ์ดํ„ฐ ์ค€๋น„, ๊ทธ๋ฆฌ๊ณ  ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ธฐ๋Š” ๊ธฐ๋ณธ!

๋ฐ์ดํ„ฐ ํ™•์ธ.

#  sklearn ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ datasets ํŒจํ‚ค์ง€ ์•ˆ load_iris๋ฅผ import 
#  iris ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋”ฉ

from sklearn.datasets import load_iris

iris = load_iris()

print(dir(iris)) # iris ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‚˜
# dir()๋Š” ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๋‚˜์—ดํ•จ
['DESCR', 'data', 'data_module', 'feature_names', 'filename', 'frame', 'target', 'target_names']

๋‹ด๊ธด ์ •๋ณด ํ™•์ธ

#  iris์—๋Š” ์–ด๋–ค ์ •๋ณด๋“ค์ด ๋‹ด๊ฒผ์„์ง€, keys() ๋ผ๋Š” ๋ฉ”์„œ๋“œ๋กœ ํ™•์ธ
iris.keys()
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename', 'data_module'])

7๊ฐ€์ง€ : 'data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename'

->>data_module ์€???

์ค‘์š” ๋ฐ์ดํ„ฐ ๋ณ€์ˆ˜์— ์ €์žฅ

#  ์ค‘์š”ํ•œ ๋ฐ์ดํ„ฐ๋Š” iris_data ๋ณ€์ˆ˜์— ์ €์žฅ

iris_data = iris.data

๋ฐ์ดํ„ฐ ํฌ๊ธฐ ํ™•์ธ.

print(iris_data.shape) 
#shape๋Š” ๋ฐฐ์—ด์˜ ํ˜•์ƒ์ •๋ณด๋ฅผ ์ถœ๋ ฅ
(150, 4)
  • 150๊ฐœ ๋ฐ์ดํ„ฐ
  • 4์ข…๋ฅ˜์˜ ์ •๋ณด

์ƒ˜ํ”Œ๋ฐ์ดํ„ฐ ํ™•์ธ.

iris_data[0] # 0๋ฒˆ index์— ์ ‘๊ทผๆŽฅ่ฟ‘
array([5.1, 3.5, 1.4, 0.2])

ํ•ด๊ฒฐํ•  ๋ฌธ์ œ ํ™•์ธ. -> ๋ผ๋ฒจ or ํƒ€๊ฒŸ

๋ถ“๊ฝƒ์˜ ๊ฝƒ์žŽ ๊ธธ์ด์™€ ํญ / ๊ฝƒ๋ฐ›์นจ ๊ธธ์ด์™€ ํญ ์ด์šฉ

๋ถ“๊ฝƒ์˜ ์ข…๋ฅ˜๊ฐ€ setosa, versicolor, virginica ์„ธ ๊ฐ€์ง€ ์ค‘ ๋ฌด์—‡์ธ์ง€.

ํƒ€๊ฒŸ ์ •๋ณด ํ™•์ธ

iris_label = iris.target  # iris_label ๋ณ€์ˆ˜์— ํƒ€๊ฒŸ์ •๋ณด ์ €์žฅ
print(iris_label.shape)   # ๋ณ€์ˆ˜ ํฌ๊ธฐ ์ถœ๋ ฅ
iris_label                # 
(150,)





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์—์„œ ํ™•์ธ
iris.target_names
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
  • 0 ์ด๋ผ๋ฉด setosa,
  • 1 ์ด๋ผ๋ฉด versicolor,
  • 2 ๋ผ๋ฉด virginica
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 ...
# feature_names์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด 4๊ฐœ์˜ ๊ฐ feature์— ๋Œ€ํ•œ ์„ค๋ช…
iris.feature_names
['sepal length (cm)',
 'sepal width (cm)',
 'petal length (cm)',
 'petal width (cm)']
# filename์—๋Š” ๋ฐ์ดํ„ฐ์…‹์˜ ์ „์ฒด ์ด๋ฆ„
iris.filename
'iris.csv'

csv ํŒŒ์ผ์ด๊ตฐ...

์ฒซ ๋ฒˆ์งธ ๋จธ์‹ ๋Ÿฌ๋‹ ์‹ค์Šต, ๊ฐ„๋‹จํ•˜๊ณ ๋„ ๋น ๋ฅด๊ฒŒ!

๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋ฌธ์ œ์ง€์™€ ์ •๋‹ต์ง€ ์ค€๋น„

pandas = pd = ํŒ๋‹ค์Šค

  • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ํŒŒ์ด์ฌ์—์„œ ํ‘œ ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ง„ 2์ฐจ์› ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ์— ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” ๋„๊ตฌ
  • ํ‘œ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ๋ถ„์„
  • ๋Œ€ํ˜• ๋ฐ์ดํ„ฐ์˜ ์—ฌ๋Ÿฌ ํ†ต๊ณ„๋Ÿ‰์„ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์— ์ตœ์ ํ™”
import pandas as pd

print(pd.__version__)
1.3.5
# ๋ถ“๊ฝƒ ๋ฐ์ดํ„ฐ์…‹์„ pandas๊ฐ€ ์ œ๊ณตํ•˜๋Š” DataFrame ์ด๋ผ๋Š” ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜
iris_df = pd.DataFrame(data=iris_data, columns=iris.feature_names)
# iris_df

# DataFrame ์„ ๋งŒ๋“ค๋ฉด์„œ data์—๋Š” iris_data๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , ๊ฐ ์ปฌ๋Ÿผ์—๋Š” feature_names๋กœ ์ด๋ฆ„์„ ๋ถ™์—ฌ์คŒ
# label ์ปฌ๋Ÿผ ์ถ”๊ฐ€
iris_df["label"] = iris.target
iris_df
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) label
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0
... ... ... ... ... ...
145 6.7 3.0 5.2 2.3 2
146 6.3 2.5 5.0 1.9 2
147 6.5 3.0 5.2 2.0 2
148 6.2 3.4 5.4 2.3 2
149 5.9 3.0 5.1 1.8 2

150 rows ร— 5 columns

๋ฌธ์ œ์ง€์™€ ์ •๋‹ต์ง€ ์ค€๋น„

  • 4๊ฐ€์ง€์˜ feature ๋ฐ์ดํ„ฐ๋“ค์€ ๋ฐ”๋กœ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์ด ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ง€

    -> [5.1, 3.5, 1.4, 0.2]๋ผ๋Š” ๋ฌธ์ œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค๋ฉด ๋ชจ๋ธ์€ 0, ์ฆ‰ setosa๋ผ๋Š” ๋‹ต์„ ๋งžํ˜€์•ผ ํ•˜๋Š” ๊ฒƒ

  • label ๋ฐ์ดํ„ฐ๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์—๊ฒŒ ์ •๋‹ต์ง€

    -> 0, 1, 2์™€ ๊ฐ™์ด ํ‘œํ˜„๋œ label ๋ฐ์ดํ„ฐ

  • ๋ฌธ์ œ์ง€ :

    • ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์—๊ฒŒ ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ.
    • feature๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•จ.
    • ๋ณ€์ˆ˜ ์ด๋ฆ„์œผ๋กœ๋Š” X๋ฅผ ๋งŽ์ด ์‚ฌ์šฉ.
  • ์ •๋‹ต์ง€ :

    • ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์ด ๋งžํ˜€์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ.
    • label ๋˜๋Š” target์ด๋ผ๊ณ  ๋ถ€๋ฆ„.
    • ๋ณ€์ˆ˜ ์ด๋ฆ„์œผ๋กœ๋Š” y๋ฅผ ๋งŽ์ด ์‚ฌ์šฉ

๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์žฅ์น˜

  • ํ•™์Šต์— ์‚ฌ์šฉํ•˜๋Š” training dataset๊ณผ
  • ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” test dataset์œผ๋กœ ๋ฐ์ดํ„ฐ์…‹์„ ๋‚˜๋ˆ„๋Š” ์ž‘์—…
  • ๋ฐ์ดํ„ฐ์…‹์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์€ scikit-learn์ด ์ œ๊ณตํ•˜๋Š” train_test_split ์ด๋ผ๋Š” ํ•จ์ˆ˜

- sklearn.model_selection ํŒจํ‚ค์ง€์˜ train_test_split์„ ํ™œ์šฉ
- training dataset๊ณผ test dataset์„ ๋ถ„๋ฆฌ
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(iris_data,      
                                                    # iris_data๋Š” ๋ฌธ์ œ์ง€ :  feature
                                                    iris_label,     
                                                    # ๋งž์ถฐ์•ผ ํ•  ์ •๋‹ต๊ฐ’ : Label(3๊ฐ€์ง€์ค‘)
                                                    test_size=0.2,  
                                                    # test dataset์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆ : ์ „์ฒด์˜ 20%๋งŒ ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉ
                                                    random_state=7) 
                                                    # train ๋ฐ์ดํ„ฐ์™€ test ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌ(split)ํ•˜๋Š”๋ฐ ์ ์šฉ๋˜๋Š” ๋žœ๋ค์„ฑ

print('X_train ๊ฐœ์ˆ˜: ', len(X_train),', X_test ๊ฐœ์ˆ˜: ', len(X_test))
X_train ๊ฐœ์ˆ˜:  120 , X_test ๊ฐœ์ˆ˜:  30
# X_train๋ถ€ํ„ฐ y_test๊นŒ์ง€ ๋งŒ๋“ค์–ด์ง„ ๋ฐ์ดํ„ฐ์…‹์„ ํ™•์ธ
X_train.shape, y_train.shape
((120, 4), (120,))
X_test.shape, y_test.shape
((30, 4), (30,))

20%์˜ ๋ฐ์ดํ„ฐ๋Š” test ๋ฐ์ดํ„ฐ์…‹

๋‚˜๋จธ์ง€ 80%์˜ ๋ฐ์ดํ„ฐ๋Š” train ๋ฐ์ดํ„ฐ์…‹

y_train, y_test
(array([2, 1, 0, 2, 1, 0, 0, 0, 0, 2, 2, 1, 2, 2, 1, 0, 1, 1, 2, 0, 0, 0,
        2, 0, 2, 1, 1, 1, 0, 0, 0, 1, 2, 1, 1, 0, 2, 0, 0, 2, 2, 0, 2, 0,
        1, 2, 1, 0, 1, 0, 2, 2, 1, 0, 0, 1, 2, 0, 2, 2, 1, 0, 1, 0, 2, 2,
        0, 0, 2, 1, 2, 2, 1, 0, 0, 2, 0, 0, 1, 2, 2, 1, 1, 0, 2, 0, 0, 1,
        1, 2, 0, 1, 1, 2, 2, 1, 2, 0, 1, 1, 0, 0, 0, 1, 1, 0, 2, 2, 1, 2,
        0, 2, 1, 1, 0, 2, 1, 2, 1, 0]),
 array([2, 1, 0, 1, 2, 0, 1, 1, 0, 1, 1, 1, 0, 2, 0, 1, 2, 2, 0, 0, 1, 2,
        1, 2, 2, 2, 1, 1, 2, 2]))

์ฒซ ๋ฒˆ์งธ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ ํ•™์Šต์‹œํ‚ค๊ธฐ

๋จธ์‹ ๋Ÿฌ๋‹

  1. ์ง€๋„ํ•™์Šต(Supervised Learning)
    • ์ง€๋„๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”, ์ฆ‰ ์ •๋‹ต์ด ์žˆ๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๋Š” ๊ฒƒ
      • ๋ถ„๋ฅ˜(Classification)
        • ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ ์ค‘ ํ•˜๋‚˜๋กœ ๋ถ„๋ฅ˜ํ•ด๋‚ด๋Š” ๋ฌธ์ œ
      • ํšŒ๊ท€(Regression)
        • ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ ํŠน์ • ํ•„๋“œ์˜ ์ˆ˜์น˜๋ฅผ ๋งžํžˆ๋Š” ๋ฌธ์ œ
  1. ๋น„์ง€๋„ํ•™์Šต(Unsupervised Learning)
    • ๋น„์ง€๋„ ํ•™์Šต์€ ์ •๋‹ต์ด ์—†๋Š” ๋ฌธ์ œ๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ

๋ถ“๊ฝƒ์€
Supervised Learning / Classification

๋ถ„๋ฅ˜๋ชจ๋ธ

Decision Tree Model
  • ์ฐธ๊ณ  ์˜์‚ฌ๊ฒฐ์ •๋‚˜๋ฌด
  • ์˜์‚ฌ ๊ฒฐ์ •์„ ํ• , ์ฆ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•  ์–ด๋–ค ๊ฒฝ๊ณ„๋ฅผ ์ฐพ์•„๋‚ด์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฒด์— ๊ฑฐ๋ฅด๋“ฏ ํ•œ ๋‹จ๊ณ„์”ฉ ๋ถ„๋ฅ˜ํ•ด๋‚˜๊ฐ€๋Š” ๋ชจ๋ธ
  • ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ด๋‚˜๊ฐ€๋Š” ๋ชจ์Šต์ด ๋‚˜๋ฌด๋ฅผ ๋’ค์ง‘์–ด ๋†“์€ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ชจ์–‘
  • ์ˆœ๋„(homogeneity)๊ฐ€ ์ฆ๊ฐ€
  • ๋ถˆ์ˆœ๋„(impurity) ํ˜น์€ ๋ถˆํ™•์‹ค์„ฑ(uncertainty)์ด ์ตœ๋Œ€ํ•œ ๊ฐ์†Œํ•˜๋„๋ก ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํ•™์Šต์„ ์ง„ํ–‰
    • ์ˆœ๋„๊ฐ€ ์ฆ๊ฐ€/๋ถˆํ™•์‹ค์„ฑ์ด ๊ฐ์†Œํ•˜๋Š” ๊ฑธ ๋‘๊ณ  ์ •๋ณด์ด๋ก ์—์„œ๋Š” ์ •๋ณดํš๋“(information gain)
    • ๋ฐ์ดํ„ฐ๊ฐ€ ๊ท ์ผํ•œ ์ •๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ง€ํ‘œ, ์ฆ‰ ์ˆœ๋„
    • ์ˆœ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ์‹
      • ์—”ํŠธ๋กœํ”ผ(entropy)
      • ์ง€๋‹ˆ๊ณ„์ˆ˜(Gini Index)
      • ์˜ค๋ถ„๋ฅ˜์˜ค์ฐจ(misclassification error)
        • ์˜ค๋ถ„๋ฅ˜์˜ค์ฐจ๋Š” ์—”ํŠธ๋กœํ”ผ๋‚˜ ์ง€๋‹ˆ๊ณ„์ˆ˜์™€ ๋”๋ถˆ์–ด ๋ถˆ์ˆœ๋„๋ฅผ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์Œ
        • ๋‚˜๋จธ์ง€ ๋‘ ์ง€ํ‘œ์™€ ๋‹ฌ๋ฆฌ ๋ฏธ๋ถ„์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์  ๋•Œ๋ฌธ์— ์ž์ฃผ ์“ฐ์ด์ง€๋Š” ์•Š์Œ
  • ํ•™์Šต๊ณผ์ •
    • ์ž…๋ ฅ ๋ณ€์ˆ˜ ์˜์—ญ์„ ๋‘ ๊ฐœ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ์žฌ๊ท€์  ๋ถ„๊ธฐ(recursive partitioning) ๊ณผ์ •
    • ์ž์„ธํ•˜๊ฒŒ ๊ตฌ๋ถ„๋œ ์˜์—ญ์„ ํ†ตํ•ฉํ•˜๋Š” ๊ฐ€์ง€์น˜๊ธฐ(pruning) ๊ณผ์ •
      • ๋ชจ๋“  terminal node์˜ ์ˆœ๋„๊ฐ€ 100%์ธ ์ƒํƒœ๋ฅผ Full tree
      • Full tree๋ฅผ ์ƒ์„ฑํ•œ ๋’ค ์ ์ ˆํ•œ ์ˆ˜์ค€์—์„œ terminal node๋ฅผ ๊ฒฐํ•ฉํ•ด์ฃผ์–ด์•ผ ํ•จ
        • ๋ถ„๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ํ•™์Šต๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ(overfitting)ํ•  ์—ผ๋ ค
        • ์ฒ˜์Œ์—๋Š” ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค๋ถ„๋ฅ˜์œจ์ด ๊ฐ์†Œ
        • ์ผ์ • ์ˆ˜์ค€ ์ด์ƒ์ด ๋˜๋ฉด ์˜ค๋ถ„๋ฅ˜์œจ์ด ๋˜๋ ˆ ์ฆ๊ฐ€ํ•˜๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒ
        • ๋ถ„๊ธฐ๋ฅผ ํ•ฉ์น˜๋Š”(merge) ๊ฐœ๋…
      • ๋น„์šฉํ•จ์ˆ˜(cost function)
        • ๐ถ๐ถ(๐‘‡)=๐ธ๐‘Ÿ๐‘Ÿ(๐‘‡)+๐›ผร—๐ฟ(๐‘‡)
          • CC(T)=์˜์‚ฌ๊ฒฐ์ •๋‚˜๋ฌด์˜ ๋น„์šฉ ๋ณต์žก๋„
            (=์˜ค๋ฅ˜๊ฐ€ ์ ์œผ๋ฉด์„œ terminal node ์ˆ˜๊ฐ€ ์ ์€ ๋‹จ์ˆœํ•œ ๋ชจ๋ธ์ผ ์ˆ˜๋ก ์ž‘์€ ๊ฐ’)
          • ERR(T)=๊ฒ€์ฆ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค๋ถ„๋ฅ˜์œจ
          • L(T)=terminal node์˜ ์ˆ˜ (๊ตฌ์กฐ์˜ ๋ณต์žก๋„)
          • Alpha=ERR(T)์™€ L(T)๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฐ€์ค‘์น˜
            (์‚ฌ์šฉ์ž์— ์˜ํ•ด ๋ถ€์—ฌ๋จ, ๋ณดํ†ต 0.01~0.1์˜ ๊ฐ’์„ ์”€)
  • ์žฅ์ 
    • ๊ณ„์‚ฐ๋ณต์žก์„ฑ ๋Œ€๋น„ ๋†’์€ ์˜ˆ์ธก ์„ฑ๋Šฅ์„ ๋‚ด๋Š” ๊ฒƒ
    • ๋ณ€์ˆ˜ ๋‹จ์œ„๋กœ ์„ค๋ช…๋ ฅ์„ ์ง€๋‹Œ๋‹ค๋Š” ๊ฐ•์ 
  • ๋‹จ์ 
    • ๊ฒฐ์ •๊ฒฝ๊ณ„(decision boundary)๊ฐ€ ๋ฐ์ดํ„ฐ ์ถ•์— ์ˆ˜์ง์ด์–ด์„œ ํŠน์ • ๋ฐ์ดํ„ฐ์—๋งŒ ์ž˜ ์ž‘๋™ํ•  ๊ฐ€๋Šฅ์„ฑ
  • ๋Œ€์•ˆ
    • RandomForest
      • ๊ฐ™์€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์˜์‚ฌ๊ฒฐ์ •๋‚˜๋ฌด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค์–ด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉํ•ด ์˜ˆ์ธก ์„ฑ๋Šฅ์„ ๋†’์ด๋Š” ๊ธฐ๋ฒ•

Decidion Tree ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•

# Decision Tree๋Š” sklearn.tree ํŒจํ‚ค์ง€ ์•ˆ์— DecisionTreeClassifier ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋‚ด์žฅ

from sklearn.tree import DecisionTreeClassifier

# ๋ชจ๋ธ์„ importํ•ด์„œ ๊ฐ€์ ธ์˜ค๊ณ , decision_tree ๋ผ๋Š” ๋ณ€์ˆ˜์— ๋ชจ๋ธ์„ ์ €์žฅ

decision_tree = DecisionTreeClassifier(random_state=32)
print(decision_tree._estimator_type)
classifier

์œ„ ๊ฒฐ๊ณผ์˜ ์˜๋ฏธ๋Š”?

๋ชจ๋ธ ํ•™์Šต์€ ์šฐ๋ฆฌ๊ฐ€ ์ค€๋น„ํ•ด ๋‘” X_train ์™€ y_train ๋ฐ์ดํ„ฐ๋กœ
๋‹ค์Œ ํ•œ ์ค„์ด๋ฉด ์™„๋ฃŒ

decision_tree.fit(X_train, y_train) 
#๋ชจ๋ธ ํ•™์Šต์„ ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ค€๋น„ํ•ด๋‘” X_train y_train๋กœ ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด์— fit
DecisionTreeClassifier(random_state=32)
XGBoost
  • ๊ฐ™์€ ๋ฐ์ดํ„ฐ์— ์˜์‚ฌ๊ฒฐ์ •๋‚˜๋ฌด ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋™์‹œ์— ์ ์šฉํ•ด์„œ ํ•™์Šต์„ฑ๋Šฅ์„ ๋†’์ด๋Š” ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•
  • ์ž‘๋™ ๋ฐฉ์‹
    • ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋ณต์›์ถ”์ถœ์„ ํ†ตํ•ด
    • 30๊ฐœ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์–ด
    • ๊ฐ๊ฐ์— ์˜์‚ฌ๊ฒฐ์ •๋‚˜๋ฌด๋ฅผ ์ ์šฉํ•œ ๋’ค
    • ํ•™์Šต ๊ฒฐ๊ณผ๋ฅผ ์ทจํ•ฉํ•˜๋Š” ๋ฐฉ์‹
  • ์„ฑ๋Šฅ ์ง€ํ‘œ๋Š” ๋‹จ์ˆœ์ •ํ™•๋„(accuracy)
RandomForest
๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจ๋ธ

์ฒซ ๋ฒˆ์งธ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ ํ‰๊ฐ€ํ•˜๊ธฐ

test ๋ฐ์ดํ„ฐ๋กœ ์˜ˆ์ธก

y_pred = decision_tree.predict(X_test)
y_pred
array([2, 1, 0, 1, 2, 0, 1, 1, 0, 1, 2, 1, 0, 2, 0, 2, 2, 2, 0, 0, 1, 2,
       1, 1, 2, 2, 1, 1, 2, 2])

X_test ๋ฐ์ดํ„ฐ์—๋Š” ์ •๋‹ต์ธ label์ด ์—†๊ณ  feature ๋ฐ์ดํ„ฐ๋งŒ ์กด์žฌ

ํ•™์Šต์ด ์™„๋ฃŒ๋œ decision_tree ๋ชจ๋ธ์—

X_test ๋ฐ์ดํ„ฐ๋กœ predict๋ฅผ ์‹คํ–‰ํ•˜๋ฉด

๋ชจ๋ธ์ด ์˜ˆ์ธกํ•œ y_pred์„ ์–ป๊ฒŒ ๋จ

๋ชจ๋ธ์€ ์ด 30๊ฐœ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด
array([2, 1, 0, 1, 2, 0, 1, 1, 0, 1, 2, 1, 0, 2, 0, 2, 2, 2, 0, 0, 1, 2,
1, 1, 2, 2, 1, 1, 2, 2])๋ผ๋Š” ์˜ˆ์ธก ๊ฒฐ๊ณผ

์‹ค์ œ ์ •๋‹ต์ธ y_test์™€ ๋น„๊ตํ•ด์„œ ์–ผ๋งˆ๋‚˜ ๋งž์•˜๋Š”์ง€ ํ™•์ธ

y_test
array([2, 1, 0, 1, 2, 0, 1, 1, 0, 1, 1, 1, 0, 2, 0, 1, 2, 2, 0, 0, 1, 2,
       1, 2, 2, 2, 1, 1, 2, 2])

์•Œ์•„๋ณด๊ธฐ ํž˜๋“ฆ ใ…œใ…œ

์‰ฝ๊ฒŒ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์€

scikit-learn์—์„œ ์„ฑ๋Šฅ ํ‰๊ฐ€์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” sklearn.metrics ํŒจํ‚ค์ง€๋ฅผ ์ด์šฉ

# ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์ฒ™๋„ ์ค‘ ์ •ํ™•๋„(Accuracy) ํ™•์ธ
from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, y_pred)
accuracy
0.9

0.9๋Š” ์ˆ˜์น˜๋Š” 90% ์ •๋„์˜ ์ •ํ™•๋„

[[[์ด์ •๋ฆฌ]]]๋‹ค๋ฅธ ๋ชจ๋ธ๋„ ํ•ด ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด? ์ฝ”๋“œ ํ•œ ์ค„๋งŒ ๋ฐ”๊พธ๋ฉด ๋ผ!

# ์•ž ๋‚ด์šฉ ์ •๋ฆฌ

# (1) ํ•„์š”ํ•œ ๋ชจ๋“ˆ import
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report

# (2) ๋ฐ์ดํ„ฐ ์ค€๋น„
iris = load_iris()
iris_data = iris.data
iris_label = iris.target

# (3) train, test ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ
X_train, X_test, y_train, y_test = train_test_split(iris_data, 
                                                    iris_label, 
                                                    test_size=0.2, 
                                                    random_state=7)

# (4) ๋ชจ๋ธ ํ•™์Šต ๋ฐ ์˜ˆ์ธก -> ๋ชจ๋ธ์„ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ ๋ณ€๊ฒฝํ•˜๋Š” ๋ถ€๋ถ„
decision_tree = DecisionTreeClassifier(random_state=32)
decision_tree.fit(X_train, y_train)
y_pred = decision_tree.predict(X_test)

print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       1.00      1.00      1.00         7
           1       0.91      0.83      0.87        12
           2       0.83      0.91      0.87        11

    accuracy                           0.90        30
   macro avg       0.91      0.91      0.91        30
weighted avg       0.90      0.90      0.90        30
Random Forest
  • Random Forest๋Š” ์ƒ์œ„ ๋ชจ๋ธ๋“ค์ด ์˜ˆ์ธกํ•˜๋Š” ํŽธํ–ฅ๋œ ๊ฒฐ๊ณผ๋ณด๋‹ค,
    ๋‹ค์–‘ํ•œ ๋ชจ๋ธ๋“ค์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜์˜ํ•จ์œผ๋กœ์จ ๋” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜์‚ฌ๊ฒฐ์ • ๊ฐ€๋Šฅ
  • Decision Tree๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ
  • Decision Tree์˜ ๋‹จ์ ์„ ๊ทน๋ณตํ•œ ๋ชจ๋ธ
    • ์•™์ƒ๋ธ”(Ensemble) ๊ธฐ๋ฒ•
    • ๋‹จ์ผ ๋ชจ๋ธ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ทจํ•จ์œผ๋กœ์จ
      ๋ชจ๋ธ ํ•œ ๊ฐœ๋งŒ ์‚ฌ์šฉํ•  ๋•Œ์˜ ๋‹จ์ ์„ ์ง‘๋‹จ์ง€์„ฑ์œผ๋กœ ๊ทน๋ณตํ•˜๋Š” ๊ฐœ๋…
  • Random Forest๋Š” ๊ฐ๊ฐ์˜ ์˜์‚ฌ ๊ฒฐ์ • ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์žˆ์–ด ์“ฐ์ด๋Š” ์š”์†Œ๋“ค
    (ํก์—ฐ ์—ฌ๋ถ€, ๋‚˜์ด, ๋“ฑ๋“ฑ)์„ ๋ฌด์ž‘์œ„์ ์œผ๋กœ ์„ ์ •
  • 30๊ฐœ ์ค‘ ๋ฌด์ž‘์œ„๋กœ ์ผ๋ถ€๋งŒ ์„ ํƒํ•˜์—ฌ,
    ๊ทธ ์„ ํƒ๋œ ์ผ๋ถ€ ์ค‘ ๊ฐ€์žฅ ๊ฑด๊ฐ• ์œ„ํ—˜๋„๋ฅผ ์•Œ๋งž๊ฒŒ ์˜ˆ์ธกํ•˜๋Š”
    ํ•œ ๊ฐ€์ง€ ์š”์†Œ๊ฐ€ ์˜์‚ฌ ๊ฒฐ์ • ํŠธ๋ฆฌ์˜ ํ•œ ๋‹จ๊ณ„
  • ์˜์‚ฌ๊ฒฐ์ • ๊ณผ์ •
    1. ๊ฑด๊ฐ•์˜ ์œ„ํ—˜๋„๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งŽ์€ ์š”์†Œ๋ฅผ ๊ณ ๋ ค
      ์„ฑ๋ณ„, ํ‚ค, ๋ชธ๋ฌด๊ฒŒ, ์ง€์—ญ, ์šด๋™๋Ÿ‰, ํก์—ฐ์œ ๋ฌด, ์Œ์ฃผ ์—ฌ๋ถ€,
      ํ˜ˆ๋‹น, ๊ทผ์œก๋Ÿ‰, ๊ธฐ์ดˆ ๋Œ€์‚ฌ๋Ÿ‰ ๋“ฑ ์ˆ˜๋งŽ์€ ์š”์†Œ๊ฐ€ ํ•„์š”
    2. Feature๊ฐ€ 30๊ฐœ๋ผ ํ–ˆ์„ ๋•Œ 30๊ฐœ์˜ Feature๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ
      ํ•˜๋‚˜์˜ ๊ฒฐ์ • ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ํŠธ๋ฆฌ์˜ ๊ฐ€์ง€๊ฐ€ ๋งŽ์•„์งˆ ๊ฒƒ์ด๊ณ ,
      ์ด๋Š” ์˜ค๋ฒ„ํ”ผํŒ…์˜ ๊ฒฐ๊ณผ๋ฅผ ์•ผ๊ธฐ
      3.30๊ฐœ์˜ Feature ์ค‘ ๋žœ๋ค์œผ๋กœ 5๊ฐœ์˜ Feature๋งŒ ์„ ํƒํ•ด์„œ ํ•˜๋‚˜์˜ ๊ฒฐ์ • ํŠธ๋ฆฌ ์ƒ์„ฑ
    3. ๊ณ„์† ๋ฐ˜๋ณตํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฒฐ์ • ํŠธ๋ฆฌ ์ƒ์„ฑ
    4. ์—ฌ๋Ÿฌ ๊ฒฐ์ • ํŠธ๋ฆฌ๋“ค์ด ๋‚ด๋ฆฐ ์˜ˆ์ธก ๊ฐ’๋“ค ์ค‘ ๊ฐ€์žฅ ๋งŽ์ด ๋‚˜์˜จ ๊ฐ’์„ ์ตœ์ข… ์˜ˆ์ธก๊ฐ’์œผ๋กœ ์ง€์ •
    5. ์ด๋ ‡๊ฒŒ ์˜๊ฒฌ์„ ํ†ตํ•ฉํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฒฐ๊ณผ๋ฅผ ํ•ฉ์น˜๋Š” ๋ฐฉ์‹์„ ์•™์ƒ๋ธ”(Ensemble)์ด๋ผ๊ณ  ํ•จ
    6. ํ•˜๋‚˜์˜ ๊ฑฐ๋Œ€ํ•œ (๊นŠ์ด๊ฐ€ ๊นŠ์€) ๊ฒฐ์ • ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์€ ๊ฒฐ์ • ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ
    7. ๋ถ„๋ฅ˜ : ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์€ ๊ฒฐ์ • ํŠธ๋ฆฌ๊ฐ€ ์˜ˆ์ธกํ•œ ๊ฐ’๋“ค ์ค‘ ๊ฐ€์žฅ ๋งŽ์€ ๊ฐ’, ํšŒ๊ท€ : ํ‰๊ท 
# Random Forest๋Š” sklearn.ensemble ํŒจํ‚ค์ง€ ๋‚ด
from sklearn.ensemble import RandomForestClassifier

X_train, X_test, y_train, y_test = train_test_split(iris_data, 
                                                    iris_label, 
                                                    test_size=0.2, 
                                                    random_state=21)

random_forest = RandomForestClassifier(random_state=32)
random_forest.fit(X_train, y_train)
y_pred = random_forest.predict(X_test)

print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       1.00      1.00      1.00        11
           1       1.00      0.83      0.91        12
           2       0.78      1.00      0.88         7

    accuracy                           0.93        30
   macro avg       0.93      0.94      0.93        30
weighted avg       0.95      0.93      0.93        30

๋‹ค๋ฅธ scikit-learn ๋‚ด์žฅ ๋ถ„๋ฅ˜ ๋ชจ๋ธ


Support Vector Machine (SVM)
  • Support Vector์™€ Hyperplane(์ดˆํ‰๋ฉด)์„ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰
  • ๋Œ€ํ‘œ์ ์ธ ์„ ํ˜• ๋ถ„๋ฅ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜

2 ์ฐจ์› ๊ณต๊ฐ„์—์„œ, ์ฆ‰ ๋ฐ์ดํ„ฐ์— 2๊ฐœ์˜ ํด๋ž˜์Šค๋งŒ ์กด์žฌํ•  ๋•Œ,

  • Decision Boundary(๊ฒฐ์ • ๊ฒฝ๊ณ„): ๋‘ ๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ๊ตฌ๋ถ„ํ•ด ์ฃผ๋Š” ์„ 
  • Support Vector: Decision Boundary์— ๊ฐ€๊นŒ์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ
  • Margin: Decision Boundary์™€ Support Vector ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ

Margin์ด ๋„“์„์ˆ˜๋ก ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. (Margin ์ตœ๋Œ€ํ™” -> robustness ์ตœ๋Œ€ํ™”)

  • Kernel Trick: ์ €์ฐจ์›์˜ ๊ณต๊ฐ„์„ ๊ณ ์ฐจ์›์˜ ๊ณต๊ฐ„์œผ๋กœ ๋งคํ•‘ํ•ด์ฃผ๋Š” ์ž‘์—….
    ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๊ฐ€ Linearly separable ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ
    ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ์ฐจ์›์œผ๋กœ ์ด๋™์‹œ์ผœ Linearly separableํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค.
  • cost: Decision Boundary์™€ Margin์˜ ๊ฐ„๊ฒฉ ๊ฒฐ์ •.
    cost๊ฐ€ ๋†’์œผ๋ฉด Margin์ด ์ข์•„์ง€๊ณ  train error๊ฐ€ ์ž‘์•„์ง„๋‹ค.
    ๊ทธ๋Ÿฌ๋‚˜ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์—์„œ๋Š” ๋ถ„๋ฅ˜๋ฅผ ์ž˜ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    cost๊ฐ€ ๋‚ฎ์œผ๋ฉด Margin์ด ๋„“์–ด์ง€๊ณ , train error๋Š” ์ปค์ง„๋‹ค.
  • ฮณ: ํ•œ train data๋‹น ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๋ฒ”์œ„ ๊ฒฐ์ •.
    ฮณ๊ฐ€ ์ปค์ง€๋ฉด ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๋ฒ”์œ„๊ฐ€ ์ค„์–ด๋“ค๊ณ ,
    Decision Boundary์— ๊ฐ€๊นŒ์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋งŒ์ด ์„ ์˜ ๊ตด๊ณก์— ์˜ํ–ฅ์„ ์ค€๋‹ค.
    ๋”ฐ๋ผ์„œ Decision Boundary๋Š” ๊ตฌ๋ถˆ๊ตฌ๋ถˆํ•˜๊ฒŒ ๊ทธ์–ด์ง„๋‹ค. (์˜ค๋ฒ„ํ”ผํŒ… ์ดˆ๋ž˜ ๊ฐ€๋Šฅ)
    ์ž‘์•„์ง€๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๋ฒ”์œ„๊ฐ€ ์ปค์ง€๊ณ ,
    ๋Œ€๋ถ€๋ถ„์˜ ๋ฐ์ดํ„ฐ๊ฐ€ Decision Boundary์— ์˜ํ–ฅ์„ ์ค€๋‹ค.
    ๋”ฐ๋ผ์„œ Decision Boundary๋ฅผ ์ง์„ ์— ๊ฐ€๊นŒ์›Œ์ง„๋‹ค.

SVM ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•

from sklearn import svm
svm_model = svm.SVC()

print(svm_model._estimator_type)
classifier
# ๋ถ„๊ฝƒ์— ์ฝ”๋“œ๋ฅผ ์ ์šฉ
svm_model.fit(X_train, y_train)
y_pred = svm_model.predict(X_test)

print(classification_report(y_test, y_pred))

# ์•„๋ž˜๋Š” decision_tree

# decision_tree = DecisionTreeClassifier(random_state=32)
# decision_tree.fit(X_train, y_train)
# y_pred = decision_tree.predict(X_test)

# print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       1.00      1.00      1.00        11
           1       0.91      0.83      0.87        12
           2       0.75      0.86      0.80         7

    accuracy                           0.90        30
   macro avg       0.89      0.90      0.89        30
weighted avg       0.91      0.90      0.90        30
Stochastic Gradient Descent Classifier (SGDClassifier)
  • SGD (Stochastic Gradient Descent)
  • ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ธ ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ• ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ํ™•๋ฅ ์  ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•์€ ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ
    ๋ฌด์ž‘์œ„๋กœ ๊ท ์ผํ•˜๊ฒŒ ์„ ํƒํ•œ ํ•˜๋‚˜์˜ ์˜ˆ๋ฅผ ์˜์กดํ•˜์—ฌ
    ๊ฐ ๋‹จ๊ณ„์˜ ์˜ˆ์ธก ๊ฒฝ์‚ฌ๋ฅผ ๊ณ„์‚ฐ

<์ตœ์†Œ๊ฐ’์„ ์ฐพ๋Š” ๊ณผ์ •>

๋ฐฐ์น˜

  • ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•์—์„œ ๋ฐฐ์น˜๋Š” ๋‹จ์ผ ๋ฐ˜๋ณต์—์„œ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ(data)์˜ ์ด ๊ฐœ์ˆ˜
  • Gradient Descent ์—์„œ์˜ ๋ฐฐ์น˜๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹๋ผ๊ณ  ๊ฐ€์ •

    ์—„์ฒญ๋‚œ ๋ฐ์ดํ„ฐ ์…‹

๋งŒ์•ฝ์— ํ›จ์”ฌ ์ ์€ ๊ณ„์‚ฐ์œผ๋กœ ์ ์ ˆํ•œ ๊ธฐ์šธ๊ธฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค๋ฉด?

  • ํ™•๋ฅ ์  ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•(SGD)์€ ์ด ์•„์ด๋””์–ด๋ฅผ ๋”์šฑ ํ™•์žฅํ•œ ๊ฒƒ
  • ๋ฐ˜๋ณต๋‹น ํ•˜๋‚˜์˜ ์˜ˆ(๋ฐฐ์น˜ ํฌ๊ธฐ 1)๋งŒ์„ ์‚ฌ์šฉ
    • 'ํ™•๋ฅ ์ (Stochastic)'์ด๋ผ๋Š” ์šฉ์–ด๋Š” ๊ฐ ๋ฐฐ์น˜๋ฅผ ํฌํ•จํ•˜๋Š” ํ•˜๋‚˜์˜ ์˜ˆ๊ฐ€ ๋ฌด์ž‘์œ„๋กœ ์„ ํƒ๋œ๋‹ค๋Š” ๊ฒƒ
  • ๋‹จ์ 
    • ๋ฐ˜๋ณต์ด ์ถฉ๋ถ„ํ•˜๋ฉด SGD๊ฐ€ ํšจ๊ณผ๋Š” ์žˆ์ง€๋งŒ ๋…ธ์ด์ฆˆ๊ฐ€ ์‹ฌํ•จ
      -ํ™•๋ฅ ์  ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•์˜ ์—ฌ๋Ÿฌ ๋ณ€ํ˜• ํ•จ์ˆ˜์˜ ์ตœ์ €์ ์— ๊ฐ€๊นŒ์šด ์ ์„
      ์ฐพ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์ง€๋งŒ ํ•ญ์ƒ ๋ณด์žฅ๋˜์ง€๋Š” ์•Š์Œ
  • ๋‹จ์  ๊ทน๋ณต
    • ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํ™•๋ฅ ์  ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•(๋ฏธ๋‹ˆ ๋ฐฐ์น˜ SGD)๋Š”
      ์ „์ฒด ๋ฐฐ์น˜ ๋ฐ˜๋ณต๊ณผ SGD ์˜ ์ ˆ์ถฉ์•ˆ
      - ๋ฌด์ž‘์œ„๋กœ ์„ ํƒํ•œ 10๊ฐœ์—์„œ 1,000๊ฐœ ์‚ฌ์ด์˜ ์˜ˆ๋กœ ๊ตฌ์„ฑ
      - ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ SGD๋Š” SGD์˜ ๋…ธ์ด์ฆˆ๋ฅผ ์ค„์ด๋ฉด์„œ๋„
      ์ „์ฒด ๋ฐฐ์น˜๋ณด๋‹ค๋Š” ๋” ํšจ์œจ์ 

SGD Classifier ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•

from sklearn.linear_model import SGDClassifier
sgd_model = SGDClassifier()

print(sgd_model._estimator_type)


# ์ฐธ๊ณ  SVM ์‚ฌ์šฉ๋ฒ•
        from sklearn import svm
        svm_model = svm.SVC()

        print(svm_model._estimator_type)

# SVM ์ ์šฉ ์˜ˆ 
        svm_model.fit(X_train, y_train)
        y_pred = svm_model.predict(X_test)

        print(classification_report(y_test, y_pred))

        
# Tree ์‚ฌ์šฉ๋ฒ•
from sklearn.tree import DecisionTreeClassifier
decision_tree = DecisionTreeClassifier(random_state=32)

print(decision_tree._estimator_type)

#Tree ์ ์šฉ ์˜ˆ
decision_tree = DecisionTreeClassifier(random_state=32)
decision_tree.fit(X_train, y_train)
y_pred = decision_tree.predict(X_test)

print(classification_report(y_test, y_pred))
  File "/tmp/ipykernel_379/2372895087.py", line 8
    from sklearn import svm
    ^
IndentationError: unexpected indent
# ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” ์ ์šฉ ์˜ˆ
sgd_model.fit(X_train, y_train)
y_pred = sgd_model.predict(X_test)

print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       1.00      1.00      1.00        11
           1       1.00      0.92      0.96        12
           2       0.88      1.00      0.93         7

    accuracy                           0.97        30
   macro avg       0.96      0.97      0.96        30
weighted avg       0.97      0.97      0.97        30
# ์ ์šฉ์˜ˆ ๋‹ต์•ˆ
sgd_model.fit(X_train, y_train)
y_pred = sgd_model.predict(X_test)

print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       1.00      1.00      1.00        11
           1       0.86      1.00      0.92        12
           2       1.00      0.71      0.83         7

    accuracy                           0.93        30
   macro avg       0.95      0.90      0.92        30
weighted avg       0.94      0.93      0.93        30
Logistic Regression
  • ๊ฐ€์žฅ ๋„๋ฆฌ ์•Œ๋ ค์ง„ ์„ ํ˜• ๋ถ„๋ฅ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜.
  • ์†Œํ”„ํŠธ๋งฅ์Šค(softmas) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€(Softmax Regression)
  • ์ด๋ฆ„์€ ํšŒ๊ท€์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰

์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜

  • ํด๋ž˜์Šค๊ฐ€ N๊ฐœ์ผ ๋•Œ, N์ฐจ์›์˜ ๋ฒกํ„ฐ๊ฐ€ ๊ฐ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ ํ‘œํ˜„ํ•˜๋„๋ก ์ •๊ทœํ™”๋ฅผ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜.
  • ์˜ˆ์‹œ๋Š” 4์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ 3๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ๋™์ž‘ ๊ณผ์ •
  • 3๊ฐœ์˜ ํด๋ž˜์Šค ์ค‘ 1๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ์ถœ๋ ฅ์€ 3์ฐจ์›์˜ ๋ฒกํ„ฐ
  • ๊ฐ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ ํŠน์ • ํด๋ž˜์Šค์ผ ํ™•๋ฅ 
  • ์˜ค์ฐจ์™€ ์‹ค์ œ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ์ค„์ด๋Š” ๊ณผ์ •์—์„œ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์ด ํ•™์Šต

Logistic Regression ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•

from sklearn.linear_model import LogisticRegression
logistic_model = LogisticRegression()

print(logistic_model._estimator_type)

# ์ฐธ๊ณ  SGD ์‚ฌ์šฉ๋ฒ•

                    from sklearn.linear_model import SGDClassifier
                    sgd_model = SGDClassifier()
    
                    print(sgd_model._estimator_type)
    
# SDG ์ ์šฉ ์˜ˆ

                    sgd_model.fit(X_train, y_train)
                    y_pred = sgd_model.predict(X_test)
            
                    print(classification_report(y_test, y_pred))


# ์ฐธ๊ณ  SVM ์‚ฌ์šฉ๋ฒ•
        from sklearn import svm
        svm_model = svm.SVC()

        print(svm_model._estimator_type)

# SVM ์ ์šฉ ์˜ˆ 
        svm_model.fit(X_train, y_train)
        y_pred = svm_model.predict(X_test)

        print(classification_report(y_test, y_pred))

        
# Tree ์‚ฌ์šฉ๋ฒ•
                from sklearn.tree import DecisionTreeClassifier
                decision_tree = DecisionTreeClassifier(random_state=32)

                print(decision_tree._estimator_type)

#Tree ์ ์šฉ ์˜ˆ
                decision_tree = DecisionTreeClassifier(random_state=32)
                decision_tree.fit(X_train, y_train)
                y_pred = decision_tree.predict(X_test)

                print(classification_report(y_test, y_pred))
  File "<tokenize>", line 22
    from sklearn import svm
    ^
IndentationError: unindent does not match any outer indentation level
# ์ ์šฉ ์˜ˆ
# ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
logistic_model.fit(X_train, y_train)
y_pred = logistic_model.predict(X_test)

print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       1.00      1.00      1.00        11
           1       1.00      0.83      0.91        12
           2       0.78      1.00      0.88         7

    accuracy                           0.93        30
   macro avg       0.93      0.94      0.93        30
weighted avg       0.95      0.93      0.93        30

๋‚ด ๋ชจ๋ธ์€ ์–ผ๋งˆ๋‚˜ ๋˜‘๋˜‘ํ•œ๊ฐ€? ๋‹ค์–‘ํ•˜๊ฒŒ ํ‰๊ฐ€ํ•ด ๋ณด๊ธฐ

  • ๋จธ์‹ ๋Ÿฌ๋‹์—์„œ๋Š” ์„ฑ๋Šฅ์„ ์ •ํ™•ํžˆ ํ‰๊ฐ€ํ•˜๊ณ  ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์ค‘์š”
  • ์ •ํ™•๋„๋ผ๋Š” ์ฒ™๋„๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ™•์ธ
  • ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ์—๋Š” ์ •ํ™•๋„๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ฒ™๋„๋“ค์ด ์กด์žฌ

์ •ํ™•๋„์—๋Š” ํ•จ์ •์ด ์žˆ๋‹ค


ํ•จ์ •์˜ ์‚ฌ๋ก€

from sklearn.datasets import load_digits

digits = load_digits()
digits.keys()
dict_keys(['data', 'target', 'frame', 'feature_names', 'target_names', 'images', 'DESCR'])

digits ๋ผ๋Š” ๋ณ€์ˆ˜์— ์†๊ธ€์”จ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ–ˆ๊ณ , ๊ทธ ์•ˆ์—๋Š” iris ๋ฐ์ดํ„ฐ์™€ ๋˜‘๊ฐ™์ด ๋ช‡ ๊ฐ€์ง€์˜ ์ •๋ณด

# ๊ฐ€์žฅ ์ค‘์š”ํ•œ data๋ฅผ ๋จผ์ € ํ™•์ธ
digits_data = digits.data
digits_data.shape
(1797, 64)

๋ฐ์ดํ„ฐ๋Š” ์ด 1,797๊ฐœ,
๊ฐ ๋ฐ์ดํ„ฐ๋Š” 64๊ฐœ์˜ ์ˆซ์ž.

# 1,797๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ˜ํ”Œ๋กœ ํ™•์ธ
digits_data[0]
array([ 0.,  0.,  5., 13.,  9.,  1.,  0.,  0.,  0.,  0., 13., 15., 10.,
       15.,  5.,  0.,  0.,  3., 15.,  2.,  0., 11.,  8.,  0.,  0.,  4.,
       12.,  0.,  0.,  8.,  8.,  0.,  0.,  5.,  8.,  0.,  0.,  9.,  8.,
        0.,  0.,  4., 11.,  0.,  1., 12.,  7.,  0.,  0.,  2., 14.,  5.,
       10., 12.,  0.,  0.,  0.,  0.,  6., 13., 10.,  0.,  0.,  0.])

์˜ˆ์ƒ๋Œ€๋กœ 64๊ฐœ์˜ ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด(array)์ด ์ถœ๋ ฅ

์ˆซ์ž๋Š” ์–ด๋–ค ์˜๋ฏธ๊ฐ€ ์žˆ์„๊นŒ์š”?

์†๊ธ€์”จ ๋ฐ์ดํ„ฐ๋Š” ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ. -> ๊ฐ ์ˆซ์ž๋Š” ํ”ฝ์…€๊ฐ’ ์˜๋ฏธ
๊ธธ์ด 64์˜ ์ˆซ์ž ๋ฐฐ์—ด์€ ์‚ฌ์‹ค (8 x 8) ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ผ๋ ฌ๋กœ ์ญ‰ ํŽด๋†“์€ ๊ฒƒ

์ด๋ฏธ์ง€๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” matplotlib์ด๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”.

matplotlib.pyplot์„ plt๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ณ ,
์ด๋ฏธ์ง€๋ฅผ ํ˜„์žฌ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด
%matplotlib inline์ด๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€

# ์ผ๋ ฌ๋กœ ํŽด์ง„ 64๊ฐœ ๋ฐ์ดํ„ฐ๋ฅผ (8, 8)๋กœ reshape

import matplotlib.pyplot as plt
%matplotlib inline

plt.imshow(digits.data[0].reshape(8, 8), cmap='gray')
plt.axis('off')
plt.show()


# ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ํ•œ ๋ฒˆ์— ํ™•์ธ
for i in range(10):
    plt.subplot(2, 5, i+1)
    plt.imshow(digits.data[i].reshape(8, 8), cmap='gray')
    plt.axis('off')
plt.show()


# target ๋ฐ์ดํ„ฐ๋Š”?
digits_label = digits.target
print(digits_label.shape)
digits_label[:20]
(1797,)





array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

์ด 1,797๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๊ณ , 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๋กœ ๋‚˜ํƒ€๋‚จ.
๋ฐ”๋กœ ๊ฐ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”์ง€๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ

๋ถ“๊ฝƒ ๋ฌธ์ œ์™€ ๊ฐ™์ด,
๊ฐ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ
๊ทธ ์ด๋ฏธ์ง€๊ฐ€ ์ˆซ์ž ๋ช‡์„ ๋‚˜ํƒ€๋‚ด๋Š”
์ด๋ฏธ์ง€์ธ์ง€๋ฅผ ๋งž์ถ”๋Š” ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ํ•™์Šต

์ •ํ™•๋„์˜ ํ•จ์ •์„ ํ™•์ธํ•˜๋Š” ์‹คํ—˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์•ฝ๊ฐ„์˜ ์žฅ์น˜๋ฅผ ๋„ฃ์–ด๋ณผ ๊ฒƒ
->์ˆซ์ž 10๊ฐœ๋ฅผ ๋ชจ๋‘ ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ,
ํ•ด๋‹น ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๊ฐ€ 3์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋งžํžˆ๋Š” ๋ฌธ์ œ๋กœ ๋ณ€ํ˜•ํ•ด์„œ ํ’€์–ด๋ณด๋Š” ๊ฒƒ

  • ์ฆ‰ ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ 3์ด๋ผ๋ฉด 3์„,
    3์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ˆซ์ž๋ผ๋ฉด
    0์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜๋Š” ๋ชจ๋ธ์„ ์ƒ๊ฐ
# target์ธ digits_label์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ด์ง ๋ณ€ํ˜•
#  label์ธ digits_label์—์„œ ์ˆซ์ž๊ฐ€ 3์ด๋ผ๋ฉด ๊ทธ๋Œ€๋กœ 3์„, ์•„๋‹ˆ๋ผ๋ฉด 0์„ ๊ฐ€์ง€๋Š” new_label
new_label = [3 if i == 3 else 0 for i in digits_label]
new_label[:20]
[0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0]

digits_data์™€ new_label๋กœ Decision Tree ๋ชจ๋ธ์„ ํ•™์Šต, ์ •ํ™•๋„๋ฅผ ํ™•์ธ

  • train_test_split์œผ๋กœ
  • ํ•™์Šต ๋ฐ์ดํ„ฐ์™€
  • ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“  ํ›„,
  • ๋ชจ๋ธ์„ fit ์‹œํ‚ค๊ณ ,
  • predict๋ฅผ ํ†ตํ•ด ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“  ํ›„
  • accuracy_score๋ฅผ ์ด์šฉํ•ด ์ •ํ™•๋„๋ฅผ ์ธก์ •
# ์ฐธ๊ณ  ๋ถ„๊ฝƒ์˜ ์˜ˆ

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(iris_data,      
                                                    # iris_data๋Š” ๋ฌธ์ œ์ง€ :  feature
                                                    iris_label,     
                                                    # ๋งž์ถฐ์•ผ ํ•  ์ •๋‹ต๊ฐ’ : Label(3๊ฐ€์ง€์ค‘)
                                                    test_size=0.2,  
                                                    # test dataset์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆ : ์ „์ฒด์˜ 20%๋งŒ ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉ
                                                    random_state=7) 
                                                    # train ๋ฐ์ดํ„ฐ์™€ test ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌ(split)ํ•˜๋Š”๋ฐ ์ ์šฉ๋˜๋Š” ๋žœ๋ค์„ฑ

print('X_train ๊ฐœ์ˆ˜: ', len(X_train),', X_test ๊ฐœ์ˆ˜: ', len(X_test))
X_train ๊ฐœ์ˆ˜:  120 , X_test ๊ฐœ์ˆ˜:  30
from sklearn.metrics import accuracy_score                    # ์ •ํ™•๋„ ํ™•์ธ ๋ชจ๋“ˆ
from sklearn.model_selection import train_test_split          # 
from sklearn.tree import DecisionTreeClassifier               # Decision Tree Model

X_train, X_test, y_train, y_test = train_test_split(digits_data,     # digits_data ๋ผ๋Š” ๋ฌธ์ œ์ง€
                                                    new_label,       # ๋งž์ถฐ์•ผํ•  ์ •๋‹ถ๊ฐ’
                                                    test_size=0.2,   # ์ „์ฒด์˜ 20%๋งŒ ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉ
                                                    random_state=15) # train ๋ฐ์ดํ„ฐ์™€ test ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌ(split)ํ•˜๋Š”๋ฐ ์ ์šฉ๋˜๋Š” ๋žœ๋ค์„ฑ

decision_tree = DecisionTreeClassifier(random_state=15)       #๋””ํŠธ๋ฆฌ
decision_tree.fit(X_train, y_train)                           #๋””ํŠธ๋ฆฌ๋ชจ๋ธ Fit
y_pred = decision_tree.predict(X_test)                        #๋””ํŠธ๋ฆฌ๋ชจ๋ธ ํ…Œ์ŠคํŠธ๋กœ ์˜ˆ์ธก

accuracy = accuracy_score(y_test, y_pred)                     #์ •ํ™•๋„ ์Šค์ฝ”์–ด ํ…Œ์ŠคํŠธ์™€ ์˜ˆ์ธก๊ฐ’ ๋น„๊ต
accuracy                                                      #๊ฐ’์€
0.9388888888888889
๋ชจ๋ธ์ด ์ „ํ˜€ ํ•™์Šตํ•˜์ง€ ์•Š๊ณ  ์ •๋‹ต์„ ๋ชจ๋‘ 0์œผ๋กœ๋งŒ ์„ ํƒํ•ด๋„ ์ •ํ™•๋„๊ฐ€ 90%๊ฐ€๋Ÿ‰์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ
# ๊ธธ์ด๋Š” y_pred์™€ ๊ฐ™์œผ๋ฉด์„œ 0์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ๋ฅผ fake_pred๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•ด ๋ณด๊ณ , 
# ์ด ๋ฆฌ์ŠคํŠธ์™€ ์‹ค์ œ ์ •๋‹ต์ธ y_test๊ฐ„์˜ ์ •ํ™•๋„๋ฅผ ํ™•์ธ.
fake_pred = [0] * len(y_pred)

accuracy = accuracy_score(y_test, fake_pred)
accuracy
0.925

์ •๋‹ต์„ ๋ชจ๋‘ 0์œผ๋กœ๋งŒ ์„ ํƒํ•ด๋„ ์ •ํ™•๋„๊ฐ€ 90%๊ฐ€๋Ÿ‰์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋Š” ๋ถˆ๊ท ํ˜•ํ•œ ๋ฐ์ดํ„ฐ, unbalanced ๋ฐ์ดํ„ฐ์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ

์ฆ‰ ์ •ํ™•๋„๋Š” ์ •๋‹ต์˜ ๋ถ„ํฌ์— ๋”ฐ๋ผ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์ž˜ ํ‰๊ฐ€ํ•˜์ง€ ๋ชปํ•˜๋Š” ์ฒ™๋„๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ

์ •๋‹ต๊ณผ ์˜ค๋‹ต์—๋„ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค!

์˜ค์ฐจ ํ–‰๋ ฌ(confusion matrix) : ์ •๋‹ต๊ณผ ์˜ค๋‹ต์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

What is Confusion Matrix and Advanced Classification Metrics?

์˜ค์ฐจ ํ–‰๋ ฌ์—์„œ๋Š” ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋„ค ๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„

  • TN(True Negative), FP(False Positive), FN(False Negative), TP(True Positive)

์˜ค์ฐจ ํ–‰๋ ฌ์—์„œ ๋‚˜ํƒ€๋‚˜๋Š” ์„ฑ๋Šฅ ์ง€ํ‘œ๋ฅผ ๋‹ค์„ฏ ๊ฐ€์ง€

  • Precision, Negative Predictive Value, Sensitivity, Specificity, Accuracy

TP, FN, FP, TN์˜ ์ˆ˜์น˜๋กœ ๊ณ„์‚ฐ๋˜๋Š” ์„ฑ๋Šฅ ์ง€ํ‘œ

  • ์ •๋ฐ€๋„(Precision)
    • ๋†’์„์ˆ˜๋ก ์ข‹์Œ
    • TP / TP +FP
    • ํ‹€๋ ธ๋Š”๋ฐ ๋งž๋‹ค๊ณ  ์˜ˆ์ธกํ•˜๋ฉด ์•ˆ๋จ(Type 1 Error:FP:Precision)
  • ์žฌํ˜„์œจ(Recall, Sensitivity)
    • ๋†’์„์ˆ˜๋ก ์ข‹์Œ
    • ๋งž๋Š”๋ฐ ํ‹€๋ ธ๋‹ค๊ณ  ์˜ˆ์ธกํ•˜๋ฉด ์•ˆ๋จ(type 2 Error:FN)
  • F1 ์Šค์ฝ”์–ด(f1 score)

Q17. ์ „์ฒด ๋ฉ”์ผํ•จ์—์„œ ์ŠคํŒธ ๋ฉ”์ผ์„ ๊ฑฐ๋ฅด๋Š” ๋ชจ๋ธ์—๊ฒŒ๋Š”
Precision์ด ๋” ์ค‘์š”ํ• ๊นŒ์š”, Recall์ด ๋” ์ค‘์š”ํ• ๊นŒ์š”?
(์ŠคํŒธ ๋ฉ”์ผ์„ positive, ์ •์ƒ ๋ฉ”์ผ์„ negative๋กœ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค)

์˜ˆ์‹œ๋‹ต์•ˆ

๋ฉ”์ผ ์ฒ˜๋ฆฌ ๋ชจ๋ธ์€ ์ŠคํŒธ ๋ฉ”์ผ์„ ๋ชป ๊ฑฐ๋ฅด๋Š” ๊ฒƒ์€ ๊ดœ์ฐฎ์ง€๋งŒ,
์ •์ƒ ๋ฉ”์ผ์„ ์ŠคํŒธ ๋ฉ”์ผ๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ฒƒ์€ ๋” ํฐ ๋ฌธ์ œ์ด๋‹ค.
์ฆ‰ ์Œ์„ฑ์„ ์–‘์„ฑ์œผ๋กœ ํŒ๋‹จํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.
๋”ฐ๋ผ์„œ Precision์ด ๋” ์ค‘์š”ํ•˜๋‹ค.

์‹ค์ œ(Actual Class)์—์„œ ๋ญ๊ฐ€ ๋” ์‹ฌ๊ฐํ•˜๋ƒ. ๋ฌธ์ œ๊ฐ€ ํฌ๋ƒ. ๋ญ๊ฐ€ ์ค‘์š”ํ•˜๋ƒ

  • ์‹ค์ œ ํฌ์ง€ํ‹ฐ๋ธŒ๋ฅผ ์ž˜๋ชป ํŒ๋‹จํ•œ๊ฒŒ ์ค‘์š” -> ํฌ์ง€ํ‹ฐ๋ธŒ๊ฐ€ ์ค‘์š” FN Recall
  • ์‹ค์ œ ๋„ค๊ฑฐํ‹ฐ๋ธŒ๋ฅผ ์ž˜๋ชป ํŒ๋‹จํ•œ๊ฒŒ ์ค‘์š” -> ๋„ค๊ฑฐํ‹ฐ๋ธŒ๊ฐ€ ์ค‘์š” FP Precision

==>>>์ •์ƒ์ด ์ค‘์š”

scikit-learn์œผ๋กœ ์ด ์ง€ํ‘œ๋“ค์„ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•

์˜ค์ฐจ ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด sklearn.metrics ํŒจํ‚ค์ง€ ๋‚ด์˜ confusion_matrix๋กœ ํ™•์ธ

from sklearn.metrics import confusion_matrix

confusion_matrix(y_test, y_pred)
array([[320,  13],
       [  9,  18]])

์™ผ์ชฝ ์œ„๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ TPTP, FNFN, FPFP, TNTN์˜ ๊ฐœ์ˆ˜

#  ๋ชจ๋“  ์ˆซ์ž๋ฅผ 0์œผ๋กœ ์˜ˆ์ธกํ•œ fake_pred์˜ ๊ฒฝ์šฐ
confusion_matrix(y_test, fake_pred)
array([[333,   0],
       [ 27,   0]])

์†๊ธ€์”จ ๊ฒฐ๊ณผ์˜ Precision, Recall, F1 score๋Š”
sklearn.metrics์˜ classification_report๋ฅผ ํ™œ์šฉ.

from sklearn.metrics import classification_report

print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       0.97      0.96      0.97       333
           3       0.58      0.67      0.62        27

    accuracy                           0.94       360
   macro avg       0.78      0.81      0.79       360
weighted avg       0.94      0.94      0.94       360
#  fake_pred์˜ ๊ฒฝ์šฐ

print(classification_report(y_test, fake_pred, zero_division=0))
              precision    recall  f1-score   support

           0       0.93      1.00      0.96       333
           3       0.00      0.00      0.00        27

    accuracy                           0.93       360
   macro avg       0.46      0.50      0.48       360
weighted avg       0.86      0.93      0.89       360

label์ด ๋ถˆ๊ท ํ˜•ํ•˜๊ฒŒ ๋ถ„ํฌ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” ๋” ์กฐ์‹ฌ

์ •ํ™•๋„๋งŒ์œผ๋กœ ํŒ๋‹จํ•˜์ง€ ๋ง๊ฒƒ.

๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ฌ๋ผ๋„ ๋ฌธ์ œ ์—†์–ด์š”!

scikit-learn์˜ ์˜ˆ์ œ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉ

๋ฐ์ดํ„ฐ์…‹ ์†Œ๊ฐœ : ์‚ฌ์ดํ‚ท๋Ÿฐ toy datasets


Toy Dataset ์ค‘ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ์ ํ•ฉํ•œ ๋ฐ์ดํ„ฐ์…‹

load_digits : ์†๊ธ€์”จ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ
load_wine : ์™€์ธ ๋ฐ์ดํ„ฐ
load_breast_cancer : ์œ ๋ฐฉ์•” ๋ฐ์ดํ„ฐ

1๋‹จ๊ณ„ : ๋ฐ์ดํ„ฐ ํ™•์ธํ•˜๊ธฐ

#  sklearn ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ datasets ํŒจํ‚ค์ง€ ์•ˆ load_$$$$๋ฅผ import 
#  $$$$ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋”ฉ
from sklearn.datasets import load_wine

print(wine.DESCR) # DESCR์—๋Š” ๋ฐ์ดํ„ฐ์…‹์˜ ์„ค๋ช…
.. _wine_dataset:

Wine recognition dataset
------------------------

**Data Set Characteristics:**

    :Number of Instances: 178 (50 in each of three classes)
    :Number of Attributes: 13 numeric, predictive attributes and the class
    :Attribute Information:
 		- Alcohol
 		- Malic acid
 		- Ash
		- Alcalinity of ash  
 		- Magnesium
		- Total phenols
 		- Flavanoids
 		- Nonflavanoid phenols
 		- Proanthocyanins
		- Color intensity
 		- Hue
 		- OD280/OD315 of diluted wines
 		- Proline

    - class:
            - class_0
            - class_1
            - class_2
		
    :Summary Statistics:
    
    ============================= ==== ===== ======= =====
                                   Min   Max   Mean     SD
    ============================= ==== ===== ======= =====
    Alcohol:                      11.0  14.8    13.0   0.8
    Malic Acid:                   0.74  5.80    2.34  1.12
    Ash:                          1.36  3.23    2.36  0.27
    Alcalinity of Ash:            10.6  30.0    19.5   3.3
    Magnesium:                    70.0 162.0    99.7  14.3
    Total Phenols:                0.98  3.88    2.29  0.63
    Flavanoids:                   0.34  5.08    2.03  1.00
    Nonflavanoid Phenols:         0.13  0.66    0.36  0.12
    Proanthocyanins:              0.41  3.58    1.59  0.57
    Colour Intensity:              1.3  13.0     5.1   2.3
    Hue:                          0.48  1.71    0.96  0.23
    OD280/OD315 of diluted wines: 1.27  4.00    2.61  0.71
    Proline:                       278  1680     746   315
    ============================= ==== ===== ======= =====

    :Missing Attribute Values: None
    :Class Distribution: class_0 (59), class_1 (71), class_2 (48)
    :Creator: R.A. Fisher
    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
    :Date: July, 1988

This is a copy of UCI ML Wine recognition datasets.
https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data

The data is the results of a chemical analysis of wines grown in the same
region in Italy by three different cultivators. There are thirteen different
measurements taken for different constituents found in the three types of
wine.

Original Owners: 

Forina, M. et al, PARVUS - 
An Extendible Package for Data Exploration, Classification and Correlation. 
Institute of Pharmaceutical and Food Analysis and Technologies,
Via Brigata Salerno, 16147 Genoa, Italy.

Citation:

Lichman, M. (2013). UCI Machine Learning Repository
[https://archive.ics.uci.edu/ml]. Irvine, CA: University of California,
School of Information and Computer Science. 

.. topic:: References

  (1) S. Aeberhard, D. Coomans and O. de Vel, 
  Comparison of Classifiers in High Dimensional Settings, 
  Tech. Rep. no. 92-02, (1992), Dept. of Computer Science and Dept. of  
  Mathematics and Statistics, James Cook University of North Queensland. 
  (Also submitted to Technometrics). 

  The data was used with many others for comparing various 
  classifiers. The classes are separable, though only RDA 
  has achieved 100% correct classification. 
  (RDA : 100%, QDA 99.4%, LDA 98.9%, 1NN 96.1% (z-transformed data)) 
  (All results using the leave-one-out technique) 

  (2) S. Aeberhard, D. Coomans and O. de Vel, 
  "THE CLASSIFICATION PERFORMANCE OF RDA" 
  Tech. Rep. no. 92-01, (1992), Dept. of Computer Science and Dept. of 
  Mathematics and Statistics, James Cook University of North Queensland. 
  (Also submitted to Journal of Chemometrics).
#  sklearn ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ datasets ํŒจํ‚ค์ง€ ์•ˆ load_$$$$๋ฅผ import 
#  $$$$ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋”ฉ
from sklearn.datasets import load_breast_cancer

breast_cancer = load_breast_cancer()

print(breast_cancer.DESCR) # DESCR์—๋Š” ๋ฐ์ดํ„ฐ์…‹์˜ ์„ค๋ช…
.. _breast_cancer_dataset:

Breast cancer wisconsin (diagnostic) dataset
--------------------------------------------

**Data Set Characteristics:**

    :Number of Instances: 569

    :Number of Attributes: 30 numeric, predictive attributes and the class

    :Attribute Information:
        - radius (mean of distances from center to points on the perimeter)
        - texture (standard deviation of gray-scale values)
        - perimeter
        - area
        - smoothness (local variation in radius lengths)
        - compactness (perimeter^2 / area - 1.0)
        - concavity (severity of concave portions of the contour)
        - concave points (number of concave portions of the contour)
        - symmetry
        - fractal dimension ("coastline approximation" - 1)

        The mean, standard error, and "worst" or largest (mean of the three
        worst/largest values) of these features were computed for each image,
        resulting in 30 features.  For instance, field 0 is Mean Radius, field
        10 is Radius SE, field 20 is Worst Radius.

        - class:
                - WDBC-Malignant
                - WDBC-Benign

    :Summary Statistics:

    ===================================== ====== ======
                                           Min    Max
    ===================================== ====== ======
    radius (mean):                        6.981  28.11
    texture (mean):                       9.71   39.28
    perimeter (mean):                     43.79  188.5
    area (mean):                          143.5  2501.0
    smoothness (mean):                    0.053  0.163
    compactness (mean):                   0.019  0.345
    concavity (mean):                     0.0    0.427
    concave points (mean):                0.0    0.201
    symmetry (mean):                      0.106  0.304
    fractal dimension (mean):             0.05   0.097
    radius (standard error):              0.112  2.873
    texture (standard error):             0.36   4.885
    perimeter (standard error):           0.757  21.98
    area (standard error):                6.802  542.2
    smoothness (standard error):          0.002  0.031
    compactness (standard error):         0.002  0.135
    concavity (standard error):           0.0    0.396
    concave points (standard error):      0.0    0.053
    symmetry (standard error):            0.008  0.079
    fractal dimension (standard error):   0.001  0.03
    radius (worst):                       7.93   36.04
    texture (worst):                      12.02  49.54
    perimeter (worst):                    50.41  251.2
    area (worst):                         185.2  4254.0
    smoothness (worst):                   0.071  0.223
    compactness (worst):                  0.027  1.058
    concavity (worst):                    0.0    1.252
    concave points (worst):               0.0    0.291
    symmetry (worst):                     0.156  0.664
    fractal dimension (worst):            0.055  0.208
    ===================================== ====== ======

    :Missing Attribute Values: None

    :Class Distribution: 212 - Malignant, 357 - Benign

    :Creator:  Dr. William H. Wolberg, W. Nick Street, Olvi L. Mangasarian

    :Donor: Nick Street

    :Date: November, 1995

This is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets.
https://goo.gl/U2Uwz2

Features are computed from a digitized image of a fine needle
aspirate (FNA) of a breast mass.  They describe
characteristics of the cell nuclei present in the image.

Separating plane described above was obtained using
Multisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree
Construction Via Linear Programming." Proceedings of the 4th
Midwest Artificial Intelligence and Cognitive Science Society,
pp. 97-101, 1992], a classification method which uses linear
programming to construct a decision tree.  Relevant features
were selected using an exhaustive search in the space of 1-4
features and 1-3 separating planes.

The actual linear program used to obtain the separating plane
in the 3-dimensional space is that described in:
[K. P. Bennett and O. L. Mangasarian: "Robust Linear
Programming Discrimination of Two Linearly Inseparable Sets",
Optimization Methods and Software 1, 1992, 23-34].

This database is also available through the UW CS ftp server:

ftp ftp.cs.wisc.edu
cd math-prog/cpo-dataset/machine-learn/WDBC/

.. topic:: References

   - W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction 
     for breast tumor diagnosis. IS&T/SPIE 1993 International Symposium on 
     Electronic Imaging: Science and Technology, volume 1905, pages 861-870,
     San Jose, CA, 1993.
   - O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and 
     prognosis via linear programming. Operations Research, 43(4), pages 570-577, 
     July-August 1995.
   - W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques
     to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) 
     163-171.

ํ”„๋กœ์ ํŠธ

๋‹จ๊ณ„๋ณ„ ์ง„ํ–‰๋ฐฉ๋ฒ•

ํ•„์š”ํ•œ ๋ชจ๋“ˆ Import

from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

๋ฐ์ดํ„ฐ ํ™•์ธํ•˜๊ธฐ

from sklearn.datasets import load_digits

print(digits.DESCR)
.. _digits_dataset:

Optical recognition of handwritten digits dataset
--------------------------------------------------

**Data Set Characteristics:**

    :Number of Instances: 1797
    :Number of Attributes: 64
    :Attribute Information: 8x8 image of integer pixels in the range 0..16.
    :Missing Attribute Values: None
    :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)
    :Date: July; 1998

This is a copy of the test set of the UCI ML hand-written digits datasets
https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits

The data set contains images of hand-written digits: 10 classes where
each class refers to a digit.

Preprocessing programs made available by NIST were used to extract
normalized bitmaps of handwritten digits from a preprinted form. From a
total of 43 people, 30 contributed to the training set and different 13
to the test set. 32x32 bitmaps are divided into nonoverlapping blocks of
4x4 and the number of on pixels are counted in each block. This generates
an input matrix of 8x8 where each element is an integer in the range
0..16. This reduces dimensionality and gives invariance to small
distortions.

For info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.
T. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.
L. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,
1994.

.. topic:: References

  - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their
    Applications to Handwritten Digit Recognition, MSc Thesis, Institute of
    Graduate Studies in Science and Engineering, Bogazici University.
  - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.
  - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.
    Linear dimensionalityreduction using relevance weighted LDA. School of
    Electrical and Electronic Engineering Nanyang Technological University.
    2005.
  - Claudio Gentile. A New Approximate Maximal Margin Classification
    Algorithm. NIPS. 2000.

๋ฐ์ดํ„ฐ ์ค€๋น„

load_digits ๋ฉ”์„œ๋“œ

digits = load_digits()

๋ฐ์ดํ„ฐ ์ดํ•ดํ•˜๊ธฐ

  • Feature Data ์ง€์ •ํ•˜๊ธฐ
  • Label Data ์ง€์ •ํ•˜๊ธฐ
  • Target Names ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ
  • ๋ฐ์ดํ„ฐ Describe ํ•ด ๋ณด๊ธฐ

train, test ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ

  • ๋ชจ๋ธ ํ•™์Šต๊ณผ ํ…Œ์ŠคํŠธ์šฉ ๋ฌธ์ œ์ง€์™€ ์ •๋‹ต์ง€
  • X_train, X_test, y_train, y_test๋ฅผ ์ƒ์„ฑ

๋‹ค์–‘ํ•œ ๋ชจ๋ธ๋กœ ํ•™์Šต

  • Decision Tree ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
  • Random Forest ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
  • SVM ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
  • SGD Classifier ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
  • Logistic Regression ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ

๋ชจ๋ธ์„ ํ‰๊ฐ€.

sklearn.metrics ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‰๊ฐ€์ง€ํ‘œ

load_digits : ์†๊ธ€์”จ๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ด…์‹œ๋‹ค

load_wine : ์™€์ธ์„ ๋ถ„๋ฅ˜ํ•ด ๋ด…์‹œ๋‹ค

load_breast_cancer : ์œ ๋ฐฉ์•” ์—ฌ๋ถ€๋ฅผ ์ง„๋‹จํ•ด ๋ด…์‹œ๋‹ค

ํ”„๋กœ์ ํŠธ ์ œ์ถœ

profile
๋งˆ์ผ€ํŒ…์„ ์œ„ํ•œ ์ธ๊ณต์ง€๋Šฅ ์„ค๊ณ„์™€ ์Šคํƒ€ํŠธ์—… Log

0๊ฐœ์˜ ๋Œ“๊ธ€