수업 복습/머신러닝과 딥러닝
아이리스 데이터셋(Iris DataSet)
김복칠
2023. 12. 29. 15:48
1. Iris DataSet
- 데이터셋 : 특정한 작업을 위해 데이터를 관련성 있게 모아놓은 것
from sklearn.datasets import load_iris
iris = load_iris()
- 먼저 아이리스 데이터를 불러옵니다
- 아이리스 데이터는 사이킷런에서 제공하는 정제가 잘 되어있고 선형회기랑 가장 잘 어울리는 샘플데이터입니다
print(iris['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 ...
- 아이리스 데이터에 대한 내용을 확인합니다
import pandas as pd
df_iris = pd.DataFrame(data, columns = feature_names)
df_iris.head()
- 이후 아이리스 데이터를 데이터 프레임 형태로 변환해주기 위해 pandas를 import 해줍니다
from sklearn.model_selection import train_test_split
target = iris['target']
df_iris['target'] = target
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 = 2023)
- 먼저 학습을 위해 종속변수로 활용할 target 컬럼을 생성해줍니다
- 그리고 교육을 위해 target 컬럼을 제외한 독립변수와 종속변수, test_size(비율, 4:1), random_state(임의의 값 2023)을 입력해주고 학습을 시켜줍니다
X_train.shape, X_test.shape
((120, 4), (30, 4))
y_train.shape, y_test.shape
((120,), (30,))
- 학습을 시킬 데이터의 값과 학습을 시킨 후 결과의 값을 출력해보면 설정한 비율로 추출된 것을 볼수 있습니다
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
svc = SVC()
svc.fit(X_train, y_train)
y_pred = svc.predict(X_test)
- 모의고사가 끝났으면 결과값이 동일하게 나오는지 확인해 줍니다
- 사이킷런의 계산 모듈들을 import 해주고 fit 함수로 train 값들을 학습해줍니다
- 이후 predict 함수를 통해 정답을 계산해 y_pred에 넣어주고 accuracy_score로 정답률을 확인해줍니다