def fit(self): print "fit the model" train = np.array(self.model.data) X = train[:, :2] y = train[:, 2] C = float(self.complexity.get()) gamma = float(self.gamma.get()) coef0 = float(self.coef0.get()) degree = int(self.degree.get()) kernel_map = {0: "linear", 1: "rbf", 2: "poly"} if len(np.unique(y)) == 1: clf = svm.OneClassSVM(kernel=kernel_map[self.kernel.get()], gamma=gamma, coef0=coef0, degree=degree) clf.fit(X) else: clf = svm.SVC(kernel=kernel_map[self.kernel.get()], C=C, gamma=gamma, coef0=coef0, degree=degree) clf.fit(X, y) if hasattr(clf, 'score'): print "Accuracy:", clf.score(X, y) * 100 X1, X2, Z = self.decision_surface(clf) self.model.clf = clf self.model.set_surface((X1, X2, Z)) self.model.surface_type = self.surface_type.get() self.fitted = True self.model.changed("surface")
def test_oneclass(): """ Test OneClassSVM """ clf = svm.OneClassSVM() clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(pred, [1, -1, -1]) assert_array_almost_equal(clf.intercept_, [-1.3514943]) assert_array_almost_equal(clf.dual_coef_, [[ 0.75061969, 0.74938031, 0.74996915, 0.75003085]]) assert_raises(NotImplementedError, lambda: clf.coef_)
def test_oneclass(): """ Test OneClassSVM """ clf = svm.OneClassSVM() clf.fit(X) pred = clf.predict(T) assert_array_almost_equal(pred, [1, -1, -1]) assert_array_almost_equal(clf.intercept_, [-1.351], decimal=3) assert_array_almost_equal(clf.dual_coef_, [[0.750, 0.749, 0.749, 0.750]], decimal=3) assert_raises(NotImplementedError, lambda: clf.coef_)
def train(data, label): mocsvm_model = [] maxdata = scipy.zeros((len(scipy.unique(label)), data.shape[1])) mindata = scipy.zeros((len(scipy.unique(label)), data.shape[1])) cnt = 0 for i in scipy.unique(label): # for every class learn a one class svm model # Learn an approximate value for gamma tempdata = data[(label == i), :] tempdata, maxdata[cnt, :], mindata[cnt, :] = MLUtility.normalizedata( tempdata.copy()) dist = scipy.zeros((tempdata.shape[0], tempdata.shape[0])) for j in range(tempdata.shape[0]): for k in range(tempdata.shape[0]): dist[j, k] = sum((tempdata[j, :] - tempdata[k, :])**2) g = dist.max().max() model = svm.OneClassSVM(nu=0.1, kernel='rbf', gamma=g * 8, C=10) model.fit(tempdata) # append it to the list of models mocsvm_model.append(model) cnt = cnt + 1 return mocsvm_model, maxdata, mindata
X.append([data[0], data[1]]) X = np.array(X, dtype='float64') print X x1_max = X[:][:, 0].max() x1_min = X[:][:, 0].min() x2_max = X[:][:, 1].max() x2_min = X[:][:, 1].min() x_size = len(X) xx, yy = np.meshgrid(np.linspace(x1_min, x1_max, x_size), np.linspace(x2_min, x2_max, x_size)) # fit the model clf = svm.OneClassSVM(nu=0.1, kernel="linear", gamma=0.1) clf.fit(X) # plot the line, the points, and the nearest vectors to the plane Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) y_pred = clf.predict(X) pl.set_cmap(pl.cm.Paired) pl.contourf(xx, yy, Z) pl.scatter(X[y_pred > 0, 0], X[y_pred > 0, 1], c='white', label='inliers') pl.scatter(X[y_pred <= 0, 0], X[y_pred <= 0, 1], c='black', label='outliers') pl.axis('tight') pl.legend() pl.show()
# The grid in x,y coordinates X, Y = np.meshgrid(xx, yy[::-1]) #basemap = False for i, species in enumerate([bv, mm]): print "_" * 80 print "Modeling distribution of species '%s'" % species.name print # Standardize features mean = species.train_cover.mean(axis=0) std = species.train_cover.std(axis=0) train_cover_std = (species.train_cover - mean) / std # Fit OneClassSVM print "fit OneClassSVM ... ", clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.5) clf.fit(train_cover_std) print "done. " # Plot map of South America pl.subplot(1, 2, i + 1) if basemap: print "plot coastlines using basemap" m = Basemap(projection='cyl', llcrnrlat=ymin, urcrnrlat=ymax, llcrnrlon=xmin, urcrnrlon=xmax, resolution='c') m.drawcoastlines() m.drawcountries() #m.drawrivers() else: print "plot coastlines from coverage"