from sklearn import datasets
d=datasets.load_iris()
for i in range(0,len(d.data)):
print(i+1, d.data[i], d.target[i])
샘플 [특징 벡터] 레이블
1 [5.1 3.5 1.4 0.2] 0
2 [4.9 3. 1.4 0.2] 0
3 [4.7 3.2 1.3 0.2] 0
4 [4.6 3.1 1.5 0.2] 0
5 [5. 3.6 1.4 0.2] 0
6 [5.4 3.9 1.7 0.4] 0
7 [4.6 3.4 1.4 0.3] 0
8 [5. 3.4 1.5 0.2] 0
9 [4.4 2.9 1.4 0.2] 0
10 [4.9 3.1 1.5 0.1] 0
11 [5.4 3.7 1.5 0.2] 0
12 [4.8 3.4 1.6 0.2] 0
13 [4.8 3. 1.4 0.1] 0
14 [4.3 3. 1.1 0.1] 0
15 [5.8 4. 1.2 0.2] 0
16 [5.7 4.4 1.5 0.4] 0
17 [5.4 3.9 1.3 0.4] 0
18 [5.1 3.5 1.4 0.3] 0
19 [5.7 3.8 1.7 0.3] 0
20 [5.1 3.8 1.5 0.3] 0
21 [5.4 3.4 1.7 0.2] 0
22 [5.1 3.7 1.5 0.4] 0
23 [4.6 3.6 1. 0.2] 0
24 [5.1 3.3 1.7 0.5] 0
25 [4.8 3.4 1.9 0.2] 0
26 [5. 3. 1.6 0.2] 0
27 [5. 3.4 1.6 0.4] 0
28 [5.2 3.5 1.5 0.2] 0
29 [5.2 3.4 1.4 0.2] 0
…
from sklearn import datasets
from sklearn import svm #sklearn 라이브러리가 제공하는 svm 클래스 import
d=datasets.load_iris()
s = svm.SVC(gamma=0.1, C=10) #svm 클래스가 제공하는 분류 모델 SVC를 생성해 객체 s에 저장
s.fit(d.data, d.target) #fit 함수를 사용해 모델을 학습
new_d=[[6.5, 3.2, 6.0, 2.5], [7.1, 3.1, 4.7, 1.35]] #예측하기위한 새로운 데이터 테스트 집합
res=s.predict(new_d) #예측
print("새로운 2개 샘플의 부류는", res)
새로운 2개 샘플의 부류는 [2 1]
gamma
는 결정 경계의 유연성을 조절하고, C
는 오분류에 대한 페널티를 조절
원리: 새로운 특징 공간 내에서 선형 모델을 이용해 분류
과적합: 과다하게 복잡한 결정 경계
(둘 다 비슷한 얘기 아닌가?)
import plotly.express as px #Plotly Express를 px로 import. Plotly Express는 데이터 시각화 인터페이스
import plotly.offline as offline #오프라인 파일로 저장하는 인터페이스
df = px.data.iris() # iris 데이터셋을 df 변수에 할당
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='species')
#x, y, z는 각각 x축, y축, z축에 해당하는 열의 이름을 지정하고,
#color는 데이터 포인트를 색으로 구분할 열의 이름을 지정.
#열의 이름은(sepal_length), (sepal_width), (petal_width)을 사용하며, 종(species)에 따라 색으로 구분
offline.plot(fig, filename='scatter_plot.html') #오프라인 파일로 저장
데이터 수집 → 특징 추출 → 모델링 → 예측
TP: 긍정을 긍정으로 예측(참 긍정)
FN: 긍정을 부정으로 잘 못 예측(거짓 부정)
FP: 부정을 긍정으로 잘 못 예측(거짓 긍정)
TN: 부정을 부정으로 예측(참 부정)
그라운드 트루스 | |||
---|---|---|---|
긍정 | 부정 | ||
예측값 | 긍정 | TP(참 긍정) | FP(거짓 긍정) |
부정 | FN(거짓 부정) | TN(참 부정) |
일반화: 새로운 데이터로 성능을 측정하는 일
from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import train_test_split
import numpy as np
# 데이터셋을 읽고 훈련 집합과 테스트 집합으로 분할
digit = datasets.load_digits()
x_train, x_test, y_train, y_test = train_test_split(digit.data, digit.target, train_size=0.6)
#특징 벡터, 레이블, 훈련집합 0.6, 테스트집합 0.4
#svm의 분류 모델 SVC를 학습
s=svm.SVC(gamma=0.001)
s.fit(x_train, y_train)
res = s.predict(x_test)
#혼동 행렬 구함
conf=np.zeros((10,10))
for i in range(len(res)):
conf[res[i]][y_test[i]]+=1
print(conf)
#정확률 측정하고 출력
no_correct = 0
for i in range(10):
no_correct += conf[i][i]
accuracy = no_correct/len(res)
print("테스트 집합에 대한 정확률은", accuracy*100, "%입니다.")
[[64. 0. 0. 0. 0. 0. 0. 0. 0. 0.][ 0. 75. 0. 0. 0. 0. 0. 0. 1. 0.]
[ 0. 0. 61. 0. 0. 0. 0. 0. 0. 0.][ 0. 0. 0. 69. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 74. 0. 0. 0. 0. 0.][ 0. 0. 0. 1. 0. 77. 0. 0. 0. 1.]
[ 0. 0. 0. 0. 0. 1. 82. 0. 0. 0.][ 0. 0. 1. 1. 0. 0. 0. 66. 0. 1.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 73. 0.][ 0. 0. 0. 0. 0. 1. 0. 0. 0. 70.]]
테스트 집합에 대한 정확률은 98.88734353268428 %입니다.
출력 결과
from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import cross_val_score
import numpy as np
# 데이터셋을 읽고 5겹 교차 검증
digit = datasets.load_digits()
s=svm.SVC(gamma=0.001)
accuracies = cross_val_score(s, digit.data, digit.target, cv=5)
print(accuracies)
print("정확률(평균)=%0.3f, 표준편차=%0.3f"%(accuracies.mean()*100, accuracies.std()))
[0.975 0.95 0.98328691 0.99164345 0.96100279]
정확률(평균)=97.219, 표준편차=0.015
⇒신뢰도 향상
from sklearn.linear_model import Perceptron
#훈련 집합 구축
X= [[0,0], [0,1], [1,0], [1,1]]
y= [-1, 1, 1, 1]
#fit 함수로 Perceptron 학습
p=Perceptron()
p.fit(X,y)
print("학습된 퍼셉트론의 매개변수: ",p.coef_, p.intercept_) #가중치w1, w2 / 가중치w0
print("훈련집합에 대한 예측: ", p.predict(X)) #예측값
print("정확률 측정: ", p.score(X,y)*100,"%")
학습된 퍼셉트론의 매개변수: [[2. 2.]][-1.]
훈련집합에 대한 예측: [-1 1 1 1]
정확률 측정: 100.0 %
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
import numpy as np
#데이터셋을 읽고 훈련 집합과 테스트 집합으로 분할
digit = datasets.load_digits()
x_train, x_test, y_train, y_test = train_test_split(digit.data, digit.target, train_size=0.6)
#MLP 분류기 모델을 학습
mlp = MLPClassifier(hidden_layer_sizes=(100), learning_rate_init=0.001, batch_size=32, max_iter=300, solver='sgd', verbose=True)
mlp.fit(x_train, y_train)
res = mlp.predict(x_test) #테스트 집합으로 예측
#혼동 행렬
conf = np.zeros((10, 10))
for i in range(len(res)):
conf[res[i]][y_test[i]]+=1
print(conf)
#정확률 계산
no_correct = 0
for i in range(10):
no_correct+=conf[i][i]
accuracy = no_correct/len(res)
print("테스트 집합에 대한 정확률은 ", accuracy*100, "%입니다.")
hidden_layer_sizes=(100)
: 노드가 100개인 은닉층 하나
learning_rate_init=0.001
: 학습률의 초기값. 학습률은 가중치 업데이트 시 사용되는 상수.
batch_size=32
: 미니 배치 경사 하강법에서 한 번의 가중치 업데이트를 위해 사용되는 샘플의 수.
max_iter=300
: 최대 반복 횟수
solver='sgd'
: 최적화 알고리즘으로 확률적 경사 하강법을 나타내는 'sgd'를 선택함.
verbose=True
: 학습 과정 중에 진행 상황을 출력할지 여부를 나타냄. True로 설정하면 학습 중 출력이 표시.
Iteration 1, loss = 2.32213324
Iteration 2, loss = 0.32505309
Iteration 3, loss = 0.19824941
Iteration 4, loss = 0.14842346
Iteration 5, loss = 0.11559600
Iteration 6, loss = 0.11005246
Iteration 7, loss = 0.08608624
Iteration 8, loss = 0.07677681
Iteration 9, loss = 0.06741344
Iteration 10, loss = 0.05846336
Iteration 11, loss = 0.05638974
Iteration 12, loss = 0.05124250
…
Iteration 88, loss = 0.00553884
Iteration 89, loss = 0.00548285
Iteration 90, loss = 0.00540366
Iteration 91, loss = 0.00532382
Iteration 92, loss = 0.00524335
Iteration 93, loss = 0.00517307
Iteration 94, loss = 0.00514018
Training loss did not improve more than tol=0.000100 for 10 consecutive epochs. Stopping.
[[68. 0. 0. 0. 0. 0. 1. 0. 0. 0.][ 0. 89. 1. 0. 0. 0. 0. 0. 3. 0.]
[ 0. 0. 72. 0. 0. 0. 0. 0. 0. 0.][ 0. 0. 0. 78. 0. 0. 0. 0. 1. 1.]
[ 0. 0. 0. 0. 75. 1. 0. 0. 0. 0.][ 0. 0. 0. 2. 0. 63. 0. 0. 0. 1.]
[ 0. 1. 0. 0. 0. 0. 69. 0. 0. 0.][ 0. 0. 0. 0. 1. 0. 0. 65. 0. 0.]
[ 0. 0. 0. 1. 0. 0. 0. 0. 55. 1.][ 0. 0. 0. 0. 0. 1. 0. 1. 0. 68.]]
테스트 집합에 대한 정확률은 97.63560500695411 %입니다.
tol = 0.0001
’과 ‘n_iter_no_change=10
’으로 인해 멈추었다.