/*
* Apache License
* Version 2.0, January 2004
* http://www.apache.org/licenses/
*
* Copyright 2013 Aurelian Tutuianu
* Copyright 2014 Aurelian Tutuianu
* Copyright 2015 Aurelian Tutuianu
* Copyright 2016 Aurelian Tutuianu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package rapaio.ml.regression.simple;
import rapaio.core.stat.Mean;
import rapaio.data.Frame;
import rapaio.data.Var;
import rapaio.data.VarType;
import rapaio.ml.common.Capabilities;
import rapaio.ml.regression.AbstractRegression;
import rapaio.ml.regression.RFit;
import rapaio.ml.regression.Regression;
/**
* User: Aurelian Tutuianu <padreati@yahoo.com>
*/
public class L2Regression extends AbstractRegression {
private static final long serialVersionUID = -8666168876139028337L;
public static L2Regression create() {
return new L2Regression();
}
private double[] means;
private L2Regression() {
}
@Override
public L2Regression newInstance() {
return new L2Regression();
}
@Override
public String name() {
return "L2Regression";
}
@Override
public String fullName() {
return name();
}
@Override
public Capabilities capabilities() {
return new Capabilities()
.withInputCount(0, 1_000_000)
.withTargetCount(1, 1)
.withInputTypes(VarType.NUMERIC, VarType.ORDINAL, VarType.BINARY, VarType.INDEX, VarType.NOMINAL, VarType.STAMP, VarType.TEXT)
.withTargetTypes(VarType.NUMERIC)
.withAllowMissingInputValues(true)
.withAllowMissingTargetValues(true);
}
@Override
protected boolean coreTrain(Frame df, Var weights) {
means = new double[targetNames().length];
for (int i = 0; i < targetNames().length; i++) {
double mean = Mean.from(df.var(targetName(i))).value();
means[i] = mean;
}
return true;
}
@Override
protected RFit coreFit(final Frame df, final boolean withResiduals) {
RFit pred = RFit.build(this, df, withResiduals);
for (int i = 0; i < targetNames().length; i++) {
double mean = means[i];
pred.fit(targetName(i)).stream().forEach(s -> s.setValue(mean));
}
pred.buildComplete();
return pred;
}
@Override
public String summary() {
StringBuilder sb = new StringBuilder();
sb.append(name()).append(" Summary\n");
sb.append("=========================\n");
sb.append("TODO: complete\n");
return sb.toString();
}
}