More actions
imported>rabierre No edit summary |
No edit summary |
||
| (6 intermediate revisions by one other user not shown) | |||
| Line 2: | Line 2: | ||
[[머신러닝스터디/2016/목차]] | [[머신러닝스터디/2016/목차]] | ||
== 내용 == | == 내용 == | ||
* SVM 실습 with skflow | * SVM 실습 with [http://scikit-learn.org/stable/ sklearn] | ||
** skflow(tensorflow의 contib/learn으로 흡수됨)을 사용하려 했으나 svm모듈 부분이 최신 커밋에만 포함되어 있어 sklearn을 사용하기로 함 | |||
** sklearn 버전은 0.17.1 | |||
설치 방법 | |||
$ sudo pip install sklearn | |||
=== 코드 === | |||
import sklearn | |||
from sklearn import svm | |||
#### SVC with rbf kernel | |||
# default is rbf kernel | |||
clf = svm.SVC() | |||
x_data = [[0,0], [0,1], [1,0], [1,1]] | |||
# linear | |||
## And gate | |||
y_data = [0, 0, 0, 1] | |||
clf.fit(x_data, y_data) | |||
# SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, | |||
# decision_function_shape=None, degree=3, gamma='auto', kernel='rbf', | |||
# max_iter=-1, probability=False, random_state=None, shrinking=True, | |||
# tol=0.001, verbose=False) | |||
clf.predict(x_data) | |||
# array([0, 0, 0, 0]) # wrong | |||
## Or gate | |||
y_data = [0, 1, 1, 1] | |||
clf.fit(x_data, y_data) | |||
clf.predict(x_data) | |||
# array([1, 1, 1, 1]) | |||
# non-linear | |||
## Xor gate | |||
y_data = [0, 1, 1, 0] | |||
clf.fit(x_data, y_data) | |||
clf.predict(x_data) | |||
# array([0, 1, 1, 0]) # Correct answer | |||
#### SVC with Linear kernel | |||
clf = svm.SVC(kernel='linear') | |||
clf.fit(x_data, y_data) | |||
# SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, | |||
# decision_function_shape=None, degree=3, gamma='auto', kernel='linear', | |||
# max_iter=-1, probability=False, random_state=None, shrinking=True, | |||
# tol=0.001, verbose=False) | |||
clf.predict(x_data) | |||
# array([0, 0, 0, 0]) | |||
#### LinearSVC | |||
clf = svm.LinearSVC() | |||
clf.fit(x_data, y_data) | |||
# LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True, | |||
# intercept_scaling=1, loss='squared_hinge', max_iter=1000, | |||
# multi_class='ovr', penalty='l2', random_state=None, tol=0.0001, | |||
# verbose=0) | |||
clf.predict(x_data) | |||
# array([0, 0, 0, 1]) # Correct answer | |||
* non-linear는 분류하지만 linear는 분류하지 못하는 SVC? | |||
* multi-class에서 분류 시에 voter의 결과에 따라 분류됨 | |||
** 결과에 0이 많으면(And gate) 모두 0이 되고 | |||
y_data = [0, 0, 0, 1]면 결과가 [0, 0, 0, 0] | |||
** 1이 더 많은 경우(Or gate) 학습 결과가 다음과 같음 | |||
y_data = [0, 1, 1, 1]면 결과가 [1, 1, 1, 1] | |||
* SVC와 LinearSVC의 차이란? | |||
* [http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn-svm-svc]에 따르면 둘의 차이점은 다음과 같다 | |||
** SVC; '''C-Support Vector Classification'''. The implementation is based on libsvm. | |||
** LinearSVC; '''Scalable Linear Support Vector Machine''' for classification implemented using liblinear. Check the See also section of LinearSVC for more comparison element. | |||
** libsvm과 liblinear의 차이점은 뭐지? - [[서지혜]] | |||
== 다음 시간에는 == | == 다음 시간에는 == | ||
* 동영상 8주차 보기 | |||
== 더 보기 == | == 더 보기 == | ||
* skflow의 탄생에 영감을 준 [http://scikit-learn.org scikit] | |||
* [https://github.com/tensorflow/skflow skflow github] | |||
** 현재 tensorflow의 contrib중 learn 모듈로 변경됨 - [https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/learn tflearn] | |||
Latest revision as of 07:45, 28 July 2016
내용
- SVM 실습 with sklearn
- skflow(tensorflow의 contib/learn으로 흡수됨)을 사용하려 했으나 svm모듈 부분이 최신 커밋에만 포함되어 있어 sklearn을 사용하기로 함
- sklearn 버전은 0.17.1
설치 방법
$ sudo pip install sklearn
코드
import sklearn from sklearn import svm #### SVC with rbf kernel # default is rbf kernel clf = svm.SVC() x_data = [[0,0], [0,1], [1,0], [1,1]] # linear ## And gate y_data = [0, 0, 0, 1] clf.fit(x_data, y_data) # SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, # decision_function_shape=None, degree=3, gamma='auto', kernel='rbf', # max_iter=-1, probability=False, random_state=None, shrinking=True, # tol=0.001, verbose=False) clf.predict(x_data) # array([0, 0, 0, 0]) # wrong ## Or gate y_data = [0, 1, 1, 1] clf.fit(x_data, y_data) clf.predict(x_data) # array([1, 1, 1, 1]) # non-linear ## Xor gate y_data = [0, 1, 1, 0] clf.fit(x_data, y_data) clf.predict(x_data) # array([0, 1, 1, 0]) # Correct answer #### SVC with Linear kernel clf = svm.SVC(kernel='linear') clf.fit(x_data, y_data) # SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, # decision_function_shape=None, degree=3, gamma='auto', kernel='linear', # max_iter=-1, probability=False, random_state=None, shrinking=True, # tol=0.001, verbose=False) clf.predict(x_data) # array([0, 0, 0, 0]) #### LinearSVC clf = svm.LinearSVC() clf.fit(x_data, y_data) # LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True, # intercept_scaling=1, loss='squared_hinge', max_iter=1000, # multi_class='ovr', penalty='l2', random_state=None, tol=0.0001, # verbose=0) clf.predict(x_data) # array([0, 0, 0, 1]) # Correct answer
- non-linear는 분류하지만 linear는 분류하지 못하는 SVC?
- multi-class에서 분류 시에 voter의 결과에 따라 분류됨
- 결과에 0이 많으면(And gate) 모두 0이 되고
y_data = [0, 0, 0, 1]면 결과가 [0, 0, 0, 0]
- 1이 더 많은 경우(Or gate) 학습 결과가 다음과 같음
y_data = [0, 1, 1, 1]면 결과가 [1, 1, 1, 1]
- SVC와 LinearSVC의 차이란?
- [1]에 따르면 둘의 차이점은 다음과 같다
- SVC; C-Support Vector Classification. The implementation is based on libsvm.
- LinearSVC; Scalable Linear Support Vector Machine for classification implemented using liblinear. Check the See also section of LinearSVC for more comparison element.
- libsvm과 liblinear의 차이점은 뭐지? - 서지혜
다음 시간에는
- 동영상 8주차 보기
더 보기
- skflow의 탄생에 영감을 준 scikit
- skflow github
- 현재 tensorflow의 contrib중 learn 모듈로 변경됨 - tflearn