Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added python examples for new classes
- Loading branch information
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
examples/undocumented/python_modular/classifier_multiclassmultipleoutputliblinear_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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import classifier_multiclass_shared | ||
|
||
[traindat, label_traindat, testdat, label_testdat] = classifier_multiclass_shared.prepare_data(False) | ||
|
||
parameter_list = [[traindat,testdat,label_traindat,label_testdat,2.1,1,1e-5],[traindat,testdat,label_traindat,label_testdat,2.2,1,1e-5]] | ||
|
||
def classifier_multiclassmultipleoutputliblinear_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,label_test_multiclass=label_testdat,width=2.1,C=1,epsilon=1e-5): | ||
from shogun.Features import RealFeatures, MulticlassLabels, MulticlassMultipleOutputLabels | ||
from shogun.Classifier import MulticlassLibLinear | ||
|
||
feats_train=RealFeatures(fm_train_real) | ||
feats_test=RealFeatures(fm_test_real) | ||
|
||
labels=MulticlassLabels(label_train_multiclass) | ||
|
||
classifier = MulticlassLibLinear(C,feats_train,labels) | ||
classifier.train() | ||
|
||
label_pred = classifier.apply_multiclass_multiple_output(feats_test,2) | ||
out = label_pred.get_labels() | ||
print out | ||
return out | ||
|
||
if __name__=='__main__': | ||
print('MulticlassMultipleOutputLibLinear') | ||
classifier_multiclassmultipleoutputliblinear_modular(*parameter_list[0]) |
84 changes: 84 additions & 0 deletions
84
examples/undocumented/python_modular/modelselection_random_search_liblinear_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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Copyright (C) 2012 Sergey Lisitsyn | ||
|
||
from numpy import * | ||
from numpy.random import randn | ||
|
||
# generate some overlapping training vectors | ||
num_vectors=100 | ||
vec_distance=1 | ||
traindat=concatenate((randn(2,num_vectors)-vec_distance, | ||
randn(2,num_vectors)+vec_distance), axis=1) | ||
label_traindat=concatenate((-ones(num_vectors), ones(num_vectors))); | ||
|
||
parameter_list = [[traindat,label_traindat]] | ||
|
||
def modelselection_random_search_liblinear_modular(traindat=traindat, label_traindat=label_traindat): | ||
from shogun.Evaluation import CrossValidation, CrossValidationResult | ||
from shogun.Evaluation import ContingencyTableEvaluation, ACCURACY | ||
from shogun.Evaluation import StratifiedCrossValidationSplitting | ||
from shogun.ModelSelection import RandomSearchModelSelection | ||
from shogun.ModelSelection import ModelSelectionParameters, R_EXP | ||
from shogun.ModelSelection import ParameterCombination | ||
from shogun.Features import BinaryLabels | ||
from shogun.Features import RealFeatures | ||
from shogun.Classifier import LibLinear, L2R_L2LOSS_SVC | ||
|
||
# build parameter tree to select C1 and C2 | ||
param_tree_root=ModelSelectionParameters() | ||
c1=ModelSelectionParameters("C1"); | ||
param_tree_root.append_child(c1) | ||
c1.build_values(-2.0, 2.0, R_EXP); | ||
|
||
c2=ModelSelectionParameters("C2"); | ||
param_tree_root.append_child(c2); | ||
c2.build_values(-2.0, 2.0, R_EXP); | ||
|
||
# training data | ||
features=RealFeatures(traindat) | ||
labels=BinaryLabels(label_traindat) | ||
|
||
# classifier | ||
classifier=LibLinear(L2R_L2LOSS_SVC) | ||
|
||
# print all parameter available for modelselection | ||
# Dont worry if yours is not included but, write to the mailing list | ||
#classifier.print_modsel_params() | ||
|
||
# splitting strategy for cross-validation | ||
splitting_strategy=StratifiedCrossValidationSplitting(labels, 10) | ||
|
||
# evaluation method | ||
evaluation_criterium=ContingencyTableEvaluation(ACCURACY) | ||
|
||
# cross-validation instance | ||
cross_validation=CrossValidation(classifier, features, labels, | ||
splitting_strategy, evaluation_criterium) | ||
cross_validation.set_autolock(False) | ||
|
||
# model selection instance | ||
model_selection=RandomSearchModelSelection(param_tree_root, | ||
cross_validation,0.5) | ||
|
||
# perform model selection with selected methods | ||
#print "performing model selection of" | ||
#param_tree_root.print_tree() | ||
best_parameters=model_selection.select_model() | ||
|
||
# print best parameters | ||
#print "best parameters:" | ||
#best_parameters.print_tree() | ||
|
||
# apply them and print result | ||
best_parameters.apply_to_machine(classifier) | ||
result=cross_validation.evaluate() | ||
#result.print_result() | ||
|
||
if __name__=='__main__': | ||
print('ModelSelectionRandomSearchLibLinear') | ||
modelselection_random_search_liblinear_modular(*parameter_list[0]) |
41 changes: 41 additions & 0 deletions
41
examples/undocumented/python_modular/transfer_multitask_clustered_logistic_regression.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from numpy import array,hstack,sin,cos | ||
from numpy.random import seed, rand | ||
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]] | ||
|
||
def transfer_multitask_clustered_logistic_regression(fm_train=traindat,fm_test=testdat,label_train=label_traindat): | ||
|
||
from modshogun import BinaryLabels, RealFeatures, Task, TaskGroup, MultitaskClusteredLogisticRegression, MSG_DEBUG | ||
|
||
features = RealFeatures(hstack((traindat,sin(traindat),cos(traindat)))) | ||
labels = BinaryLabels(hstack((label_train,label_train,label_train))) | ||
|
||
n_vectors = features.get_num_vectors() | ||
task_one = Task(0,n_vectors/3) | ||
task_two = Task(n_vectors/3,2*n_vectors/3) | ||
task_three = Task(2*n_vectors/3,n_vectors) | ||
task_group = TaskGroup() | ||
task_group.append_task(task_one) | ||
task_group.append_task(task_two) | ||
task_group.append_task(task_three) | ||
|
||
mtlr = MultitaskClusteredLogisticRegression(1.0,100.0,features,labels,task_group,2) | ||
mtlr.io.set_loglevel(MSG_DEBUG) | ||
mtlr.set_tolerance(1e-3) # use 1e-2 tolerance | ||
mtlr.set_max_iter(100) | ||
mtlr.train() | ||
mtlr.set_current_task(0) | ||
print mtlr.get_w() | ||
out = mtlr.apply_regression().get_labels() | ||
|
||
return out | ||
|
||
if __name__=='__main__': | ||
print('TransferMultitaskClusteredLogisticRegression') | ||
transfer_multitask_clustered_logistic_regression(*parameter_list[0]) |