-
Notifications
You must be signed in to change notification settings - Fork 2
/
wekaCluster.m
90 lines (72 loc) · 2.67 KB
/
wekaCluster.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function C = wekaCluster( data, clusterer, options )
%WEKACLUSTER Performs clustering on Weka data
% C = WEKACLUSTER(DATA, CLUSTERER, OPTIONS) returns a weka.clusterers.*
% Java object that has been built on DATA using the supplied
% CLUSTERER.
%
% DATA A weka.core.Instances object holding the data to be
% clustered.
%
% CLUSTERER Name or classpath to a valid Weka clusterer, e.g.
% 'SimpleKMeans' or 'weka.clusterers.SimpleKMeans'.
%
% OPTIONS (Optional) string or cell array of strings with options specific
% to the clusterer. See the Weka documentation for further
% detail.
%
% Notes:
% Weka clusterers will throw an error if class attribute is present,
% the following code will remove any class attribute:
% D = wekaApplyFilter(D, 'unsupervised.attribute.Remove', {'-R', num2str(D.classIndex+1)});
%
% Examples:
%
% % K-means clustering with K=3
% KM = wekaCluster(D, 'SimpleKMeans', '-N 3');
%
% % Expectation Maximization with verbose output
% EM = wekaCluster(D, 'EM', '-V');
%
% See also WEKACLASSIFY
%% Parse inputs
if nargin < 2
error('WEKALAB:wekaCluster:IncorrectArguments', 'Insufficient arguments supplied.');
elseif nargin > 3
error('WEKALAB:wekaCluster:IncorrectArguments', 'Too many arguments supplied.');
end
% Set defaults
if ~exist('options', 'var')
options = [];
end
% Check that data is correct object
if ~isa(data, 'weka.core.Instances')
error('WEKALAB:wekaCluster:WrongFormat', 'Data argument must be a weka.core.Instances Java object.');
end
% Check that data has no class attribute (otherwise clusterer will throw
% error anyway)
if data.classIndex ~= -1
error('WEKALAB:wekaCluster:DataHasClassAttribute', 'Data argument has class attribute which must be removed prior to clustering. (See help ''wekaCluster'' notes)');
end
% Check clusterer string
if ischar(clusterer)
if isempty(strfind(clusterer, 'weka.clusterers'))
clusterer = ['weka.clusterers.', clusterer];
end
else
error('WEKALAB:wekaCluster:InvalidArgument', 'Clusterer argument must be a string.');
end
% Check clusterer options and convert to cellstr
if ~isempty(options)
if ischar(options)
options = stringsplit(options, ' ');
elseif ~iscellstr(options)
error('WEKALAB:wekaFilter:InvalidArgument', 'Clusterer options argument must be a string or cellstr.');
end
end
%% Code
C = javaObject(clusterer);
if ~isempty(options)
C.setOptions(options);
end
C.buildClusterer(data);
end