elif dec_name == 'quadratic': feat_calc = soil.QuadraticSurfaceCalculator( 2, np.array([X[:, 0].min(), X[:, 0].max(), X[:, 1].min(), X[:, 1].max()], dtype='float32'), random_seed=rs) cls = [] lm = [] for i in range(n_trees): cls.append( soil.ThresholdDecider( feat_sel_prov, feat_calc, soil.RandomizedClassificationThresholdOptimizer( suggestions, n_classes, soil.EntropyGain(soil.ShannonEntropy()), random_seed=rs))) lm.append(soil.ClassificationLeafManager(n_classes)) forest = soil.Forest(depth, 1, 2, n_trees, cls, lm, soil.ClassicTraining(soil.NoBagging())) forest.fit(X, Y) if INTERACTIVE: plt.figure() point_prob_plot(forest, X, Y, plotx, ploty) plt.title('Random Forest $(T=%d, D=%d, |\Theta_j|=%d)$' % (n_trees, depth, suggestions * 2)) plt.savefig('spiral_rf.png') plt.show() assert accuracy_score(Y, np.argmax(forest.predict(X), axis=1)) > 0.95
X, Y = make_spiral(n_arms=n_classes, noise=.4) ############################################################################## parameters = {'kernel': ['rbf'], 'C': [1, 10, 100, 1000, 10000, 100000], 'gamma': [10 ** x for x in range(-5, 3)], 'probability':[True]} svr = svm.SVC() clf = grid_search.GridSearchCV(svr, parameters) clf.fit(X, Y.ravel()) svmp = probaproxy(clf.predict_proba) plt.figure() point_prob_plot(svmp, X, Y, plotx, ploty) plt.title('RBF kernel SVM $(\gamma=%d, C=%f)$' % (clf.best_params_['gamma'], clf.best_params_['C'])) plt.savefig('spiral_svm.png') plt.show() ############################################################################## parameters = {'C': [1, 10, 100, 1000, 10000], 'probability': [True]} svr = svm.SVC(kernel='linear', max_iter=20000) clf = grid_search.GridSearchCV(svr, parameters) clf.fit(X, Y.ravel()) svmp = probaproxy(clf.predict_proba) plt.figure() point_prob_plot(svmp, X, Y, plotx, ploty)
n_trees = 200 # These variables will contain the classifiers and leaf managers for each tree. cls = [] lm = [] for i in range(n_trees): # Classifier: cls.append(soil.ThresholdDecider( soil.StandardFeatureSelectionProvider(1, 2, 2, 2, random_seed=1+i), soil.LinearSurfaceCalculator(400, random_seed=1+i), soil.RandomizedClassificationThresholdOptimizer( True, 2, soil.EntropyGain(soil.ShannonEntropy()), random_seed=1+i))) # Leaf manager: lm.append(soil.ClassificationLeafManager(2)) forest = soil.Forest(depth, 1, 2, n_trees, cls, lm, soil.ClassicTraining(soil.NoBagging())) forest.fit(X, Y) assert accuracy_score(Y, np.argmax(forest.predict(X), axis=1)) > 0.95 if INTERACTIVE: plt.figure() point_prob_plot(forest, X, Y, plotx, ploty) plt.savefig('custom.png') plt.show()
############################################################################## parameters = { 'kernel': ['rbf'], 'C': [1, 10, 100, 1000, 10000, 100000], 'gamma': [10**x for x in range(-5, 3)], 'probability': [True] } svr = svm.SVC() clf = grid_search.GridSearchCV(svr, parameters) clf.fit(X, Y.ravel()) svmp = probaproxy(clf.predict_proba) plt.figure() point_prob_plot(svmp, X, Y, plotx, ploty) plt.title('RBF kernel SVM $(\gamma=%d, C=%f)$' % (clf.best_params_['gamma'], clf.best_params_['C'])) plt.savefig('spiral_svm.png') plt.show() ############################################################################## parameters = {'C': [1, 10, 100, 1000, 10000], 'probability': [True]} svr = svm.SVC(kernel='linear', max_iter=20000) clf = grid_search.GridSearchCV(svr, parameters) clf.fit(X, Y.ravel()) svmp = probaproxy(clf.predict_proba) plt.figure() point_prob_plot(svmp, X, Y, plotx, ploty)
h = SimpleNode(sx, 2, 4) h2 = SimpleNode(h, 4, 2) out = SimpleNode(h2, 2, 1, nlin=pynnet.nlins.sigmoid) cost = errors.mse(out, sy) theano.config.blas.ldflags = '' eval = theano.function([sx], out.output) test = theano.function([sx, sy], cost.output) train = theano.function([sx, sy], cost.output, updates=get_updates(cost.params, cost.output, 0.01)) print("Error at start:", test(X, Y)) for i in range(200000): train(X, Y) print("Error after 200000:", test(X, Y)) def pfunc(x): return 1. - eval(x) clp = probaproxy(pfunc) plt.figure() point_prob_plot(clp, X, Y, plotx, ploty) plt.title('ANN (2 hidden layers, 4, 2)') plt.savefig('spiral_ann.png') plt.show()
#%% Build ANN. sx = theano.tensor.matrix('x') sy = theano.tensor.matrix('y') h = SimpleNode(sx, 2, 4) h2 = SimpleNode(h, 4, 2) out = SimpleNode(h2, 2, 1, nlin=pynnet.nlins.sigmoid) cost = errors.mse(out, sy) theano.config.blas.ldflags='' eval = theano.function([sx], out.output) test = theano.function([sx, sy], cost.output) train = theano.function([sx, sy], cost.output, updates=get_updates(cost.params, cost.output, 0.01)) print("Error at start:", test(X, Y)) for i in range(200000): train(X, Y) print("Error after 200000:", test(X, Y)) def pfunc(x): return 1. - eval(x) clp = probaproxy(pfunc) plt.figure() point_prob_plot(clp, X, Y, plotx, ploty) plt.title('ANN (2 hidden layers, 4, 2)') plt.savefig('spiral_ann.png') plt.show()