| 13 | import com.winvector.variables.LevelVectors.VectorRow; |
| 14 | |
| 15 | public final class VariableEncodings implements Serializable { |
| 16 | private static final long serialVersionUID = 1L; |
| 17 | |
| 18 | // definitional |
| 19 | private final PrimaVariableInfo def; |
| 20 | private final boolean useIntercept; |
| 21 | // weight control |
| 22 | public final String weightKey; |
| 23 | // derived inputs |
| 24 | public final int vdim; |
| 25 | public final ArrayList<VariableMapping> adaptions = new ArrayList<VariableMapping>(); |
| 26 | // derived results |
| 27 | public final SortedMap<String,Integer> outcomeCategories = new TreeMap<String,Integer>(); |
| 28 | private final ArrayList<String> outcomeNames = new ArrayList<String>(); |
| 29 | |
| 30 | public VariableEncodings(final PrimaVariableInfo def, final boolean useIntercept, final String weightKey, |
| 31 | final Map<String,Map<String,VectorRow>> vectorEncodings) { |
| 32 | this.def = def; |
| 33 | this.useIntercept = useIntercept; |
| 34 | this.weightKey = weightKey; |
| 35 | // encode variables |
| 36 | int adapterDim = 0; |
| 37 | if(useIntercept) { |
| 38 | final VariableMapping adaption = new ConstRecord(adapterDim); |
| 39 | adaptions.add(adaption); |
| 40 | adapterDim += adaption.indexR() - adaption.indexL(); |
| 41 | } |
| 42 | for(final String ni: new TreeSet<String>(def.numericColumnSet.keySet())) { |
| 43 | final VariableMapping adaption = new NumericVariable(ni,adapterDim); |
| 44 | adaptions.add(adaption); |
| 45 | adapterDim += adaption.indexR() - adaption.indexL(); |
| 46 | } |
| 47 | //final Random rand = new Random(15135); |
| 48 | for(final String ci: new TreeSet<String>(def.catLevels.keySet())) { |
| 49 | final VariableMapping adaption; |
| 50 | if((vectorEncodings!=null)&&(vectorEncodings.containsKey(ci))) { |
| 51 | adaption = new LevelVectors(ci,adapterDim,vectorEncodings.get(ci)); |
| 52 | } else { |
| 53 | final CountMap<String> levels = def.catLevels.get(ci); |
| 54 | adaption = new LevelIndicators(ci,adapterDim,levels.keySet()); |
| 55 | } |
| 56 | adaptions.add(adaption); |
| 57 | adapterDim += adaption.indexR() - adaption.indexL(); |
| 58 | } |
| 59 | vdim = adapterDim; |
| 60 | // encode outcome |
| 61 | for(final String oci: new TreeSet<String>(def.outcomes.keySet())) { |
| 62 | outcomeCategories.put(oci,outcomeCategories.size()); |
| 63 | outcomeNames.add(oci); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public VariableEncodings(final PrimaVariableInfo def, final boolean useIntercept, final String weightKey) { |
| 68 | this(def,useIntercept,weightKey,null); |
| 69 | } |
| 70 | |
| 71 | public PrimaVariableInfo def() { |
| 72 | return def; |
nothing calls this directly
no outgoing calls
no test coverage detected