본문 바로가기
Python/머신러닝, 딥러닝

scikit-learn(사이킷 런) 활용하여 정답률 확인하기

by IT두잇 2022. 4. 28.

데이터셋으로 제공되는 wine 데이터를 활용해 결과를 검증해보려고 합니다.

 

필요한 라이브러리 설치

import pandas as pd
from sklearn import svm, metrics
from sklearn.model_selection import train_test_split
from sklearn import datasets
cs

 

wine 데이터셋 불러오기

wine = datasets.load_wine()
pd.DataFrame(wine)
cs

 


검증 과정 진행

1) wine의 샘플 데이터를 data에, 정답 데이터를 target에 데이터 프레임으로 저장

data = pd.DataFrame(wine['data'])
target = pd.DataFrame(wine['target'])
cs

 

2) data와 target을 train_data, test_data, train_label, test_label로 나누어 봅시다.

    (train에는 128 row, test에는 50 row씩 저장)

 

# train_test_split  사용할 때

train_data, test_data, train_label, test_label = train_test_split(
    data, target, test_size = 50)
 
cs

 

# iloc 사용할 때 (판다스)

train_data = data.iloc[:128, ]
test_data = data.iloc[128:178, ]
train_label = target.iloc[:128, ]
train_label = target.iloc[128:178, ]
cs

 

# iloc 사용할 때 (넘파이)

train_data = data.iloc[:128, ].values
test_data = data.iloc[128:178, ].values
train_label = target.iloc[:128, ].values
train_label = target.iloc[128:178, ].values
cs
 

3) 나눈 데이터를 svm을 이용하여 정답률을 구해봅시다.

* svm(support vector machine) : 매개변수를 조정해서 요소들을 구분하는 선을 짓고, 이를 기반으로 패턴을 인식하는 방법

#모델 선택
clf = svm.SVC()
 
#학습하기
clf.fit(train_data, train_label)
 
#예측하기
predict = clf.predict(test_data)
 
#평가하기
# 몇개를 맞추었습니까?
ac_score = metrics.accuracy_score(test_label, predict)
cl_report = metrics.classification_report(test_label, predict)
print("정답률 =", ac_score)
print("리포트 =\n", cl_report)
 
cs

 

 

결과값: