데이터셋으로 제공되는 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 |
결과값:

'Python > 머신러닝, 딥러닝' 카테고리의 다른 글
[Tensorflow] Keras를 통한 딥러닝 (ANN - 인공신경망) (0) | 2022.05.13 |
---|---|
KoNLPy 설치 및 한국어 형태소 라이브러리 비교 (0) | 2022.04.29 |
TensorFlow(텐서플로우) 설치하기 (0) | 2022.04.27 |
[파이썬] 웹 크롤링 후 csv로 저장하기 - Pandas / BeautifulSoup (0) | 2022.04.22 |
[파이썬] 웹 크롤링 - BeautifulSoup 라이브러리 (0) | 2022.04.20 |