In [30]:
# make datas
np.random.seed(0)
X_xor = np.random.randn(200, 2)
y_xor = np.logical_xor(X_xor[:, 0] > 0, X_xor[:, 1] > 0) # 두 조건(X_xor[:, 0] > 0, X_xor[:, 1] > 0)에 대해 xor
y_xor = np.where(y_xor, 1, -1) # y_xor : True = 1, False = -1
In [31]:
# draw graph function
def plot_xor(model, title="", X=X_xor, y=y_xor, xmin=-3, xmax=3, ymin=-3, ymax=3):
XX, YY = np.meshgrid(np.arange(xmin, xmax, (xmax-xmin)/1000), np.arange(ymin, ymax, (ymax-ymin)/1000))
ZZ = np.reshape(model.predict(np.array([XX.ravel(), YY.ravel()]).T), XX.shape)
plt.contourf(XX, YY, ZZ, cmap=mpl.cm.Paired_r, alpha=0.5)
plt.scatter(X[y== 1, 0], X[y== 1, 1], c='b', marker='o', label='+1', s=100)
plt.scatter(X[y==-1, 0], X[y==-1, 1], c='r', marker='s', label='-1', s=100)
plt.scatter(model.support_vectors_[y[model.support_]==1,0],model.support_vectors_[y[model.support_]==1,1],
c="b", marker='o', s=150, linewidth=3, facecolor=None)
plt.scatter(model.support_vectors_[y[model.support_]==-1,0],model.support_vectors_[y[model.support_]==-1,1],
c="r", marker='s', s=150, linewidth=3, facecolor=None)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.title(title)
plt.show()
In [26]:
from sklearn.svm import SVC
for i in range(1,20,5):
gamma = i
C = 100
print("gamma : {0}".format(gamma))
# C : slack valiable : 봐주는 정도 : 높을수록 안 봐줌 # 서포트의 갯수가 줄어든다.
# gamma를 늘리면 서포트의 갯수가 늘어난다.(관계가 있는 거리를 좁게 잡아서 서포트의 갯수가 늘어난다.)
# gamma와 C가 커질수록 오버피팅이 된다.
model = SVC(kernel="rbf", gamma=gamma, C=C).fit(X_xor, y_xor)
plot_xor(model)
In [ ]:
In [47]:
from sklearn.datasets import load_iris
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
iris = load_iris()
X = iris.data[:, [2, 3]]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))
def plot_iris(X, y, model, title, xmin=-2.5, xmax=2.5, ymin=-2.5, ymax=2.5):
XX, YY = np.meshgrid(np.arange(xmin, xmax, (xmax-xmin)/1000), np.arange(ymin, ymax, (ymax-ymin)/1000))
ZZ = np.reshape(model.predict(np.array([XX.ravel(), YY.ravel()]).T), XX.shape)
plt.contourf(XX, YY, ZZ, cmap=mpl.cm.Paired_r, alpha=0.5)
plt.scatter(X[y==0, 0], X[y==0, 1], c='r', marker='^', label='0', s=100)
plt.scatter(X[y==1, 0], X[y==1, 1], c='g', marker='o', label='1', s=100)
plt.scatter(X[y==2, 0], X[y==2, 1], c='b', marker='s', label='2', s=100)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.title(title)
plt.show()
In [48]:
model = SVC(kernel='rbf', gamma=50, C=1.0).fit(X_test_std, y_test)
plot_iris(X_test_std, y_test, model, "RBF SVC")
In [ ]: