A transform for applying a polynomial transformation on the data set. As the dimension of the data set grows, the number of new features created by a polynomial transform grows rapidly. It is recommended only for small dimension problems using small degrees. @author Edward Raff
| 15 | * @author Edward Raff |
| 16 | */ |
| 17 | public class PolynomialTransform implements DataTransform |
| 18 | { |
| 19 | |
| 20 | private static final long serialVersionUID = -5332216444253168283L; |
| 21 | private int degree; |
| 22 | |
| 23 | /** |
| 24 | * Creates a new polynomial transform of the given degree |
| 25 | * @param degree the degree of the polynomial |
| 26 | * @throws ArithmeticException if the degree is not greater than 1 |
| 27 | */ |
| 28 | public PolynomialTransform(int degree) |
| 29 | { |
| 30 | if(degree < 2) |
| 31 | throw new ArithmeticException("The degree of the polynomial was a nonsense value: " + degree); |
| 32 | this.degree = degree; |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public void fit(DataSet data) |
| 37 | { |
| 38 | //no-op, nothing needs to be done |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public DataPoint transform(DataPoint dp) |
| 43 | { |
| 44 | Vec x = dp.getNumericalValues(); |
| 45 | int[] setTo = new int[x.length()]; |
| 46 | |
| 47 | //TODO compute final size directly isntead of doing a pre loop |
| 48 | int finalSize = 0; |
| 49 | |
| 50 | int curCount = increment(setTo, degree, 0); |
| 51 | do |
| 52 | { |
| 53 | finalSize++; |
| 54 | curCount = increment(setTo, degree, curCount); |
| 55 | } |
| 56 | while(setTo[x.length()-1] <= degree); |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | double[] newVec = new double[finalSize]; |
| 62 | Arrays.fill(newVec, 1.0); |
| 63 | int index = 0; |
| 64 | |
| 65 | Arrays.fill(setTo, 0); |
| 66 | curCount = increment(setTo, degree, 0); |
| 67 | do |
| 68 | { |
| 69 | for(int i = 0; i < setTo.length; i++) |
| 70 | if(setTo[i] > 0) |
| 71 | newVec[index] *= Math.pow(x.get(i), setTo[i]); |
| 72 | index++; |
| 73 | curCount = increment(setTo, degree, curCount); |
| 74 | } |
nothing calls this directly
no outgoing calls
no test coverage detected