반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

sklearn.SVM. C and gamma 변화 관찰 본문

[AI]/python.sklearn

sklearn.SVM. C and gamma 변화 관찰

givemebro 2020. 4. 17. 14:06
반응형
import numpy as np
import matplotlib.pyplot as plt

 

from sklearn.datasets import load_breast_cancer
cancer=load_breast_cancer()

col1=0
col2=5
X=cancer.data[:,[col1,col2]]
y=cancer.target
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y)

X_mean=X_train.mean(axis=0)
X_std=X_train.std(axis=0)

X_train_norm=(X_train-X_mean)/X_std
X_test_norm=(X_test-X_mean)/X_std

 

from sklearn.svm import SVC
model=SVC(C=1,gamma=1,probability=True)

model.fit(X_train_norm,y_train)

train_score=model.score(X_train_norm,y_train)
test_score=model.score(X_test_norm,y_test)
display(train_score,test_score)

 

Cs=[10**i for i in range(-3,3)]
gammas=[10**i for i in range(-3,3)]
s_train=[]
s_test=[]
for C in Cs:
    s1=[]
    s2=[]

    for gamma in gammas:
        model=SVC(C=C,gamma=gamma)
        model.fit(X_train_norm,y_train)
        
        pred_y=model.predict(X_test_norm)
        s1.append(model.score(X_train_norm,y_train))
        s2.append(model.score(X_test_norm,y_test))
    s_train.append(s1)
    s_test.append(s2)
    
fig=plt.figure(figsize=[12,8])
for i in range(len(Cs)):
    plt.subplot(1,len(Cs),1+i)
    plt.plot(s_train[i],'gs--',label='train')
    plt.plot(s_test[i],'ro-',label='test')
    plt.title('C=%f'%(Cs[i]))
    plt.xticks(range(len(gammas)),gammas)
    plt.ylim(0,1)
    plt.xlabel('gamma')
    plt.ylabel('score')
    plt.legend(loc='lower right')

 

 

 

plt.imshow(s_test,vmin=0.5,vmax=1,origin='lower',cmap='Reds')
plt.xticks(range(len(gammas)),gammas)
plt.yticks(range(len(Cs)),Cs)
plt.ylabel('C')
plt.xlabel('gamma')
plt.colorbar()

 

반응형
Comments