1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * NormalizedPolyKernel.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.classifiers.functions.supportVector; |
---|
24 | |
---|
25 | import weka.core.Instance; |
---|
26 | import weka.core.Instances; |
---|
27 | import weka.core.RevisionUtils; |
---|
28 | |
---|
29 | /** |
---|
30 | <!-- globalinfo-start --> |
---|
31 | * The normalized polynomial kernel.<br/> |
---|
32 | * K(x,y) = <x,y>/sqrt(<x,x><y,y>) where <x,y> = PolyKernel(x,y) |
---|
33 | * <p/> |
---|
34 | <!-- globalinfo-end --> |
---|
35 | * |
---|
36 | <!-- options-start --> |
---|
37 | * Valid options are: <p/> |
---|
38 | * |
---|
39 | * <pre> -D |
---|
40 | * Enables debugging output (if available) to be printed. |
---|
41 | * (default: off)</pre> |
---|
42 | * |
---|
43 | * <pre> -no-checks |
---|
44 | * Turns off all checks - use with caution! |
---|
45 | * (default: checks on)</pre> |
---|
46 | * |
---|
47 | * <pre> -C <num> |
---|
48 | * The size of the cache (a prime number), 0 for full cache and |
---|
49 | * -1 to turn it off. |
---|
50 | * (default: 250007)</pre> |
---|
51 | * |
---|
52 | * <pre> -E <num> |
---|
53 | * The Exponent to use. |
---|
54 | * (default: 1.0)</pre> |
---|
55 | * |
---|
56 | * <pre> -L |
---|
57 | * Use lower-order terms. |
---|
58 | * (default: no)</pre> |
---|
59 | * |
---|
60 | <!-- options-end --> |
---|
61 | * |
---|
62 | * @author Eibe Frank (eibe@cs.waikato.ac.nz) |
---|
63 | * @version $Revision: 1.8 $ |
---|
64 | */ |
---|
65 | public class NormalizedPolyKernel |
---|
66 | extends PolyKernel { |
---|
67 | |
---|
68 | /** for serialization */ |
---|
69 | static final long serialVersionUID = 1248574185532130851L; |
---|
70 | |
---|
71 | /** |
---|
72 | * default constructor - does nothing |
---|
73 | */ |
---|
74 | public NormalizedPolyKernel() { |
---|
75 | super(); |
---|
76 | |
---|
77 | setExponent(2.0); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Creates a new <code>NormalizedPolyKernel</code> instance. |
---|
82 | * |
---|
83 | * @param dataset the training dataset used. |
---|
84 | * @param cacheSize the size of the cache (a prime number) |
---|
85 | * @param exponent the exponent to use |
---|
86 | * @param lowerOrder whether to use lower-order terms |
---|
87 | * @throws Exception if something goes wrong |
---|
88 | */ |
---|
89 | public NormalizedPolyKernel(Instances dataset, int cacheSize, |
---|
90 | double exponent, boolean lowerOrder) throws Exception { |
---|
91 | |
---|
92 | super(dataset, cacheSize, exponent, lowerOrder); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Returns a string describing the kernel |
---|
97 | * |
---|
98 | * @return a description suitable for displaying in the |
---|
99 | * explorer/experimenter gui |
---|
100 | */ |
---|
101 | public String globalInfo() { |
---|
102 | return |
---|
103 | "The normalized polynomial kernel.\n" |
---|
104 | + "K(x,y) = <x,y>/sqrt(<x,x><y,y>) where <x,y> = PolyKernel(x,y)"; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Computes the result of the kernel function for two instances. |
---|
109 | * If id1 == -1, eval use inst1 instead of an instance in the dataset. |
---|
110 | * Redefines the eval function of PolyKernel. |
---|
111 | * |
---|
112 | * @param id1 the index of the first instance in the dataset |
---|
113 | * @param id2 the index of the second instance in the dataset |
---|
114 | * @param inst1 the instance corresponding to id1 (used if id1 == -1) |
---|
115 | * @return the result of the kernel function |
---|
116 | * @throws Exception if something goes wrong |
---|
117 | */ |
---|
118 | public double eval(int id1, int id2, Instance inst1) |
---|
119 | throws Exception { |
---|
120 | |
---|
121 | double div = Math.sqrt(super.eval(id1, id1, inst1) * ((m_keys != null) |
---|
122 | ? super.eval(id2, id2, m_data.instance(id2)) |
---|
123 | : super.eval(-1, -1, m_data.instance(id2)))); |
---|
124 | |
---|
125 | if(div != 0){ |
---|
126 | return super.eval(id1, id2, inst1) / div; |
---|
127 | } else { |
---|
128 | return 0; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Sets the exponent value (must be different from 1.0). |
---|
134 | * |
---|
135 | * @param value the exponent value |
---|
136 | */ |
---|
137 | public void setExponent(double value) { |
---|
138 | if (value != 1.0) |
---|
139 | super.setExponent(value); |
---|
140 | else |
---|
141 | System.out.println("A linear kernel, i.e., Exponent=1, is not possible!"); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * returns a string representation for the Kernel |
---|
146 | * |
---|
147 | * @return a string representaiton of the kernel |
---|
148 | */ |
---|
149 | public String toString() { |
---|
150 | String result; |
---|
151 | |
---|
152 | if (getUseLowerOrder()) |
---|
153 | result = "Normalized Poly Kernel with lower order: K(x,y) = (<x,y>+1)^" + getExponent() + "/" + |
---|
154 | "((<x,x>+1)^" + getExponent() + "*" + "(<y,y>+1)^" + getExponent() + ")^(1/2)"; |
---|
155 | else |
---|
156 | result = "Normalized Poly Kernel: K(x,y) = <x,y>^" + getExponent() + "/" + "(<x,x>^" + |
---|
157 | getExponent() + "*" + "<y,y>^" + getExponent() + ")^(1/2)"; |
---|
158 | |
---|
159 | return result; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Returns the revision string. |
---|
164 | * |
---|
165 | * @return the revision |
---|
166 | */ |
---|
167 | public String getRevision() { |
---|
168 | return RevisionUtils.extract("$Revision: 1.8 $"); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|