-
Notifications
You must be signed in to change notification settings - Fork 2
/
wekaApplyFilter.m
73 lines (63 loc) · 2.58 KB
/
wekaApplyFilter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function [filteredData,filter] = wekaApplyFilter( data, filter, options )
%WEKAAPPLYFILTER Apply a Weka filter to an Instances object.
% E = WEKAAPPLYFILTER(DATA, FILTER) Applies the named filter to the data D.
%
% E = WEKAAPPLYFILTER(DATA, FILTER, OPTIONS) Applies the named filter or weka.filters.*
% object FILTER to DATA with the supplied OPTIONS.
%
% [E, FILTER] = WEKAAPPLYFILTER(DATA, FILTER, OPTIONS) Applies the named filter or weka.filters.*
% object FILTER to DATA with the supplied OPTIONS, returning the filter
% used. Useful if only a named filter is supplied.
%
% DATA A weka.core.Instances object holding the data to be filtered.
%
% FILTER A weka.filters.* object OR a classpath to valid Weka filter.
% e.g. 'weka.filters.supervised.instance.Resample'
% NOTE: The 'weka.filters.' is optional.
%
% OPTIONS (Optional) string containing the options for FILTER. See Weka
% documentation for further detail.
%
% Examples:
%
% % Apply resample filter with options -S <randomseed> and -Z <resample percentage>
% filteredData = wekaApplyFilter(wekaData, 'supervised.instance.Resample', '-S 1014 -Z 200')
%
% % Apply resample filter using predefined filter object
% myFilter = wekaFilter('supervised.instance.Resample', '-Z 200');
% filteredData = wekaApplyFilter(wekaData, myFilter);
%
% % Standardize data to zero mean and unit variance
% filteredData = wekaApplyFilter(wekaData, 'unsupervised.attribute.Standardize');
%
% See also WEKAFILTER
%% Parse and sanitize input arguments
if nargin < 2
error('WEKALAB:wekaApplyFilter:IncorrectArguments', 'Insufficient arguments supplied.');
elseif nargin > 3
error('WEKALAB:wekaApplyFilter:IncorrectArguments', 'Too many arguments supplied.');
end
if nargin == 2
options = [];
end
% Check that data is correct object
if ~isa(data, 'weka.core.Instances')
error('WEKALAB:wekaApplyFilter:WrongFormat', 'Data argument must be a weka.core.Instances Java object.');
end
% Check type string
if ischar(filter)
if isempty(strfind(filter, 'weka.filters'))
filter = ['weka.filters.', filter];
end
elseif isempty(regexp(class(filter), 'weka.filters', 'once'))
error('WEKALAB:wekaApplyFilter:InvalidArgument', 'Filter argument is ''%s'' instead of a string or weka.filters.* object', class(model));
end
%% Code
if ischar(filter)
filterObj = wekaFilter(filter, options);
else
filterObj = filter;
end
filterObj.setInputFormat(data);
filteredData = filterObj.useFilter(data, filterObj);
end