728x90
반응형
import numpy as np
from sklearn import datasets
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
iris= datasets.load_iris()
X = iris['data'][:,(2,3)]
y = (iris['target']==2).astype(np.float64)
svm_clf = Pipeline([
('scaler',StandardScaler()),
('classifier', LinearSVC(C=1,loss='hinge'))
])
svm_clf.fit(X,y)
Out[23]:
In [25]:
svm_clf.predict([[5.5, 1.7]])
Out[25]:
In [33]:
from sklearn.datasets import make_moons
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
X,y = make_moons(n_samples=100,noise=0.15, random_state=42)
# n_samples : The total number of points generated.
# noise : Standard deviation of Gaussian noise added to the data.
polynomial_svm_clf= Pipeline([
('scaler', StandardScaler()),
('poly_scaler',PolynomialFeatures(degree=3)),
('svm_clf',LinearSVC(C=10, loss='hinge',max_iter=2000,random_state=42))
])
polynomial_svm_clf.fit(X,y)
Out[33]:
In [37]:
polynomial_svm_clf.predict([[5.5,1.7]])
Out[37]:
In [45]:
import matplotlib.pyplot as plt
def plot_dataset(X, y, axes):
plt.plot(X[:, 0][y==0], X[:, 1][y==0], "bs") # 1열 중에서, 2열 중에서 y==0
plt.plot(X[:, 0][y==1], X[:, 1][y==1], "g^")
plt.axis(axes)
plt.grid(True, which='both')
plt.xlabel(r"$x_1$", fontsize=20)
plt.ylabel(r"$x_2$", fontsize=20, rotation=0)
def plot_predictions(clf, axes):
x0s = np.linspace(axes[0], axes[1], 100)
x1s = np.linspace(axes[2], axes[3], 100)
x0, x1 = np.meshgrid(x0s, x1s)
X = np.c_[x0.ravel(), x1.ravel()]
y_pred = clf.predict(X).reshape(x0.shape)
y_decision = clf.decision_function(X).reshape(x0.shape)
plt.contourf(x0, x1, y_pred, cmap=plt.cm.brg, alpha=0.2)
plt.contourf(x0, x1, y_decision, cmap=plt.cm.brg, alpha=0.1)
plot_predictions(polynomial_svm_clf, [-1.5, 2.5, -1, 1.5])
plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
plt.show()
In [54]:
# 다항식 커널
from sklearn.svm import SVC
poly_kernel_svm_clf = Pipeline([
('scaler',StandardScaler()),
('svm_clf',SVC(kernel='poly',degree=3,coef0=1,C=5)), # 3차 다항식 커널 이용
])
poly_kernel_svm_clf.fit(X,y)
# 실제로 특성 추가하지 않으면서 다항식의 특성을 추가한 것과 같은 결과를 냄
Out[54]:
In [55]:
rbf_kernel_svm_clf = Pipeline([
('scaler',StandardScaler()),
('svm_clf', SVC(kernel='rbf',gamma=5,C=0.001))
])
rbf_kernel_svm_clf.fit(X,y)
Out[55]:
In [56]:
# https://bskyvision.com/163 SVM 설명
from sklearn.svm import LinearSVR
svm_reg = LinearSVR(epsilon=1.5)
svm_reg.fit(X,y)
Out[56]:
In [57]:
from sklearn.svm import SVR
svm_poly_reg = SVR(kernel='poly',gamma='auto',degree=2,C=100,epsilon=0.1)
svm_poly_reg.fit(X,y)
Out[57]:
In [97]:
from sklearn import datasets
import numpy as np
from sklearn.svm import LinearSVC, SVC
from sklearn.linear_model import SGDClassifier
from sklearn.preprocessing import StandardScaler
In [98]:
iris = datasets.load_iris()
X = iris['data'][:,(2,3)]
y = iris['target']
setosa_or_versicolor = (y==0) | (y == 1)
X = X[setosa_or_versicolor]
y = y[setosa_or_versicolor]
In [99]:
C = 5
alpha = 1 / (C * len(X))
In [101]:
lin_svc = LinearSVC(loss='hinge',C=C,random_state=42)
svc = SVC(kernel='linear',C=C)
sgd_clf = SGDClassifier(loss='hinge', learning_rate = 'constant', eta0=0.001, tol=1e-3,alpha=alpha,
max_iter=100000, random_state=42)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
lin_svc.fit(X_scaled,y)
svc.fit(X_scaled,y)
sgd_clf.fit(X_scaled,y)
print('LinearSVC :',lin_svc.intercept_,lin_svc.coef_)
print('SVC :',svc.intercept_,svc.coef_)
print('SGDClassifier :',sgd_clf.intercept_,sgd_clf.coef_)
In [105]:
# 세 개 모델의 결정 경계를 그리자
# 먼저 결정 결제들의 기울기(W)와 편향(b)을 구함
w1 = -lin_svc.coef_[0,0] / lin_svc.coef_[0,1]
b1 = -lin_svc.intercept_[0] / lin_svc.coef_[0,1]
w2 = -svc.coef_[0, 0]/svc.coef_[0, 1]
b2 = -svc.intercept_[0]/svc.coef_[0, 1]
w3 = -sgd_clf.coef_[0, 0]/sgd_clf.coef_[0, 1]
b3 = -sgd_clf.intercept_[0]/sgd_clf.coef_[0, 1]
# 결정 결계를 원본 스케일로 변환하기
line1 = scaler.inverse_transform([[-10,-10*w1+b1],[10,10*w1+b1]])
line2 = scaler.inverse_transform([[-10,-10*w2+b2],[10,10*w2+b2]])
line3 = scaler.inverse_transform([[-10,-10*w3+b3],[10,10*w3+b3]])
# 세 개의 결정 경계를 모두 그리자
plt.figure(figsize=(11,4))
plt.plot(line1[:,0], line1[:,1], "k:",label="LinearSVC")
plt.plot(line2[:,0],line2[:,1],"b--",linewidth=2,label="SVC")
plt.plot(line3[:,0],line3[:,1],'r-',label="SGDClassifier")
plt.plot(X[:,0][y==1],X[:,1][y==1], 'bs')
plt.plot(X[:,0][y==0],X[:,1][y==0],'yo')
plt.xlabel("꽃잎 길이", fontsize=14)
plt.ylabel("꽃잎 너비",fontsize=14)
plt.legend(loc='upper center',fontsize=14)
plt.axis([0,5.5,0,2])
plt.show()
# 아주 비슷한 결정 경계를 보인다.
In [106]:
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784',version=1)
In [107]:
X = mnist['data']
y = mnist['target']
X_train = X[:60000]
y_train = y[:60000]
X_test = X[60000:]
y_test=y[60000:]
In [108]:
# 훈련 샘플 순서 섞기
np.random.seed(42)
rnd_idx = np.random.permutation(60000)
X_train = X_train[rnd_idx]
y_train = y_train[rnd_idx]
In [109]:
lin_clf = LinearSVC(max_iter=10000,random_state=42)
lin_clf.fit(X_train,y_train)
Out[109]:
In [110]:
from sklearn.metrics import accuracy_score
y_pred = lin_clf.predict(X_train)
accuracy_score(y_train,y_pred)
Out[110]:
In [111]:
# 전처리 과정 ( 스케일 조정 )
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train.astype(np.float32))
X_test_scaled = scaler.fit_transform(X_test.astype(np.float32))
In [112]:
lin_clf = LinearSVC(max_iter=10000,random_state=42)
lin_clf.fit(X_train_scaled,y_train)
Out[112]:
In [113]:
y_pred = lin_clf.predict(X_train_scaled)
accuracy_score(y_train,y_pred)
Out[113]:
In [114]:
svm_clf = SVC(gamma='auto',decision_function_shape='ovr')
svm_clf.fit(X_train_scaled[:10000],y_train[:10000])
Out[114]:
In [116]:
y_pred = svm_clf.predict(X_train_scaled)
accuracy_score(y_train,y_pred)
Out[116]:
In [117]:
# 랜덤 서치로 하이퍼파라미 튜닝하기
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import reciprocal, uniform
param_dist = {'gamma':reciprocal(0.001,0.1),"C":uniform(1,10)}
rnd_search_cv = RandomizedSearchCV(svm_clf,param_dist,cv=3,n_iter=10,verbose=2,n_jobs=-1)
rnd_search_cv.fit(X_train_scaled[:1000],y_train[:1000])
Out[117]:
In [119]:
rnd_search_cv.best_estimator_
Out[119]:
In [120]:
rnd_search_cv.best_score_
Out[120]:
In [121]:
rnd_search_cv.best_estimator_.fit(X_train_scaled,y_train)
Out[121]:
In [122]:
y_pred = rnd_search_cv.best_estimator_.predict(X_train_scaled)
accuracy_score(y_train,y_pred)
Out[122]:
In [124]:
# test to the Test Set
y_pred = rnd_search_cv.best_estimator_.predict(X_test_scaled)
accuracy_score(y_test,y_pred)
Out[124]:
In [129]:
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
X = housing['data']
y = housing['target']
In [130]:
# 훈련 / 테스트 세트로 나누기
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)
In [131]:
# 데이터 스케일링
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
In [133]:
# LinearSVC 먼저 학습
lin_svr = LinearSVR(max_iter=10000,random_state=42)
lin_svr.fit(X_train_scaled,y_train)
Out[133]:
In [134]:
# 성능 확인
from sklearn.metrics import mean_squared_error
y_pred = lin_svr.predict(X_train_scaled)
mse = mean_squared_error(y_train,y_pred)
mse
Out[134]:
In [135]:
#RMSE
np.sqrt(mse)
Out[135]:
In [140]:
# rbf kernel 이용하기 위해 랜덤서치로 하이퍼파라미터(C,gamma) 찾기
from sklearn.svm import SVR
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import reciprocal,uniform
param_dist = {'gamma':reciprocal(0.001,0.1),'C':uniform(1,10)}
rnd_search_cv = RandomizedSearchCV(SVR(),param_dist,cv=3,n_iter=10,verbose=2,random_state=42,n_jobs=-1)
rnd_search_cv.fit(X_train_scaled,y_train)
Out[140]:
In [146]:
y_pred = rnd_search_cv.best_estimator_.predict(X_train_scaled)
mse = mean_squared_error(y_train,y_pred)
mse
# 선형모델보다 나아졌다.
Out[146]:
In [147]:
y_pred = rnd_search_cv.best_estimator_.predict(X_test_scaled)
mse = mean_squared_error(y_test,y_pred)
np.sqrt(mse)
Out[147]:
728x90
반응형