largeRCRF-Java/src/main/java/ca/joeltherrien/randomforest/CovariateRow.java
Joel Therrien a5fe856857 Massive refactor; Use Iterators/Updaters when calculating difference scores for faster calculations.
Changed the covariates to be more clever with how they produce the different splits. In the future (not yet implemented) a clever GroupDifferentiator
could update the current score calculation based just on how many rows moved from one hand to the other. There were a few other changes as well;
TreeTrainer#growTree now accepts a Random as a parameter which is used throughout the entire growing process. This means it's now theoretically
possible to grow trees using a seed, so that results can be fully reproducible.
2019-01-09 21:31:27 -08:00

47 lines
1.4 KiB
Java

package ca.joeltherrien.randomforest;
import ca.joeltherrien.randomforest.covariates.Covariate;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
public class CovariateRow implements Serializable {
private final Covariate.Value[] valueArray;
@Getter
private final int id;
public <V> Covariate.Value<V> getCovariateValue(Covariate<V> covariate){
return valueArray[covariate.getIndex()];
}
@Override
public String toString(){
return "CovariateRow " + this.id;
}
public static CovariateRow createSimple(Map<String, String> simpleMap, List<Covariate> covariateList, int id){
final Covariate.Value[] valueArray = new Covariate.Value[covariateList.size()];
final Map<String, Covariate> covariateMap = new HashMap<>();
covariateList.forEach(covariate -> covariateMap.put(covariate.getName(), covariate));
simpleMap.forEach((name, valueStr) -> {
final Covariate covariate = covariateMap.get(name);
if(covariate != null){ // happens often in tests where we experiment with adding / removing covariates
valueArray[covariate.getIndex()] = covariate.createValue(valueStr);
}
});
return new CovariateRow(valueArray, id);
}
}