本文整理了Java中water.fvec.Frame.replace()
方法的一些代码示例,展示了Frame.replace()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Frame.replace()
方法的具体详情如下:
包路径:water.fvec.Frame
类名称:Frame
方法名:replace
暂无
代码示例来源:origin: h2oai/h2o-2
public Vec factor(int col) {
Vec nv = vecs()[col].toEnum();
return replace(col, nv);
}
代码示例来源:origin: h2oai/h2o-3
public Vec setWeights(String name, Vec vec) {
if(_weights)
return _adaptedFrame.replace(weightChunkId(),vec);
_adaptedFrame.insertVec(weightChunkId(),name,vec);
_weights = true;
return null;
}
代码示例来源:origin: h2oai/h2o-2
@Override
protected Response serve() {
try {
if (column_index <= 0) throw new IllegalArgumentException("Column index is 1 based. Please supply a valid column index in the range [1,"+ src_key.numCols()+"]");
Log.info("Factorizing column " + column_index);
Vec nv = src_key.vecs()[column_index - 1].toEnum();
src_key.replace(column_index - 1, nv);
} catch( Throwable e ) {
return Response.error(e);
}
return Inspect2.redirect(this, src_key._key.toString());
}
}
代码示例来源:origin: h2oai/h2o-3
private void convert2Enum(Frame f, int[] cols) {
for (int col : cols) {
f.replace(col, f.vec(col).toCategoricalVec()).remove();
}
DKV.put(f);
}
代码示例来源:origin: h2oai/h2o-2
@Override protected void execImpl() {
Vec va = null;
try {
va = vactual.toEnum(); // always returns TransfVec
actual_domain = va._domain;
if (max_k > predict.numCols()-1) {
Log.warn("Reducing Hitratio Top-K value to maximum value allowed: " + String.format("%,d", predict.numCols() - 1));
max_k = predict.numCols() - 1;
}
final Frame actual_predict = new Frame(predict.names().clone(), predict.vecs().clone());
actual_predict.replace(0, va); // place actual labels in first column
hit_ratios = new HitRatioTask(max_k, seed).doAll(actual_predict).hit_ratios();
} finally { // Delete adaptation vectors
if (va!=null) UKV.remove(va._key);
}
}
代码示例来源:origin: h2oai/h2o-3
@Override
public String[] adaptTestForTrain(Frame test, boolean expensive, boolean computeMetrics) {
boolean createStrataVec = _parms.isStratified() && (test.vec(_parms._strata_column) == null);
if (createStrataVec) {
Vec strataVec = test.anyVec().makeCon(Double.NaN);
_toDelete.put(strataVec._key, "adapted missing strata vector");
test.add(_parms._strata_column, strataVec);
}
String[] msgs = super.adaptTestForTrain(test, expensive, computeMetrics);
if (createStrataVec) {
Vec strataVec = CoxPH.StrataTask.makeStrataVec(test, _parms._stratify_by, _output._strataMap);
_toDelete.put(strataVec._key, "adapted missing strata vector");
test.replace(test.find(_parms._strata_column), strataVec);
if (_output._strataOnlyCols != null)
test.remove(_output._strataOnlyCols);
}
return msgs;
}
代码示例来源:origin: h2oai/h2o-3
int prep(Frame fr) {
fr.remove("ID").remove(); // Remove not-predictive ID
int ci = fr.find("RACE"); // Change RACE to categorical
Scope.track(fr.replace(ci,fr.vecs()[ci].toCategoricalVec()));
return fr.find("CAPSULE"); // Prostate: predict on CAPSULE
}
}, false, DistributionFamily.bernoulli);
代码示例来源:origin: h2oai/h2o-2
@Override
protected Response serve() {
try {
if (column_index <= 0 || column_index > src_key.numCols()) throw new IllegalArgumentException("Column index is 1 based. Please supply a valid column index in the range [1,"+ src_key.numCols()+"]");
Log.info("Integerizing column " + column_index);
Vec nv;
if ((nv= src_key.vecs()[column_index-1].masterVec()) == null) {
assert src_key.vecs()[column_index-1].isInt();
nv = src_key.vecs()[column_index-1];
nv._domain = null;
} else {
assert src_key.vecs()[column_index - 1].masterVec().isInt();
nv = src_key.vecs()[column_index - 1].masterVec();
}
src_key.replace(column_index - 1, nv);
} catch( Throwable e ) {
return Response.error(e);
}
return Inspect2.redirect(this, src_key._key.toString());
}
}
代码示例来源:origin: h2oai/h2o-3
private GBMModel trainGbm(final int ntrees) {
Frame f = Scope.track(parse_test_file("smalldata/logreg/prostate.csv"));
final String response = "CAPSULE";
f.replace(f.find(response), f.vec(response).toCategoricalVec()).remove();
DKV.put(f._key, f);
GBMModel.GBMParameters gbmParams = new GBMModel.GBMParameters();
gbmParams._seed = 123;
gbmParams._train = f._key;
gbmParams._ignored_columns = new String[]{"ID"};
gbmParams._response_column = response;
gbmParams._ntrees = ntrees;
gbmParams._score_each_iteration = true;
return(GBMModel) Scope.track_generic(new GBM(gbmParams).trainModel().get());
}
代码示例来源:origin: h2oai/h2o-3
@BeforeClass
public static void setup() {
stall_till_cloudsize(1);
_covtype = parse_test_file("smalldata/covtype/covtype.20k.data");
_covtype.replace(_covtype.numCols()-1,_covtype.lastVec().toCategoricalVec()).remove();
Key[] keys = new Key[]{Key.make("train"),Key.make("test")};
H2O.submitTask(new FrameSplitter(_covtype, new double[]{.8},keys,null)).join();
_train = DKV.getGet(keys[0]);
_test = DKV.getGet(keys[1]);
}
代码示例来源:origin: h2oai/h2o-3
private GBMModel prepareGBMModel(String dataset, String[] ignoredColumns, String response, boolean classification, int ntrees) {
Frame f = parse_test_file(dataset);
try {
if (classification && !f.vec(response).isCategorical()) {
f.replace(f.find(response), f.vec(response).toCategoricalVec()).remove();
DKV.put(f._key, f);
}
GBMModel.GBMParameters gbmParams = new GBMModel.GBMParameters();
gbmParams._train = f._key;
gbmParams._ignored_columns = ignoredColumns;
gbmParams._response_column = response;
gbmParams._ntrees = ntrees;
gbmParams._score_each_iteration = true;
return new GBM(gbmParams).trainModel().get();
} finally {
if (f!=null) f.delete();
}
}
代码示例来源:origin: h2oai/h2o-3
private DRFModel prepareDRFModel(String dataset, String[] ignoredColumns, String response, boolean classification, int ntrees) {
Frame f = parse_test_file(dataset);
try {
if (classification && !f.vec(response).isCategorical()) {
f.replace(f.find(response), f.vec(response).toCategoricalVec()).remove();
DKV.put(f._key, f);
}
DRFModel.DRFParameters drfParams = new DRFModel.DRFParameters();
drfParams._train = f._key;
drfParams._ignored_columns = ignoredColumns;
drfParams._response_column = response;
drfParams._ntrees = ntrees;
drfParams._score_each_iteration = true;
return new DRF(drfParams).trainModel().get();
} finally {
if (f!=null) f.delete();
}
}
代码示例来源:origin: h2oai/h2o-3
@Test
public void testMonotoneConstraintsInverse() {
Scope.enter();
try {
final String response = "power (hp)";
Frame f = parse_test_file("smalldata/junit/cars.csv");
f.replace(f.find(response), f.vecs()[f.find("cylinders")].toNumericVec()).remove();
DKV.put(Scope.track(f));
GBMModel.GBMParameters parms = new GBMModel.GBMParameters();
parms._response_column = response;
parms._train = f._key;
parms._ignored_columns = new String[]{"name"};
parms._seed = 42;
GBMModel.GBMParameters noConstrParams = (GBMModel.GBMParameters) parms.clone();
GBMModel noConstrModel = new GBM(noConstrParams).trainModel().get();
Scope.track_generic(noConstrModel);
assertTrue(noConstrModel._output._varimp.toMap().get("cylinders") > 0);
GBMModel.GBMParameters constrParams = (GBMModel.GBMParameters) parms.clone();
constrParams._monotone_constraints = new KeyValue[] {new KeyValue("cylinders", -1)};
GBMModel constrModel = new GBM(constrParams).trainModel().get();
Scope.track_generic(constrModel);
// we essentially eliminated the effect of the feature by setting an inverted constraint
assertEquals(constrModel._output._varimp.toMap().get("cylinders"), 0, 0);
} finally {
Scope.exit();
}
}
代码示例来源:origin: h2oai/h2o-3
@Test public void testCovtype() throws InterruptedException, ExecutionException {
NaiveBayesModel model = null;
Frame train = null, score = null;
try {
Scope.enter();
train = parse_test_file(Key.make("covtype.hex"), "smalldata/covtype/covtype.20k.data");
Scope.track(train.replace(54, train.vecs()[54].toCategoricalVec())); // Change response to categorical
DKV.put(train);
NaiveBayesParameters parms = new NaiveBayesParameters();
parms._train = train._key;
parms._laplace = 0;
parms._response_column = train._names[54];
parms._compute_metrics = false;
model = new NaiveBayes(parms).trainModel().get();
// Done building model; produce a score column with class assignments
score = model.score(train);
Assert.assertTrue(model.testJavaScoring(train,score,1e-6));
} finally {
if (train != null) train.delete();
if (score != null) score.delete();
if (model != null) model.delete();
Scope.exit();
}
}
}
代码示例来源:origin: h2oai/h2o-3
@Test @Ignore public void testConstantColumns(){
GLMModel model1 = null, model2 = null, model3 = null, model4 = null;
Frame fr = parse_test_file(Key.make("Airlines"), "smalldata/airlines/allyears2k_headers.zip");
Vec y = fr.vec("IsDepDelayed").makeCopy(null);
fr.replace(fr.find("IsDepDelayed"),y).remove();
Vec weights = fr.anyVec().makeZero();
new MRTask(){
@Override public void map(Chunk c){
int i = 0;
for(i = 0; i < c._len; ++i){
long rid = c.start()+i;
if(rid >= 1999) break;
c.set(i,1);
}
}
}.doAll(weights);
fr.add("weights", weights);
DKV.put(fr);
GLMParameters parms = new GLMParameters(Family.gaussian);
parms._train = fr._key;
parms._weights_column = "weights";
parms._lambda_search = true;
parms._alpha = new double[]{0};
parms._response_column = "IsDepDelayed";
parms._ignored_columns = new String[]{"DepTime", "ArrTime", "Cancelled", "CancellationCode", "DepDelay", "Diverted", "CarrierDelay", "WeatherDelay", "NASDelay", "SecurityDelay", "LateAircraftDelay", "IsArrDelayed"};
parms._standardize = true;
model1 = new GLM(parms).trainModel().get();
model1.delete();
fr.delete();
}
代码示例来源:origin: h2oai/h2o-3
@Test
public void imputeWithMeanTest() {
fr = new TestFrameBuilder()
.withName("testFrame")
.withColNames("ColA")
.withVecTypes(Vec.T_STR)
.withDataForCol(0, ar("1", "2", null))
.build();
String[] teColumns = {""};
TargetEncoder tec = new TargetEncoder(teColumns);
// We have to do this trick because we cant initialize array with `null` values.
Vec strVec = fr.vec("ColA");
Vec numericVec = strVec.toNumericVec();
fr.replace(0, numericVec);
Frame withImputed = tec.imputeWithMean(fr, 0, 1.5);
Vec expected = dvec(1, 2, 1.5);
Vec resultVec = withImputed.vec(0);
assertVecEquals(expected, resultVec, 1e-5);
expected.remove();
strVec.remove();
resultVec.remove();
withImputed.delete();
numericVec.remove();
}
代码示例来源:origin: h2oai/h2o-3
Scope.track(fr2.replace(ci, fr2.vecs()[ci].toCategoricalVec())); // Convert response 'Angaus' to categorical
DKV.put(fr2);
parms._train = fr2._key;
代码示例来源:origin: h2oai/h2o-3
@Test public void testProstate() throws InterruptedException, ExecutionException {
NaiveBayesModel model = null;
Frame train = null, score = null;
final int[] cats = new int[]{1,3,4,5}; // Categoricals: CAPSULE, RACE, DPROS, DCAPS
try {
Scope.enter();
train = parse_test_file(Key.make("prostate.hex"), "smalldata/logreg/prostate.csv");
for(int i = 0; i < cats.length; i++)
Scope.track(train.replace(cats[i], train.vec(cats[i]).toCategoricalVec()));
train.remove("ID").remove();
DKV.put(train._key, train);
NaiveBayesParameters parms = new NaiveBayesParameters();
parms._train = train._key;
parms._laplace = 0;
parms._response_column = train._names[0];
parms._compute_metrics = true;
model = new NaiveBayes(parms).trainModel().get();
// Done building model; produce a score column with class assignments
score = model.score(train);
Assert.assertTrue(model.testJavaScoring(train,score,1e-6));
} finally {
if (train != null) train.delete();
if (score != null) score.delete();
if (model != null) model.delete();
Scope.exit();
}
}
代码示例来源:origin: h2oai/h2o-3
train = parse_test_file(Key.make("prostate.hex"), "smalldata/logreg/prostate.csv");
for(int i = 0; i < cats.length; i++)
Scope.track(train.replace(cats[i], train.vec(cats[i]).toCategoricalVec()));
train.remove("ID").remove();
DKV.put(train._key, train);
代码示例来源:origin: h2oai/h2o-3
@Test public void testGBMPredict() {
GBMModel gbm = null;
GBMModel.GBMParameters parms = new GBMModel.GBMParameters();
Frame pred=null, res=null;
Scope.enter();
try {
Frame train = parse_test_file("smalldata/gbm_test/ecology_model.csv");
train.remove("Site").remove(); // Remove unique ID
int ci = train.find("Angaus");
Scope.track(train.replace(ci, train.vecs()[ci].toCategoricalVec())); // Convert response 'Angaus' to categorical
DKV.put(train); // Update frame after hacking it
parms._train = train._key;
parms._response_column = "Angaus"; // Train on the outcome
parms._distribution = DistributionFamily.multinomial;
gbm = new GBM(parms).trainModel().get();
pred = parse_test_file("smalldata/gbm_test/ecology_eval.csv" );
pred.remove("Angaus").remove(); // No response column during scoring
res = gbm.score(pred);
// Build a POJO, validate same results
Assert.assertTrue(gbm.testJavaScoring(pred, res, 1e-15));
} finally {
parms._train.remove();
if( gbm != null ) gbm .delete();
if( pred != null ) pred.remove();
if( res != null ) res .remove();
Scope.exit();
}
}
内容来源于网络,如有侵权,请联系作者删除!