function [trconf tsconf cl] = train_weka_classif_affective( train, test, classf, varargin ) %TRAIN_WEKA_CLASSF Train and test a Weka classifier on a data set % [TRCONF TSCONF CL] = TRAIN_WEKA_CLASSF( TRAIN, TEST, CLASSF, ARG1, ARG2... ) % [TRCONF CL] = TRAIN_WEKA_CLASSF( TRAIN, [], CLASSF, ARG1, ARG2... ) % CL = TRAIN_WEKA_CLASSF( TRAIN, [], CLASSF, ARG1, ARG2... ) % TRAIN '.arff' file name for the training set, or a weka.core.Instances % object % TEST '.arff' file name for the test set % CLASSF Full name of the classifier Java class ('weka.classifiers...') % ARG1... Additional parameters to the classifier; strings. % % TRCONF Confusion matrix for the training set. Columns are % classification, rows are the true class. % TSCONF Confusion matrix for the test set. % CL Trained classifier if nargin < 3 error( 'Not enough arguments' ) end dotest = ~isempty( test ); doeval = dotest || (~dotest && nargout > 1); if (~dotest && nargout > 2) || (dotest && nargout > 3) error( 'Too many output arguments' ) end cl = javaObject( classf ); if length(varargin) > 0 cl.setOptions( varargin{1} ) end if ischar( train ) tdata = weka.core.Instances( java.io.BufferedReader( java.io.FileReader( train ) ) ); tdata.setClassIndex( tdata.numAttributes() - 1 ); % Class is the last attribute elseif isjava( train ) && isa( train, 'weka.core.Instances' ) tdata = train; else error( 'Invalid type of first argument' ) end cl.buildClassifier( tdata ) if doeval evtr = weka.classifiers.Evaluation( weka.core.Instances( tdata, 0 ), [] ); evtr.setPriors( tdata ) if dotest evts = weka.classifiers.Evaluation( weka.core.Instances( tdata, 0 ), [] ); evts.setPriors( tdata ) end evtr.evaluateModel( cl, tdata ); trconf = evtr.confusionMatrix(); clear evtr if dotest tdata = weka.core.Instances( java.io.BufferedReader( java.io.FileReader( test ) ) ); tdata.setClassIndex( tdata.numAttributes() - 1 ); %% Class is the last attribute evts.evaluateModel( cl, tdata ); tsconf = evts.confusionMatrix(); else tsconf = cl; end else trconf = cl; end end % train_weka_classif_affective()