DAY51. Python Classification (1)Classification (๋ฌธ์ ๋ถ๋ฅ)
kNN
์๋ ค์ง ๋ฒ์ฃผ๋ก ์๋ ค์ง์ง ์์ ๋ฒ์ฃผ ๋ถ๋ฅ (ํด์ ์ฉ์ด)
๊ธฐ์กด์ ๋ฒ์ฃผ๊ฐ ์กด์ฌํด์ผ ํจ - ์๋ฃํ(๊ณผ์ผ, ์ฑ์, ๋จ๋ฐฑ์ง ๋ฑ)
ํ์ตํ์ง ์์ : ๊ฒ์ผ๋ฅธ ํ์ต
๊ฒฐ์ธก์น(NA)/์ด์์น ์ ์ฒ๋ฆฌ ์ค์
๋ง์ ํน์ง์ ๊ฐ๋ ๋ฐ์ดํฐ ์
์ ๋ถ์ ํฉ
์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ๊ณ์ฐ์ ์ด์ฉ : ๊ฐ์ฅ ์ ์ฌํ ๋ฒ์ฃผ๋ฅผ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ฑฐ๋ฆฌ๋ก ์ ํ
from sklearn.neighbors import KNeighborsClassifier #kNN ๋ถ๋ฅ๊ธฐ
1. dataset ์์ฑ
grape = [8, 5] #๊ณผ์ผ - 0
fish = [2, 3] #๋จ๋ฐฑ์ง - 1
carrot = [7, 10] #์ฑ์ - 2
orange = [7, 3] #๊ณผ์ผ - 0
celery = [3, 8] #์ฑ์ - 2
cheese = [1, 1] #๋จ๋ฐฑ์ง - 1
know_group = [grape,fish,carrot,orange,celery,cheese] #x๋ณ์
y_class = [0, 1, 2, 0, 2, 1] #๋ถ๋ฅ ํด๋์ค(y๋ณ์)
class_label= ['๊ณผ์ผ', '๋จ๋ฐฑ์ง', '์ฑ์'] #๋ ์ด๋ธ
2. kNN ๋ถ๋ฅ๊ธฐ
knn = KNeighborsClassifier(n_neighbors=3) #k=3 : ์ต๊ทผ์ ์ด์
model = knn.fit(X = know_group, y=y_class)
3. kNN ๋ถ๋ฅ๊ธฐ
x1 = int(input('๋จ๋ง(1~10) : '))
x2 = int(input('์์ญ๊ฑฐ๋ฆผ(1~10) : '))
test_X = [[x1, x2]] # ์๋ ค์ง์ง ์์ ์ง๋จ
class ์์ธก
y_pred = model.predict(X = test_X)
print(y_pred) # [2]
print('๋ถ๋ฅ๊ฒฐ๊ณผ : ', class_label[y_pred[0]])
5, 9 -> ์ฑ์
8, 5 -> ๊ณผ์ผ
2, 3 -> ๋จ๋ฐฑ์ง
NB (Naive Bayes)
ํต๊ณ์ ๋ถ๋ฅ๊ธฐ : ์กฐ๊ฑด๋ถ ํ๋ฅ . ์ฃผ์ด์ง ๋ฐ์ดํฐ๊ฐ ํน์ ํด๋์ค์ ์ํ๋์ง๋ฅผ ํ๋ฅ ์ ํตํด์ ์์ธก
* ์กฐ๊ฑด๋ถ ํ๋ฅ : ์ฌ๊ฑด A๊ฐ ๋ฐ์ํ๋ค๋ ์ ์ ํ์์ ๋ค๋ฅธ ์ฌ๊ฑด B๊ฐ ๋ฐ์ํ ํ๋ฅ
๋ฒ ์ด์ฆ ํ๋ฅ ์ด๋ก (Bayes’ theorem)์ ์ ์ฉํ ๊ธฐ๊ณํ์ต ๋ฐฉ๋ฒ
* ๋ฒ ์ด์ฆ ํ๋ฅ ์ด๋ก : ๊ณผ๊ฑฐ์ ๊ฒฝํ(์ฌ๊ฑด B)๊ณผ ํ์ฌ์ ์ฆ๊ฑฐ(์ฌ๊ฑด A)๋ฅผ ํ ๋๋ก ์ด ๋ค ์ฌ๊ฑด์ ํ๋ฅ ์ ์์ธกํ๋ ์ด๋ก
ํน์ ์์ญ์์๋ DT๋ kNN ๋ถ๋ฅ๊ธฐ ๋ณด๋ค ์ฑ๋ฅ์ด ์ฐ์
ํ ์คํธ ๋ฐ์ดํฐ ์ฒ๋ผ ํฌ์ํ ๊ณ ์ฐจ์์ธ ๊ฒฝ์ฐ ๋์ ์ ํ๋์ ์๋ ์ ๊ณต
GaussianNB : x๋ณ์๊ฐ ์ฐ์ํ, ์ ๊ท๋ถํฌ์ธ ๊ฒฝ์ฐ ์ ์ฉ
MultinomialNB : ๊ณ ์ฐจ์์ ํ
์คํธ ๋ถ๋ฅ(tf-idf)์ ์ ์ฉ(Sparse matrix)
<์กฐ๊ฑด๋ถ ํ๋ฅ >
์ฌ๊ฑดA ๋ฐ์ํ๋ฅ -> ์ฌ๊ฑดB ๋ฐ์ ์ํฅ
P(B|A) = P(A^B)/P(A) = P(A|B)*p(B)/P(A) : ํ๋ฅ ๊ณฑ์
๋ฒ์น
์ฌ์ ํ๋ฅ : P(A), P(B)
๊ฒฐํฉํ๋ฅ : P(A|B)
ex) ๋ ์จ์ ๋น ๊ด๊ณ
yes no tot
๋ง์๋ 2 8 10
ํ๋ฆฐ๋ 6 4 10
tot 8 12 20
1. ์ฌ์ ํ๋ฅ : ํ๋ฅ ์คํ ์ด์ ๋ถํฐ ์๊ณ ์๋ ํ๋ฅ
1) ๋น๊ฐ ์จ ํ๋ฅ : 8/20 = 0.4
p_yes = 8/20 #0.4
2) ๋น๊ฐ ์ ์จ ํ๋ฅ
p_no = 12/20 #0.6
-> ๋น ์จ ํ๋ฅ ๊ณผ ์ ์จ ํ๋ฅ ์ ๋
๋ฆฝ์ฌ๊ฑด
2. ์กฐ๊ฑด๋ถ ํ๋ฅ : P(B|A) = P(A^B)/P(A) = P(A|B)*p(B)/P(A)
ex) ๋ง์ ๋ (A) ๋น๊ฐ ์จ(B) ํ๋ฅ
P(yes|๋ง์ ๋ ) = P(๋ง์ ๋ |yes) * P(yes) / P(๋ง์ ๋ )
p = (2/8) * (8/20) / (10/20)
p #0.2 -> 20%
ex2) ํ๋ฆฐ ๋ (A) ๋น๊ฐ ์จ(B) ํ๋ฅ
P(yes|ํ๋ฆฐ ๋ ) = P(ํ๋ฆฐ ๋ |yes) * P(yes) / P(ํ๋ฆฐ ๋ )
p2 = (6/8) * (8/20) / (10/20)
p2 #0.60 -> 60%
from sklearn.naive_bayes import GaussianNB #model
from sklearn.datasets import load_iris #dataset
from sklearn.model_selection import train_test_split #split
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report #ํผ๋ํ๋ ฌ, ๋ถ๋ฅ์ ํ๋, y๊ฐ ๋ถ๊ท ํ์ผ ๊ฒฝ์ฐ f1 score๊น์ง ๋ชจ๋ธ ํ๊ฐ
y๋ณ์ ๋คํญ๋ถ๋ฅ๊ธฐ
1. dataset load
X, y = load_iris(return_X_y = True)
X #numpyํ์, 2์ฐจ์, ์ฐ์ํ ๋ณ์
y
2. train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = 0.3, random_state=123)
3. nb model ์์ฑ
model = GaussianNB().fit(X=X_train, y=y_train)
4. model ํ๊ฐ
y_pred = model.predict(X = X_test) #class๋จ์๋ก y์ ์์ธก์น ๊ตฌํ๊ธฐ
y_true = y_test
1) confusion_matrix
con_mat = confusion_matrix(y_true, y_pred)
con_mat
array([[18, 0, 0], -> ์์ธก์น์ 0๋ฒ ์ค๋ถ๋ฅ ์์
[ 0, 10, 0], -> ์์ธก์น์ 1๋ฒ ์ค๋ถ๋ฅ ์์
[ 0, 2, 15]], dtype=int64) -> ์์ธก์น์ 2๋ฒ ์ค๋ถ๋ฅ 2๊ฐ
2) accuracy_score
acc = accuracy_score(y_true, y_pred)
acc #0.9555555555555556 ์ ํ๋
3) classification_report
report = classification_report (y_true, y_pred) #๋ถ๋ฅ๊ฒฐ๊ณผ๋ฅผ reportํ์์ผ๋ก ์ ๊ณต
print(report)
precision recall f1-score support
-> precision(์ ํ๋ฅ ) + recall(์ฌํ์จ) = f1-score, support : ์ํ๋ง ๋ ๊ฐฏ์ (45๊ฐ ์ค 18๊ฐ)
0 1.00 1.00 1.00 18 -> 18๊ฐ ์ํ๋ง ์๋ฒฝํ๊ฒ(100%) ๋ถ๋ฅ๋จ
1 0.83 1.00 0.91 10
2 1.00 0.88 0.94 17
-> ์ํ๋ง ๋น๋์๊ฐ ๋ทธ๊ท ํํ๋ฉด ์ฃผ๋ก f1-score(macro avg, weighted avg)๋ก ํ๊ฐ
accuracy 0.96 45 -> 2๋ฒ ํ๊ฐ๋๊ตฌ์ ๋์ผ (๋ฐ์ฌ๋ฆผ๋จ)
macro avg 0.94 0.96 0.95 45 -> ํด๋์ค๋ณ f1-score๋ฅผ ์ต์ข
์ผ๋ก ์ง๊ณ(ํด๋์ค๋ณ f1-score๊ฐ์ ํ๊ท )
weighted avg 0.96 0.96 0.96 45 -> ํด๋์ค๋ณ f1-score๋ฅผ ์ต์ข
์ผ๋ก ์ง๊ณ(ํด๋์ค๋ณ f1-score๊ฐ์ ํ๊ท )
* macro avg : ์ฐ์ ํ๊ท ๊ฐ. (1 + 0.91 + 0.94) / 3 = 0.950
* weighted avg : ๊ฐ class์ ํ๋ณธ์๋ฅผ ๊ฐ์คํ๊ท ๋ธ ๊ฐ
ex) (1.0 * 0.18 + 0.91 * 0.1 + 0.94 * 0.17) / (0.18 + 0.1 + 0.17) = 0.9573333333333333
0.18, 0.1, 0.17 = ๊ฐ์ค์น = ์ํ๋ง ๋ ์
SVM
1. ์ ํ SVM, ๋น์ ํ SVM
2. Hyper parameter : kernel, C, gamma
3. Grid Search : best parameter ์ฐพ๊ธฐ
from sklearn.svm import SVC #SVM model
from sklearn.datasets import load_breast_cancer #datasat
from sklearn.model_selection import train_test_split #dataset split
from sklearn.metrics import accuracy_score #model ํ๊ฐ
1. dataset load
X, y = load_breast_cancer(return_X_y=True)
X.shape # (569, 30)
y #0 or 1
2. train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=123)
3. ๋น์ ํ SVM ๋ชจ๋ธ
help(SVC)
C=1.0 : float, default=1.0 - ๊ฒฐ์ ๊ฒฝ๊ณ ์์น ์กฐ์
kernel='rbf' : ์ปค๋ํธ๋ฆญ ํจ์('linear' or 'rbf')
-> {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'}
gamma='scale' : ๊ฒฐ์ ๊ฒฝ๊ณ ๋ชจ์ ์กฐ์
-> {'scale', 'auto'}
'scale' : 1 / (n_features * X.var())
'auto' : 1 / n_features.
random_state=None : seed๊ฐ
svc = SVC(C=1.0, kernel='rbf', gamma='scale',random_state=123)
model = svc.fit(X=X_train, y=y_train)
model ํ๊ฐ
y_pred = model.predict(X = X_test)
y_true = y_test
acc = accuracy_score(y_true, y_pred)
print('accuracy =', acc)
accuracy = 0.9005847953216374
4. ์ ํ SVM : ์ ํ๋ถ๋ฅ ๊ฐ๋ฅํ ๋ฐ์ดํฐ
svc = SVC(C=1.0, kernel='linear', gamma='scale',random_state=123)
model2 = svc.fit(X=X_train, y=y_train)
model ํ๊ฐ
y_pred = model2.predict(X = X_test)
y_true = y_test
acc2 = accuracy_score(y_true, y_pred)
print('accuracy =', acc2)
accuracy = 0.9707602339181286
Grid Search
์ต์ ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฐพ๋ ๋ฐฉ๋ฒ -> model ํ๋
from sklearn.model_selection import GridSearchCV # best parameter
parmas = {'kernel' : ['rbf', 'linear'],
'C' : [0.01, 0.1, 1.0, 10.0, 100.0],
'gamma' : ['scale', 'auto'] } # dict ์ ์
cv=5 : 5๊ฒน ๊ต์ฐจ๊ฒ์ -> ์ ์ฒด dataset
grid_model = GridSearchCV(model, param_grid=parmas,
scoring='accuracy', cv=5).fit(X=X, y=y)
dir(grid_model)
print('best socre :', grid_model.best_score_)
best socre : 0.9631268436578171
print('best params : ', grid_model.best_params_)
best params : {'C': 100.0, 'gamma': 'scale', 'kernel': 'linear'}
svc = SVC(C=100.0, kernel='linear', gamma='scale',random_state=123)
model = svc.fit(X=X_train, y=y_train)
test_score = model.score(X=X_test, y=y_test)
print(test_score) # 0.9766081871345029