def persistence(): print "Persistence" numpy.random.seed(42) n_dims = 2 n_class = 12 size = 100 labels = numpy.random.random_integers(0, 256, size) samples = numpy.zeros((size, n_dims), dtype=type) print "Generating data" for i in range(0, size): t = 6.28 * numpy.random.random_sample() samples[i][0] = 2 * labels[i] + 1.5 * numpy.cos(t) samples[i][1] = 2 * labels[i] + 1.5 * numpy.sin(t) print "Creating dense classifier" classifier = algo.svm_dense(0, n_dims=n_dims, seed=42) print "Adding sample vectors to dense classifier" for y, x_list in zip(labels, samples): x = numpy.array(x_list, dtype=type) classifier.add_sample(float(y), x) print "Pickling dense classifier" cPickle.dump(classifier, open('test', 'wb')) classifier = cPickle.load(open('test', 'rb')) print "Training dense classifier" classifier.train(gamma=1, C=10, eps=1e-1) print "Predicting with dense classifier" print classifier.predict(samples[0]) print "Creating 0/1 classifier" classifier01 = algo.svm_01(n_dims=n_dims, seed=42) print "Adding sample vectors to 0/1 classifier" for y, x_list in zip(labels, samples): x = numpy.array(x_list, dtype=type) classifier01.add_sample(float(y), x) print "Training 0/1 classifier" classifier01.train(gamma=1. / 3., C=100, eps=1e-1) print "Pickling 0/1 classifier" cPickle.dump(classifier01, open('test', 'wb')) classifier01 = cPickle.load(open('test', 'rb')) print "Predicting with 0/1 classifier" print classifier01.predict(numpy.array(samples[0], dtype=type))
def persistence(): print "Persistence" numpy.random.seed(42) n_dims = 2 n_class = 12 size = 100 labels = numpy.random.random_integers(0, 256, size) samples = numpy.zeros((size, n_dims), dtype=type) print "Generating data" for i in range(0, size): t = 6.28 * numpy.random.random_sample() samples[i][0] = 2 * labels[i] + 1.5 * numpy.cos(t) samples[i][1] = 2 * labels[i] + 1.5 * numpy.sin(t) print "Creating dense classifier" classifier = algo.svm_dense(0, n_dims = n_dims, seed=42) print "Adding sample vectors to dense classifier" for y, x_list in zip(labels, samples): x = numpy.array(x_list, dtype=type) classifier.add_sample(float(y), x) print "Pickling dense classifier" cPickle.dump(classifier, open('test', 'wb')) classifier = cPickle.load(open('test', 'rb')) print "Training dense classifier" classifier.train(gamma = 1, C = 10, eps=1e-1) print "Predicting with dense classifier" print classifier.predict(samples[0]) print "Creating 0/1 classifier" classifier01 = algo.svm_01(n_dims = n_dims, seed=42) print "Adding sample vectors to 0/1 classifier" for y, x_list in zip(labels, samples): x = numpy.array(x_list, dtype=type) classifier01.add_sample(float(y), x) print "Training 0/1 classifier" classifier01.train(gamma = 1./3., C = 100, eps=1e-1) print "Pickling 0/1 classifier" cPickle.dump(classifier01, open('test', 'wb')) classifier01 = cPickle.load(open('test', 'rb')) print "Predicting with 0/1 classifier" print classifier01.predict(numpy.array(samples[0], dtype=type))
def testPersistentSize(self): for _ in range(2): # Multiple of 8 because of sse. nDims = 32 nClass = _RGEN.randint(4, 8) size = _RGEN.randint(20, 50) labels = _RGEN.random_integers(0, nClass - 1, size) samples = _RGEN.normal(size=(size, nDims)).astype(_DTYPE) classifier = svm_dense(0, nDims, seed=_SEED, probability=True) for y, xList in zip(labels, samples): x = np.array(xList, dtype=_DTYPE) classifier.add_sample(float(y), x) classifier.train(gamma=1.0 / 3.0, C=100, eps=1e-1) classifier.cross_validate(2, gamma=0.5, C=10, eps=1e-3) s = classifier.__getstate__() print("nDims=", nDims, "nClass=", nClass, "n_vectors=", size, end=' ') print("dense:", len(s), classifier.persistent_size(), end=' ') self.assertEqual(len(s), classifier.persistent_size()) classifier01 = svm_01(0, nDims, seed=_SEED, probability=True) for y, xList in zip(labels, samples): x = np.array(xList, dtype=_DTYPE) classifier01.add_sample(float(y), x) classifier01.train(gamma=1.0 / 3.0, C=100, eps=1e-1) classifier01.cross_validate(2, gamma=0.5, C=10, eps=1e-3) s = classifier01.__getstate__() print("0/1", len(s), classifier01.persistent_size()) self.assertEqual(len(s), classifier01.persistent_size())
def testPersistentSize(self): for _ in range(2): # Multiple of 8 because of sse. nDims = 32 nClass = _RGEN.randint(4, 8) size = _RGEN.randint(20, 50) labels = _RGEN.random_integers(0, nClass - 1, size) samples = _RGEN.normal(size=(size, nDims)).astype(_DTYPE) classifier = svm_dense(0, nDims, seed=_SEED, probability=True) for y, xList in zip(labels, samples): x = np.array(xList, dtype=_DTYPE) classifier.add_sample(float(y), x) classifier.train(gamma=1.0/3.0, C=100, eps=1e-1) classifier.cross_validate(2, gamma=0.5, C=10, eps=1e-3) s = classifier.__getstate__() print "nDims=", nDims, "nClass=", nClass, "n_vectors=", size, print "dense:", len(s), classifier.persistent_size(), self.assertEqual(len(s), classifier.persistent_size()) classifier01 = svm_01(0, nDims, seed=_SEED, probability=True) for y, xList in zip(labels, samples): x = np.array(xList, dtype=_DTYPE) classifier01.add_sample(float(y), x) classifier01.train(gamma=1.0/3.0, C=100, eps=1e-1) classifier01.cross_validate(2, gamma=0.5, C=10, eps=1e-3) s = classifier01.__getstate__() print "0/1", len(s), classifier01.persistent_size() self.assertEqual(len(s), classifier01.persistent_size())
def persistence(): print("Persistence") numpy.random.seed(42) n_dims = 2 n_class = 12 size = 100 labels = numpy.random.random_integers(0, 256, size) samples = numpy.zeros((size, n_dims), dtype=type) print("Generating data") for i in range(0, size): t = 6.28 * numpy.random.random_sample() samples[i][0] = 2 * labels[i] + 1.5 * numpy.cos(t) samples[i][1] = 2 * labels[i] + 1.5 * numpy.sin(t) print("Creating dense classifier") classifier = algo.svm_dense(0, n_dims=n_dims, seed=42) print("Adding sample vectors to dense classifier") for y, x_list in zip(labels, samples): x = numpy.array(x_list, dtype=type) classifier.add_sample(float(y), x) print("Serializing dense classifier") # CHH proto is not supported # schema = classifier.getSchema() # with open("test", "w+b") as f: # # Save # proto = schema.new_message() # classifier.write(proto) # proto.write(f) # # # Load # f.seek(0) # proto2 = schema.read(f) # classifier = algo.svm_dense.read(proto2) print("Training dense classifier") classifier.train(gamma=1, C=10, eps=1e-1) print("Predicting with dense classifier") print(classifier.predict(samples[0])) print("Creating 0/1 classifier") classifier01 = algo.svm_01(n_dims=n_dims, seed=42) print("Adding sample vectors to 0/1 classifier") for y, x_list in zip(labels, samples): x = numpy.array(x_list, dtype=type) classifier01.add_sample(float(y), x) print("Training 0/1 classifier") classifier01.train(gamma=1. / 3., C=100, eps=1e-1) print("Serializing 0/1 classifier") # CHH proto is not supported # schema = classifier01.getSchema() # with open("test", "w+b") as f: # # Save # proto = schema.new_message() # classifier01.write(proto) # proto.write(f) # # # Load # f.seek(0) # proto2 = schema.read(f) # classifier01 = algo.svm_01.read(proto2) print("Predicting with 0/1 classifier") print(classifier01.predict(numpy.array(samples[0], dtype=type)))
def persistence(): print "Persistence" numpy.random.seed(42) n_dims = 2 n_class = 12 size = 100 labels = numpy.random.random_integers(0, 256, size) samples = numpy.zeros((size, n_dims), dtype=type) print "Generating data" for i in range(0, size): t = 6.28 * numpy.random.random_sample() samples[i][0] = 2 * labels[i] + 1.5 * numpy.cos(t) samples[i][1] = 2 * labels[i] + 1.5 * numpy.sin(t) print "Creating dense classifier" classifier = algo.svm_dense(0, n_dims = n_dims, seed=42) print "Adding sample vectors to dense classifier" for y, x_list in zip(labels, samples): x = numpy.array(x_list, dtype=type) classifier.add_sample(float(y), x) print "Serializing dense classifier" schema = classifier.getSchema() with open("test", "w+b") as f: # Save proto = schema.new_message() classifier.write(proto) proto.write(f) # Load f.seek(0) proto2 = schema.read(f) classifier = algo.svm_dense.read(proto2) print "Training dense classifier" classifier.train(gamma = 1, C = 10, eps=1e-1) print "Predicting with dense classifier" print classifier.predict(samples[0]) print "Creating 0/1 classifier" classifier01 = algo.svm_01(n_dims = n_dims, seed=42) print "Adding sample vectors to 0/1 classifier" for y, x_list in zip(labels, samples): x = numpy.array(x_list, dtype=type) classifier01.add_sample(float(y), x) print "Training 0/1 classifier" classifier01.train(gamma = 1./3., C = 100, eps=1e-1) print "Serializing 0/1 classifier" schema = classifier01.getSchema() with open("test", "w+b") as f: # Save proto = schema.new_message() classifier01.write(proto) proto.write(f) # Load f.seek(0) proto2 = schema.read(f) classifier01 = algo.svm_01.read(proto2) print "Predicting with 0/1 classifier" print classifier01.predict(numpy.array(samples[0], dtype=type))