Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #761 from gsomix/examples
Fixes and clean ups in python examples
- Loading branch information
Showing
206 changed files
with
549 additions
and
302 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
examples/undocumented/python_modular/classifier_averaged_perceptron_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
1 change: 1 addition & 0 deletions
1
examples/undocumented/python_modular/classifier_conjugateindex_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
3 changes: 2 additions & 1 deletion
3
examples/undocumented/python_modular/classifier_custom_kernel_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
examples/undocumented/python_modular/classifier_gmnpsvm_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
1 change: 1 addition & 0 deletions
1
examples/undocumented/python_modular/classifier_gpbtsvm_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
examples/undocumented/python_modular/classifier_larank_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 33 additions & 29 deletions
62
examples/undocumented/python_modular/classifier_libsvm_minimal_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,33 @@ | ||
from numpy import * | ||
from numpy.random import randn | ||
from shogun.Features import * | ||
from shogun.Classifier import * | ||
from shogun.Kernel import * | ||
|
||
num=1000 | ||
dist=1 | ||
width=2.1 | ||
C=1 | ||
|
||
traindata_real=concatenate((randn(2,num)-dist, randn(2,num)+dist), axis=1) | ||
testdata_real=concatenate((randn(2,num)-dist, randn(2,num)+dist), axis=1); | ||
|
||
trainlab=concatenate((-ones(num), ones(num))); | ||
testlab=concatenate((-ones(num), ones(num))); | ||
|
||
feats_train=RealFeatures(traindata_real); | ||
feats_test=RealFeatures(testdata_real); | ||
kernel=GaussianKernel(feats_train, feats_train, width); | ||
|
||
labels=BinaryLabels(trainlab); | ||
svm=LibSVM(C, kernel, labels); | ||
svm.train(); | ||
|
||
kernel.init(feats_train, feats_test); | ||
out=svm.apply().get_labels(); | ||
testerr=mean(sign(out)!=testlab) | ||
print(testerr) | ||
#!/usr/bin/env python | ||
from numpy import mean, sign | ||
|
||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
traindat = lm.load_numbers('../data/fm_train_real.dat') | ||
testdat = lm.load_numbers('../data/fm_test_real.dat') | ||
label_traindat = lm.load_labels('../data/label_train_twoclass.dat') | ||
|
||
parameter_list = [[traindat,testdat,label_traindat,2.1,1]] | ||
|
||
def classifier_libsvm_minimal_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_twoclass=label_traindat,width=2.1,C=1): | ||
from shogun.Features import RealFeatures, BinaryLabels | ||
from shogun.Classifier import LibSVM | ||
from shogun.Kernel import GaussianKernel | ||
|
||
feats_train=RealFeatures(fm_train_real); | ||
feats_test=RealFeatures(fm_test_real); | ||
kernel=GaussianKernel(feats_train, feats_train, width); | ||
|
||
labels=BinaryLabels(label_train_twoclass); | ||
svm=LibSVM(C, kernel, labels); | ||
svm.train(); | ||
|
||
kernel.init(feats_train, feats_test); | ||
out=svm.apply().get_labels(); | ||
testerr=mean(sign(out)!=label_train_twoclass) | ||
print(testerr) | ||
|
||
if __name__=='__main__': | ||
print('LibSVM Minimal') | ||
classifier_libsvm_minimal_modular(*parameter_list[0]) |
1 change: 1 addition & 0 deletions
1
examples/undocumented/python_modular/classifier_libsvm_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
1 change: 1 addition & 0 deletions
1
examples/undocumented/python_modular/classifier_libsvmoneclass_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
1 change: 1 addition & 0 deletions
1
examples/undocumented/python_modular/classifier_mpdsvm_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
|
148 changes: 78 additions & 70 deletions
148
examples/undocumented/python_modular/classifier_multiclass_ecoc.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,87 @@ | ||
import classifier_multiclass_shared | ||
#!/usr/bin/env python | ||
import re | ||
import time | ||
from tools.multiclass_shared import prepare_data | ||
|
||
# run with toy data | ||
[traindat, label_traindat, testdat, label_testdat] = classifier_multiclass_shared.prepare_data() | ||
[traindat, label_traindat, testdat, label_testdat] = prepare_data() | ||
# run with opt-digits if available | ||
#[traindat, label_traindat, testdat, label_testdat] = classifier_multiclass_shared.prepare_data(False) | ||
#[traindat, label_traindat, testdat, label_testdat] = prepare_data(False) | ||
|
||
parameter_list = [[traindat,testdat,label_traindat,label_testdat,2.1,1,1e-5]] | ||
|
||
import shogun.Classifier as Classifier | ||
from shogun.Classifier import ECOCStrategy | ||
from shogun.Features import RealFeatures, MulticlassLabels | ||
from shogun.Classifier import LibLinear, L2R_L2LOSS_SVC, LinearMulticlassMachine | ||
from shogun.Evaluation import MulticlassAccuracy | ||
def classifier_multiclass_ecoc (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,label_test_multiclass=label_testdat,lawidth=2.1,C=1,epsilon=1e-5): | ||
|
||
def nonabstract_class(name): | ||
try: | ||
getattr(Classifier, name)() | ||
except TypeError: | ||
return False | ||
return True | ||
import shogun.Classifier as Classifier | ||
from shogun.Classifier import ECOCStrategy, LibLinear, L2R_L2LOSS_SVC, LinearMulticlassMachine | ||
from shogun.Evaluation import MulticlassAccuracy | ||
from shogun.Features import RealFeatures, MulticlassLabels | ||
|
||
import re | ||
encoders = [x for x in dir(Classifier) | ||
if re.match(r'ECOC.+Encoder', x) and nonabstract_class(x)] | ||
decoders = [x for x in dir(Classifier) | ||
if re.match(r'ECOC.+Decoder', x) and nonabstract_class(x)] | ||
|
||
fea_train = RealFeatures(traindat) | ||
fea_test = RealFeatures(testdat) | ||
gnd_train = MulticlassLabels(label_traindat) | ||
if label_testdat is None: | ||
gnd_test = None | ||
else: | ||
gnd_test = MulticlassLabels(label_testdat) | ||
|
||
base_classifier = LibLinear(L2R_L2LOSS_SVC) | ||
base_classifier.set_bias_enabled(True) | ||
|
||
print('Testing with %d encoders and %d decoders' % (len(encoders), len(decoders))) | ||
print('-' * 70) | ||
format_str = '%%15s + %%-10s %%-10%s %%-10%s %%-10%s' | ||
print((format_str % ('s', 's', 's')) % ('encoder', 'decoder', 'codelen', 'time', 'accuracy')) | ||
|
||
def run_ecoc(ier, idr): | ||
encoder = getattr(Classifier, encoders[ier])() | ||
decoder = getattr(Classifier, decoders[idr])() | ||
|
||
# whether encoder is data dependent | ||
if hasattr(encoder, 'set_labels'): | ||
encoder.set_labels(gnd_train) | ||
encoder.set_features(fea_train) | ||
|
||
strategy = ECOCStrategy(encoder, decoder) | ||
classifier = LinearMulticlassMachine(strategy, fea_train, base_classifier, gnd_train) | ||
classifier.train() | ||
label_pred = classifier.apply(fea_test) | ||
if gnd_test is not None: | ||
evaluator = MulticlassAccuracy() | ||
acc = evaluator.evaluate(label_pred, gnd_test) | ||
else: | ||
acc = None | ||
|
||
return (classifier.get_num_machines(), acc) | ||
def nonabstract_class(name): | ||
try: | ||
getattr(Classifier, name)() | ||
except TypeError: | ||
return False | ||
return True | ||
|
||
encoders = [x for x in dir(Classifier) | ||
if re.match(r'ECOC.+Encoder', x) and nonabstract_class(x)] | ||
decoders = [x for x in dir(Classifier) | ||
if re.match(r'ECOC.+Decoder', x) and nonabstract_class(x)] | ||
|
||
import time | ||
for ier in range(len(encoders)): | ||
for idr in range(len(decoders)): | ||
t_begin = time.clock() | ||
(codelen, acc) = run_ecoc(ier, idr) | ||
if acc is None: | ||
acc_fmt = 's' | ||
acc = 'N/A' | ||
else: | ||
acc_fmt = '.4f' | ||
|
||
t_elapse = time.clock() - t_begin | ||
print((format_str % ('d', '.3f', acc_fmt)) % | ||
(encoders[ier][4:-7], decoders[idr][4:-7], codelen, t_elapse, acc)) | ||
fea_train = RealFeatures(fm_train_real) | ||
fea_test = RealFeatures(fm_test_real) | ||
gnd_train = MulticlassLabels(label_train_multiclass) | ||
if label_test_multiclass is None: | ||
gnd_test = None | ||
else: | ||
gnd_test = MulticlassLabels(label_test_multiclass) | ||
|
||
base_classifier = LibLinear(L2R_L2LOSS_SVC) | ||
base_classifier.set_bias_enabled(True) | ||
|
||
print('Testing with %d encoders and %d decoders' % (len(encoders), len(decoders))) | ||
print('-' * 70) | ||
format_str = '%%15s + %%-10s %%-10%s %%-10%s %%-10%s' | ||
print((format_str % ('s', 's', 's')) % ('encoder', 'decoder', 'codelen', 'time', 'accuracy')) | ||
|
||
def run_ecoc(ier, idr): | ||
encoder = getattr(Classifier, encoders[ier])() | ||
decoder = getattr(Classifier, decoders[idr])() | ||
|
||
# whether encoder is data dependent | ||
if hasattr(encoder, 'set_labels'): | ||
encoder.set_labels(gnd_train) | ||
encoder.set_features(fea_train) | ||
|
||
strategy = ECOCStrategy(encoder, decoder) | ||
classifier = LinearMulticlassMachine(strategy, fea_train, base_classifier, gnd_train) | ||
classifier.train() | ||
label_pred = classifier.apply(fea_test) | ||
if gnd_test is not None: | ||
evaluator = MulticlassAccuracy() | ||
acc = evaluator.evaluate(label_pred, gnd_test) | ||
else: | ||
acc = None | ||
|
||
return (classifier.get_num_machines(), acc) | ||
|
||
|
||
for ier in range(len(encoders)): | ||
for idr in range(len(decoders)): | ||
t_begin = time.clock() | ||
(codelen, acc) = run_ecoc(ier, idr) | ||
if acc is None: | ||
acc_fmt = 's' | ||
acc = 'N/A' | ||
else: | ||
acc_fmt = '.4f' | ||
|
||
t_elapse = time.clock() - t_begin | ||
print((format_str % ('d', '.3f', acc_fmt)) % | ||
(encoders[ier][4:-7], decoders[idr][4:-7], codelen, t_elapse, acc)) | ||
|
||
if __name__=='__main__': | ||
print('MulticlassECOC') | ||
classifier_multiclass_ecoc(*parameter_list[0]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.