Dense sparce transform alters the vectors that store the numerical values. Based on a threshold in (0, 1), vectors will be converted from dense to sparce, sparce to dense, or left alone. @author Edward Raff
| 14 | * @author Edward Raff |
| 15 | */ |
| 16 | public class DenseSparceTransform implements DataTransform |
| 17 | { |
| 18 | |
| 19 | private static final long serialVersionUID = -1177913691660616290L; |
| 20 | private double factor; |
| 21 | |
| 22 | /** |
| 23 | * Creates a new Dense Sparce Transform. The <tt>factor</tt> gives the maximal |
| 24 | * percentage of values that may be non zero for a vector to be sparce. Any |
| 25 | * vector meeting the requirement will be converted to a sparce vector, and |
| 26 | * others made dense. If the factor is greater than or equal to 1, then all |
| 27 | * vectors will be made sparce. If less than or equal to 0, then all will |
| 28 | * be made dense. |
| 29 | * |
| 30 | * @param factor the fraction of the vectors values that may be non zero to qualify as sparce |
| 31 | */ |
| 32 | public DenseSparceTransform(double factor) |
| 33 | { |
| 34 | this.factor = factor; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void fit(DataSet data) |
| 39 | { |
| 40 | //no - op, nothing we need to learn |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public DataPoint transform(DataPoint dp) |
| 45 | { |
| 46 | Vec orig = dp.getNumericalValues(); |
| 47 | |
| 48 | final int nnz = orig.nnz(); |
| 49 | if (nnz / (double) orig.length() < factor)///make sparse |
| 50 | { |
| 51 | if(orig.isSparse())//already sparse, just return |
| 52 | return dp; |
| 53 | //else, make sparse |
| 54 | SparseVector sv = new SparseVector(orig.length(), nnz);//TODO create a constructor for this |
| 55 | for(int i = 0; i < orig.length(); i++) |
| 56 | if(orig.get(i) != 0) |
| 57 | sv.set(i, orig.get(i)); |
| 58 | |
| 59 | return new DataPoint(sv, dp.getCategoricalValues(), dp.getCategoricalData(), dp.getWeight()); |
| 60 | } |
| 61 | else//make dense |
| 62 | { |
| 63 | if(!orig.isSparse())//already dense, just return |
| 64 | return dp; |
| 65 | DenseVector dv = new DenseVector(orig.length()); |
| 66 | Iterator<IndexValue> iter = orig.getNonZeroIterator(); |
| 67 | while (iter.hasNext()) |
| 68 | { |
| 69 | IndexValue indexValue = iter.next(); |
| 70 | dv.set(indexValue.getIndex(), indexValue.getValue()); |
| 71 | } |
| 72 | return new DataPoint(dv, dp.getCategoricalValues(), dp.getCategoricalData(), dp.getWeight()); |
| 73 | } |
nothing calls this directly
no outgoing calls
no test coverage detected