예제 #1
0
logging.basicConfig(level=logging.DEBUG)

iris = load_iris()
X = iris.data
y = iris.target
y[y != 1] = -1
y[y == 1] = 1

weights = compute_class_weight("auto", np.unique(y), y)
sample_weight = np.zeros(y.shape, dtype=np.float)
sample_weight[y==1] = weights[0]
sample_weight[y==-1] = weights[1]

# n_iter = int(1e6 / X.shape[0])
vw_clf = VWClassifier(quiet=False, loss_function="hinge", passes=500)
vw_clf.fit(X, y.astype(np.double), sample_weight)
scores = vw_clf.decision_function(X)
print "VW AP: %.3f" % average_precision_score(y, scores)

vw_clf.set_params(l2=0.1)
vw_clf.fit(X, y.astype(np.double), sample_weight)
print "VW AP: %.3f" % average_precision_score(y, scores)

# vw_clf.fit(X, y.astype(np.double), sample_weight)
# scores = vw_clf.decision_function(X)
# print "VW AP: %.3f" % average_precision_score(y, scores)

# print "-- Second fit"
# vw_clf.fit(X, y.astype(np.double))
# scores = vw_clf.decision_function(X)
예제 #2
0
파일: test_vw.py 프로젝트: chicham/pyvw
import matplotlib.pyplot as plt
from sklearn import svm
from pyvw.classifier import VWClassifier
import logging

logging.basicConfig(level=logging.DEBUG)

# we create 40 separable points
np.random.seed(0)
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
Y = np.array([-1] * 20 + [1] * 20).astype(np.double)

# fit the model

# vw_clf = VWClassifier("--quiet --loss_function hinge --early_terminate 1")
vw_clf = VWClassifier(loss_function="hinge", b=12,
                      early_terminate=3)
vw_clf.fit(X, Y, passes=5)

clf = svm.SVC(kernel='linear')
clf.fit(X, Y)


def get_hyperplane(coefs, intercept):
    a = -coefs[0] / coefs[1]
    xx = np.linspace(-5, 5)
    yy = a * xx - (intercept) / coefs[1]
    return xx, yy

# plot the parallels to the separating hyperplane that pass through the
# support vectors
# b = clf.support_vectors_[0]