This transform converts nominal feature values to numeric ones be adding a new numeric feature for each possible categorical value for each nominal feature. The numeric features will be all zeros, with only a single numeric feature having a value of "1.0" for each nominal variable. @author Edward R
| 15 | * @author Edward Raff |
| 16 | */ |
| 17 | public class NominalToNumeric implements DataTransform |
| 18 | { |
| 19 | |
| 20 | private static final long serialVersionUID = -7765605678836464143L; |
| 21 | private int origNumericalCount; |
| 22 | private CategoricalData[] categoricalData; |
| 23 | private int addedNumers; |
| 24 | |
| 25 | /** |
| 26 | * Creates a new transform to convert categorical to numeric features |
| 27 | */ |
| 28 | public NominalToNumeric() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Creates a new transform to convert categorical to numeric features for the given dataset |
| 34 | * @param dataSet the dataset to fit the transform to |
| 35 | */ |
| 36 | public NominalToNumeric(DataSet dataSet) |
| 37 | { |
| 38 | fit(dataSet); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Copy constructor |
| 43 | * @param toCopy the object to copy |
| 44 | */ |
| 45 | public NominalToNumeric(NominalToNumeric toCopy) |
| 46 | { |
| 47 | this.origNumericalCount = toCopy.origNumericalCount; |
| 48 | this.categoricalData = toCopy.categoricalData; |
| 49 | this.addedNumers = toCopy.addedNumers; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | @Override |
| 54 | public void fit(DataSet data) |
| 55 | { |
| 56 | this.origNumericalCount = data.getNumNumericalVars(); |
| 57 | this.categoricalData = data.getCategories(); |
| 58 | addedNumers = 0; |
| 59 | |
| 60 | for(CategoricalData cd : categoricalData) |
| 61 | addedNumers += cd.getNumOfCategories(); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public DataPoint transform(DataPoint dp) |
| 66 | { |
| 67 | Vec v; |
| 68 | |
| 69 | //TODO we should detect if there are going to be so many sparce spaces added by the categorical data that we should just choose a sparce vector anyway |
| 70 | if(dp.getNumericalValues().isSparse()) |
| 71 | v = new SparseVector(origNumericalCount+addedNumers); |
| 72 | else |
| 73 | v = new DenseVector(origNumericalCount+addedNumers); |
| 74 |
nothing calls this directly
no outgoing calls
no test coverage detected