mirror of https://github.com/rusefi/jzy3d-api.git
Unify line endings of checked-in files to linux-style(LF)
To re-normalize all files that have a Windows-style line ending (CRLF or \r\n) one can use the following CLI command called in the root directory of the git repo (the dot is important): git add --renormalize .
This commit is contained in:
parent
b0564e8aba
commit
fc59374756
|
@ -1,14 +1,14 @@
|
|||
# Root logger option
|
||||
log4j.rootLogger=WARN, stdout
|
||||
|
||||
# Direct log messages to stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] [%t] - %m%n
|
||||
|
||||
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss,SSS} %c{1.} [%t] : %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%-5p %c{1.} [%t] %d{HH:mm:ss,SSS}: %m%n
|
||||
# Root logger option
|
||||
log4j.rootLogger=WARN, stdout
|
||||
|
||||
# Direct log messages to stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] [%t] - %m%n
|
||||
|
||||
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss,SSS} %c{1.} [%t] : %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%-5p %c{1.} [%t] %d{HH:mm:ss,SSS}: %m%n
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
# Root logger option
|
||||
log4j.rootLogger=INFO, stdout
|
||||
|
||||
# Direct log messages to stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] [%t] - %m%n
|
||||
|
||||
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss,SSS} %c{1.} [%t] : %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%-5p %c{1.} [%t] %d{HH:mm:ss,SSS}: %m%n
|
||||
# Root logger option
|
||||
log4j.rootLogger=INFO, stdout
|
||||
|
||||
# Direct log messages to stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] [%t] - %m%n
|
||||
|
||||
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss,SSS} %c{1.} [%t] : %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%-5p %c{1.} [%t] %d{HH:mm:ss,SSS}: %m%n
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
# Root logger option
|
||||
log4j.rootLogger=INFO, WARN, stdout
|
||||
|
||||
# Direct log messages to stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] [%t] - %m%n
|
||||
|
||||
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss,SSS} %c{1.} [%t] : %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%-5p %c{1.} [%t] %d{HH:mm:ss,SSS}: %m%n
|
||||
# Root logger option
|
||||
log4j.rootLogger=INFO, WARN, stdout
|
||||
|
||||
# Direct log messages to stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] [%t] - %m%n
|
||||
|
||||
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss,SSS} %c{1.} [%t] : %m%n
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%-5p %c{1.} [%t] %d{HH:mm:ss,SSS}: %m%n
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,2 +1,2 @@
|
|||
http://archive.ics.uci.edu/ml/
|
||||
http://archive.ics.uci.edu/ml/
|
||||
kaviar: http://trac.clermont.cemagref.fr/projets/Kaviar/wiki
|
|
@ -1,22 +1,22 @@
|
|||
|
||||
|
||||
Parameter hierarchy
|
||||
by svm
|
||||
have kernel param object
|
||||
|
||||
UI
|
||||
parameters through interface
|
||||
selection of input dataset / model
|
||||
train/apply on auto grid
|
||||
|
||||
IO
|
||||
inputs
|
||||
save trained model
|
||||
remain compatible with libsvm format
|
||||
support csv format
|
||||
|
||||
|
||||
Scaling facilities
|
||||
progress monitor?
|
||||
std out?
|
||||
|
||||
|
||||
Parameter hierarchy
|
||||
by svm
|
||||
have kernel param object
|
||||
|
||||
UI
|
||||
parameters through interface
|
||||
selection of input dataset / model
|
||||
train/apply on auto grid
|
||||
|
||||
IO
|
||||
inputs
|
||||
save trained model
|
||||
remain compatible with libsvm format
|
||||
support csv format
|
||||
|
||||
|
||||
Scaling facilities
|
||||
progress monitor?
|
||||
std out?
|
||||
support vectors?
|
|
@ -1,114 +1,114 @@
|
|||
package org.jzy3d.svm.demos;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.jzy3d.chart.Chart;
|
||||
import org.jzy3d.chart.ChartLauncher;
|
||||
import org.jzy3d.chart.SwingChartLauncher;
|
||||
import org.jzy3d.chart.factories.AWTChartFactory;
|
||||
import org.jzy3d.colors.Color;
|
||||
import org.jzy3d.colors.ColorMapper;
|
||||
import org.jzy3d.colors.colormaps.ColorMapRainbow;
|
||||
import org.jzy3d.maths.BoundingBox3d;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.maths.Range;
|
||||
import org.jzy3d.maths.Rectangle;
|
||||
import org.jzy3d.plot3d.builder.Mapper;
|
||||
import org.jzy3d.plot3d.builder.concrete.OrthonormalTessellator;
|
||||
import org.jzy3d.plot3d.primitives.Scatter;
|
||||
import org.jzy3d.plot3d.primitives.Shape;
|
||||
import org.jzy3d.plot3d.rendering.canvas.Quality;
|
||||
import org.jzy3d.svm.editors.RegressionParamsEditor;
|
||||
import org.jzy3d.svm.tesselation.SvmGrid;
|
||||
import org.jzy3d.svm.tesselation.SvmMapper;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
public abstract class Abstract3dDemo {
|
||||
public static void openChart(Chart chart) {
|
||||
ChartLauncher.openChart(chart, "Svm3D ");
|
||||
}
|
||||
|
||||
public static void openChart(Chart chart, Parameters params) {
|
||||
ChartLauncher.openChart(chart, "InstantSVM & Svm3D ");
|
||||
SwingChartLauncher.openPanel(new RegressionParamsEditor(params),
|
||||
new Rectangle(100, 100, 800, 600), "Regression params");
|
||||
}
|
||||
|
||||
public static void scale(Coord3d[] pts, float xfact, float yfact, float zfact) {
|
||||
for (int i = 0; i < pts.length; i++) {
|
||||
pts[i].x *= xfact;
|
||||
pts[i].y *= yfact;
|
||||
pts[i].y *= zfact;
|
||||
}
|
||||
}
|
||||
|
||||
/****************** CHART GENERATION ********************/
|
||||
|
||||
public static Chart getRegressionChart(Coord3d[] values, Parameters parameters) {
|
||||
SvmMapper mapper = new SvmMapper(values, parameters);
|
||||
|
||||
mapper.getSvm().getParameters().print();
|
||||
return getRegressionChart(mapper, values);
|
||||
}
|
||||
|
||||
public static Chart getRegressionChart(RegressionSVM svm, RegressionInputs inputs) {
|
||||
Coord3d[] values = Conversion.toCoord3d(inputs);
|
||||
SvmMapper mapper = new SvmMapper(svm);
|
||||
return getRegressionChart(mapper, values);
|
||||
}
|
||||
|
||||
protected static Chart getRegressionChart(SvmMapper mapper, Coord3d[] values) {
|
||||
Quality q = Quality.Advanced;
|
||||
q.setSmoothPoint(true);
|
||||
Chart chart = new AWTChartFactory().newChart(q);
|
||||
|
||||
// shape
|
||||
BoundingBox3d b = Conversion.getBounds(values);
|
||||
System.out.println(b);
|
||||
final Shape s = getRingSurface(mapper, b.getXRange(), b.getYRange(), b.getZRange(), 50);
|
||||
chart.getScene().getGraph().add(s);
|
||||
|
||||
// scatter
|
||||
loadScatter(chart, values);
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
/* 3d OBJECTS GENERATION */
|
||||
|
||||
public static Scatter loadScatter(Chart chart, Coord3d[] coords) {
|
||||
Scatter scatter = new Scatter(coords, Color.GREEN.mulSelf(1.2f), 20);
|
||||
chart.getScene().getGraph().add(scatter);
|
||||
return scatter;
|
||||
}
|
||||
|
||||
public static Shape getRingSurface(Mapper mapper, Range xrange, Range yrange, Range zrange,
|
||||
int steps) {
|
||||
ColorMapper cmapper = new ColorMapper(new ColorMapRainbow(), zrange.getMin(), zrange.getMax(),
|
||||
new Color(1, 1, 1, .5f));
|
||||
RingInterpolator tesselator =
|
||||
new RingInterpolator(0, xrange.getMax(), cmapper, new Color(1f, 1f, 1f));
|
||||
SvmGrid grid = new SvmGrid(xrange, steps, yrange, steps);
|
||||
|
||||
Shape surface = (Shape) tesselator.build(grid.apply(mapper));
|
||||
surface.setFaceDisplayed(true);
|
||||
surface.setWireframeDisplayed(true);
|
||||
surface.setWireframeColor(Color.BLACK);
|
||||
return surface;
|
||||
}
|
||||
|
||||
public static Shape getSquareSurface(Chart chart, Mapper mapper, Range xrange, Range yrange,
|
||||
int steps) {
|
||||
OrthonormalTessellator tesselator = new OrthonormalTessellator();
|
||||
SvmGrid grid = new SvmGrid(xrange, steps, yrange, steps);
|
||||
Shape surface = (Shape) tesselator.build(grid.apply(mapper));
|
||||
|
||||
surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(),
|
||||
surface.getBounds().getZmax(), new Color(1, 1, 1, .5f)));
|
||||
surface.setFaceDisplayed(true);
|
||||
surface.setWireframeDisplayed(true);
|
||||
surface.setWireframeColor(Color.BLACK);
|
||||
return surface;
|
||||
}
|
||||
}
|
||||
package org.jzy3d.svm.demos;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.jzy3d.chart.Chart;
|
||||
import org.jzy3d.chart.ChartLauncher;
|
||||
import org.jzy3d.chart.SwingChartLauncher;
|
||||
import org.jzy3d.chart.factories.AWTChartFactory;
|
||||
import org.jzy3d.colors.Color;
|
||||
import org.jzy3d.colors.ColorMapper;
|
||||
import org.jzy3d.colors.colormaps.ColorMapRainbow;
|
||||
import org.jzy3d.maths.BoundingBox3d;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.maths.Range;
|
||||
import org.jzy3d.maths.Rectangle;
|
||||
import org.jzy3d.plot3d.builder.Mapper;
|
||||
import org.jzy3d.plot3d.builder.concrete.OrthonormalTessellator;
|
||||
import org.jzy3d.plot3d.primitives.Scatter;
|
||||
import org.jzy3d.plot3d.primitives.Shape;
|
||||
import org.jzy3d.plot3d.rendering.canvas.Quality;
|
||||
import org.jzy3d.svm.editors.RegressionParamsEditor;
|
||||
import org.jzy3d.svm.tesselation.SvmGrid;
|
||||
import org.jzy3d.svm.tesselation.SvmMapper;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
public abstract class Abstract3dDemo {
|
||||
public static void openChart(Chart chart) {
|
||||
ChartLauncher.openChart(chart, "Svm3D ");
|
||||
}
|
||||
|
||||
public static void openChart(Chart chart, Parameters params) {
|
||||
ChartLauncher.openChart(chart, "InstantSVM & Svm3D ");
|
||||
SwingChartLauncher.openPanel(new RegressionParamsEditor(params),
|
||||
new Rectangle(100, 100, 800, 600), "Regression params");
|
||||
}
|
||||
|
||||
public static void scale(Coord3d[] pts, float xfact, float yfact, float zfact) {
|
||||
for (int i = 0; i < pts.length; i++) {
|
||||
pts[i].x *= xfact;
|
||||
pts[i].y *= yfact;
|
||||
pts[i].y *= zfact;
|
||||
}
|
||||
}
|
||||
|
||||
/****************** CHART GENERATION ********************/
|
||||
|
||||
public static Chart getRegressionChart(Coord3d[] values, Parameters parameters) {
|
||||
SvmMapper mapper = new SvmMapper(values, parameters);
|
||||
|
||||
mapper.getSvm().getParameters().print();
|
||||
return getRegressionChart(mapper, values);
|
||||
}
|
||||
|
||||
public static Chart getRegressionChart(RegressionSVM svm, RegressionInputs inputs) {
|
||||
Coord3d[] values = Conversion.toCoord3d(inputs);
|
||||
SvmMapper mapper = new SvmMapper(svm);
|
||||
return getRegressionChart(mapper, values);
|
||||
}
|
||||
|
||||
protected static Chart getRegressionChart(SvmMapper mapper, Coord3d[] values) {
|
||||
Quality q = Quality.Advanced;
|
||||
q.setSmoothPoint(true);
|
||||
Chart chart = new AWTChartFactory().newChart(q);
|
||||
|
||||
// shape
|
||||
BoundingBox3d b = Conversion.getBounds(values);
|
||||
System.out.println(b);
|
||||
final Shape s = getRingSurface(mapper, b.getXRange(), b.getYRange(), b.getZRange(), 50);
|
||||
chart.getScene().getGraph().add(s);
|
||||
|
||||
// scatter
|
||||
loadScatter(chart, values);
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
/* 3d OBJECTS GENERATION */
|
||||
|
||||
public static Scatter loadScatter(Chart chart, Coord3d[] coords) {
|
||||
Scatter scatter = new Scatter(coords, Color.GREEN.mulSelf(1.2f), 20);
|
||||
chart.getScene().getGraph().add(scatter);
|
||||
return scatter;
|
||||
}
|
||||
|
||||
public static Shape getRingSurface(Mapper mapper, Range xrange, Range yrange, Range zrange,
|
||||
int steps) {
|
||||
ColorMapper cmapper = new ColorMapper(new ColorMapRainbow(), zrange.getMin(), zrange.getMax(),
|
||||
new Color(1, 1, 1, .5f));
|
||||
RingInterpolator tesselator =
|
||||
new RingInterpolator(0, xrange.getMax(), cmapper, new Color(1f, 1f, 1f));
|
||||
SvmGrid grid = new SvmGrid(xrange, steps, yrange, steps);
|
||||
|
||||
Shape surface = (Shape) tesselator.build(grid.apply(mapper));
|
||||
surface.setFaceDisplayed(true);
|
||||
surface.setWireframeDisplayed(true);
|
||||
surface.setWireframeColor(Color.BLACK);
|
||||
return surface;
|
||||
}
|
||||
|
||||
public static Shape getSquareSurface(Chart chart, Mapper mapper, Range xrange, Range yrange,
|
||||
int steps) {
|
||||
OrthonormalTessellator tesselator = new OrthonormalTessellator();
|
||||
SvmGrid grid = new SvmGrid(xrange, steps, yrange, steps);
|
||||
Shape surface = (Shape) tesselator.build(grid.apply(mapper));
|
||||
|
||||
surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(),
|
||||
surface.getBounds().getZmax(), new Color(1, 1, 1, .5f)));
|
||||
surface.setFaceDisplayed(true);
|
||||
surface.setWireframeDisplayed(true);
|
||||
surface.setWireframeColor(Color.BLACK);
|
||||
return surface;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,49 +1,49 @@
|
|||
package org.jzy3d.svm.demos;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionParameters;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.instantsvm.utils.LazySVM;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.plot3d.builder.concrete.SphereScatterGenerator;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
public class ConeRegressionDemo extends Abstract3dDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
RegressionInputs inputs = Conversion.toRegressionInputs(getInputs());
|
||||
Parameters params = getParams();
|
||||
|
||||
RegressionSVM svm = LazySVM.loadOrTrainRegression("generated", params, inputs);
|
||||
|
||||
System.out.println("Maybe reused a trained svm!");
|
||||
openChart(getRegressionChart(svm, inputs));
|
||||
}
|
||||
|
||||
public static Coord3d[] getInputs() {
|
||||
return Conversion.toArray(SphereScatterGenerator.generate(new Coord3d(0, 0, 0), 1, 20, true));
|
||||
}
|
||||
|
||||
public static Parameters getParams() {
|
||||
Parameters params = new RegressionParameters(0.01, 100, 0.5);
|
||||
|
||||
if (false) {
|
||||
params.getParam().svm_type = svm_parameter.EPSILON_SVR;
|
||||
params.getParam().eps = 0.01;
|
||||
params.getParam().C = 1000;
|
||||
params.getParam().probability = 1;
|
||||
params.getParam().gamma = 0.5;
|
||||
|
||||
// params.getParam().coef0 = 0.5;
|
||||
// params.getParam().degree = 10;
|
||||
// params.getParam().p = 0.00001;
|
||||
// params.getParam().gamma = 1;
|
||||
// params.getParam().shrinking = 0;
|
||||
}
|
||||
return params;
|
||||
}
|
||||
}
|
||||
package org.jzy3d.svm.demos;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionParameters;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.instantsvm.utils.LazySVM;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.plot3d.builder.concrete.SphereScatterGenerator;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
public class ConeRegressionDemo extends Abstract3dDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
RegressionInputs inputs = Conversion.toRegressionInputs(getInputs());
|
||||
Parameters params = getParams();
|
||||
|
||||
RegressionSVM svm = LazySVM.loadOrTrainRegression("generated", params, inputs);
|
||||
|
||||
System.out.println("Maybe reused a trained svm!");
|
||||
openChart(getRegressionChart(svm, inputs));
|
||||
}
|
||||
|
||||
public static Coord3d[] getInputs() {
|
||||
return Conversion.toArray(SphereScatterGenerator.generate(new Coord3d(0, 0, 0), 1, 20, true));
|
||||
}
|
||||
|
||||
public static Parameters getParams() {
|
||||
Parameters params = new RegressionParameters(0.01, 100, 0.5);
|
||||
|
||||
if (false) {
|
||||
params.getParam().svm_type = svm_parameter.EPSILON_SVR;
|
||||
params.getParam().eps = 0.01;
|
||||
params.getParam().C = 1000;
|
||||
params.getParam().probability = 1;
|
||||
params.getParam().gamma = 0.5;
|
||||
|
||||
// params.getParam().coef0 = 0.5;
|
||||
// params.getParam().degree = 10;
|
||||
// params.getParam().p = 0.00001;
|
||||
// params.getParam().gamma = 1;
|
||||
// params.getParam().shrinking = 0;
|
||||
}
|
||||
return params;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,81 +1,81 @@
|
|||
package org.jzy3d.svm.demos;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.XValResult;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionParameters;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.jzy3d.maths.Array;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
public class RingsRegressionDemo extends Abstract3dDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
RegressionInputs inputs = Conversion.toRegressionInputs(getInputs());
|
||||
Parameters params = getParams();
|
||||
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
XValResult r = svm.xval(inputs.getX(), inputs.getY(), params, 3, 0, 10000, 6, 0, 5, 6);
|
||||
System.out.println("Best parameters:" + r);
|
||||
Array.print(r.errors);
|
||||
|
||||
params.getParam().C = r.bestC;
|
||||
params.getParam().gamma = r.bestG;
|
||||
svm.train(inputs, params);
|
||||
|
||||
System.out.println("Maybe reused a trained svm!");
|
||||
openChart(getRegressionChart(svm, inputs));
|
||||
}
|
||||
|
||||
public static Coord3d[] getInputs() {
|
||||
double[] radius = {0.2, 0.5, 0.8};
|
||||
double[] height = {0, 0.5, -0.5};
|
||||
int n[] = {12, 12, 12};
|
||||
return Conversion.toArray(generate(radius, height, n));
|
||||
}
|
||||
|
||||
public static List<Coord3d> generate(double[] radius, double height[], int[] n) {
|
||||
List<Coord3d> rings = new ArrayList<Coord3d>();
|
||||
for (int i = 0; i < n.length; i++) {
|
||||
double r = radius[i];
|
||||
double h = height[i];
|
||||
double t = 0;
|
||||
double step = 2 * Math.PI / n[i];
|
||||
while (t < (2 * Math.PI)) {
|
||||
Coord3d c = new Coord3d(t, 0, r).cartesian();
|
||||
c.z += h;
|
||||
rings.add(c);
|
||||
|
||||
t += step;
|
||||
}
|
||||
}
|
||||
return rings;
|
||||
}
|
||||
|
||||
public static Parameters getParams() {
|
||||
Parameters params = new RegressionParameters(0.01, 100, 0.5);
|
||||
|
||||
if (false) {
|
||||
params.getParam().svm_type = svm_parameter.EPSILON_SVR;
|
||||
params.getParam().eps = 0.01;
|
||||
params.getParam().C = 1000;
|
||||
params.getParam().probability = 1;
|
||||
params.getParam().gamma = 0.5;
|
||||
|
||||
// params.getParam().coef0 = 0.5;
|
||||
// params.getParam().degree = 10;
|
||||
// params.getParam().p = 0.00001;
|
||||
// params.getParam().gamma = 1;
|
||||
// params.getParam().shrinking = 0;
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
package org.jzy3d.svm.demos;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.XValResult;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionParameters;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.jzy3d.maths.Array;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
public class RingsRegressionDemo extends Abstract3dDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
RegressionInputs inputs = Conversion.toRegressionInputs(getInputs());
|
||||
Parameters params = getParams();
|
||||
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
XValResult r = svm.xval(inputs.getX(), inputs.getY(), params, 3, 0, 10000, 6, 0, 5, 6);
|
||||
System.out.println("Best parameters:" + r);
|
||||
Array.print(r.errors);
|
||||
|
||||
params.getParam().C = r.bestC;
|
||||
params.getParam().gamma = r.bestG;
|
||||
svm.train(inputs, params);
|
||||
|
||||
System.out.println("Maybe reused a trained svm!");
|
||||
openChart(getRegressionChart(svm, inputs));
|
||||
}
|
||||
|
||||
public static Coord3d[] getInputs() {
|
||||
double[] radius = {0.2, 0.5, 0.8};
|
||||
double[] height = {0, 0.5, -0.5};
|
||||
int n[] = {12, 12, 12};
|
||||
return Conversion.toArray(generate(radius, height, n));
|
||||
}
|
||||
|
||||
public static List<Coord3d> generate(double[] radius, double height[], int[] n) {
|
||||
List<Coord3d> rings = new ArrayList<Coord3d>();
|
||||
for (int i = 0; i < n.length; i++) {
|
||||
double r = radius[i];
|
||||
double h = height[i];
|
||||
double t = 0;
|
||||
double step = 2 * Math.PI / n[i];
|
||||
while (t < (2 * Math.PI)) {
|
||||
Coord3d c = new Coord3d(t, 0, r).cartesian();
|
||||
c.z += h;
|
||||
rings.add(c);
|
||||
|
||||
t += step;
|
||||
}
|
||||
}
|
||||
return rings;
|
||||
}
|
||||
|
||||
public static Parameters getParams() {
|
||||
Parameters params = new RegressionParameters(0.01, 100, 0.5);
|
||||
|
||||
if (false) {
|
||||
params.getParam().svm_type = svm_parameter.EPSILON_SVR;
|
||||
params.getParam().eps = 0.01;
|
||||
params.getParam().C = 1000;
|
||||
params.getParam().probability = 1;
|
||||
params.getParam().gamma = 0.5;
|
||||
|
||||
// params.getParam().coef0 = 0.5;
|
||||
// params.getParam().degree = 10;
|
||||
// params.getParam().p = 0.00001;
|
||||
// params.getParam().gamma = 1;
|
||||
// params.getParam().shrinking = 0;
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,37 +1,37 @@
|
|||
package org.jzy3d.svm.editors;
|
||||
|
||||
import java.awt.Component;
|
||||
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class EditorToolkit {
|
||||
public static JSlider createSlider(final String title, int min, int max) {
|
||||
final JSlider slider = new JSlider();
|
||||
// slider.setBorder(BorderFactory.createTitledBorder(title));
|
||||
Component[] c = slider.getComponents();
|
||||
for (Component ci : c) {
|
||||
System.out.println(ci);
|
||||
}
|
||||
slider.setMinimum(min);
|
||||
slider.setMaximum(max);
|
||||
slider.setMajorTickSpacing(20);
|
||||
slider.setMinorTickSpacing(5);
|
||||
slider.setPaintTicks(true);
|
||||
slider.setPaintLabels(true);
|
||||
/*
|
||||
* slider.addChangeListener(new ChangeListener() {
|
||||
*
|
||||
* @Override public void stateChanged(ChangeEvent e) { System.out.println(title + ": " +
|
||||
* slider.getValue()); }
|
||||
*
|
||||
* });
|
||||
*/
|
||||
return slider;
|
||||
}
|
||||
|
||||
public static JTextField createTextField(String content) {
|
||||
JTextField field = new JTextField(content);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
package org.jzy3d.svm.editors;
|
||||
|
||||
import java.awt.Component;
|
||||
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class EditorToolkit {
|
||||
public static JSlider createSlider(final String title, int min, int max) {
|
||||
final JSlider slider = new JSlider();
|
||||
// slider.setBorder(BorderFactory.createTitledBorder(title));
|
||||
Component[] c = slider.getComponents();
|
||||
for (Component ci : c) {
|
||||
System.out.println(ci);
|
||||
}
|
||||
slider.setMinimum(min);
|
||||
slider.setMaximum(max);
|
||||
slider.setMajorTickSpacing(20);
|
||||
slider.setMinorTickSpacing(5);
|
||||
slider.setPaintTicks(true);
|
||||
slider.setPaintLabels(true);
|
||||
/*
|
||||
* slider.addChangeListener(new ChangeListener() {
|
||||
*
|
||||
* @Override public void stateChanged(ChangeEvent e) { System.out.println(title + ": " +
|
||||
* slider.getValue()); }
|
||||
*
|
||||
* });
|
||||
*/
|
||||
return slider;
|
||||
}
|
||||
|
||||
public static JTextField createTextField(String content) {
|
||||
JTextField field = new JTextField(content);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,41 +1,41 @@
|
|||
package org.jzy3d.svm.editors;
|
||||
|
||||
import java.awt.GridLayout;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
|
||||
public class RegressionParamsEditor extends JPanel {
|
||||
public RegressionParamsEditor(Parameters params) {
|
||||
setLayout(new GridLayout(3, 2));
|
||||
// int min = 0;
|
||||
// int max = 100;
|
||||
|
||||
|
||||
setParameters(params);
|
||||
}
|
||||
|
||||
public void setParameters(Parameters params) {
|
||||
addField(0, "eps", Double.toString(params.getParam().eps));
|
||||
addField(1, "C", Double.toString(params.getParam().C));
|
||||
addField(2, "gamma", Double.toString(params.getParam().gamma));
|
||||
}
|
||||
|
||||
protected void addField(int id, String name, String value) {
|
||||
fields[id] = EditorToolkit.createTextField(value);
|
||||
add(new JLabel(name));
|
||||
add(fields[id]);
|
||||
}
|
||||
|
||||
public JTextField getField(int i) {
|
||||
return fields[i];
|
||||
}
|
||||
|
||||
protected JTextField[] fields = new JTextField[4];
|
||||
|
||||
private static final long serialVersionUID = 3090387949522460142L;
|
||||
|
||||
}
|
||||
package org.jzy3d.svm.editors;
|
||||
|
||||
import java.awt.GridLayout;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
|
||||
public class RegressionParamsEditor extends JPanel {
|
||||
public RegressionParamsEditor(Parameters params) {
|
||||
setLayout(new GridLayout(3, 2));
|
||||
// int min = 0;
|
||||
// int max = 100;
|
||||
|
||||
|
||||
setParameters(params);
|
||||
}
|
||||
|
||||
public void setParameters(Parameters params) {
|
||||
addField(0, "eps", Double.toString(params.getParam().eps));
|
||||
addField(1, "C", Double.toString(params.getParam().C));
|
||||
addField(2, "gamma", Double.toString(params.getParam().gamma));
|
||||
}
|
||||
|
||||
protected void addField(int id, String name, String value) {
|
||||
fields[id] = EditorToolkit.createTextField(value);
|
||||
add(new JLabel(name));
|
||||
add(fields[id]);
|
||||
}
|
||||
|
||||
public JTextField getField(int i) {
|
||||
return fields[i];
|
||||
}
|
||||
|
||||
protected JTextField[] fields = new JTextField[4];
|
||||
|
||||
private static final long serialVersionUID = 3090387949522460142L;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,39 +1,39 @@
|
|||
package org.jzy3d.svm.tesselation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.maths.Range;
|
||||
import org.jzy3d.plot3d.builder.Mapper;
|
||||
import org.jzy3d.plot3d.builder.concrete.OrthonormalGrid;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
public class SvmGrid extends OrthonormalGrid {
|
||||
public SvmGrid(Range xyrange, int xysteps) {
|
||||
super(xyrange, xysteps);
|
||||
}
|
||||
|
||||
public SvmGrid(Range xrange, int xsteps, Range yrange, int ysteps) {
|
||||
super(xrange, xsteps, yrange, ysteps);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Coord3d> apply(Mapper mapper) {
|
||||
if (mapper instanceof SvmMapper) {
|
||||
SvmMapper svmm = (SvmMapper) mapper;
|
||||
|
||||
Vector<svm_node[]> nodes = Conversion.toDataset(xrange, yrange, xsteps, ysteps);
|
||||
double[] out = svmm.f(nodes);
|
||||
|
||||
List<Coord3d> output = new ArrayList<Coord3d>(xsteps * ysteps);
|
||||
for (int i = 0; i < out.length; i++)
|
||||
output.add(new Coord3d(nodes.get(i)[0].value, nodes.get(i)[1].value, out[i]));
|
||||
return output;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
package org.jzy3d.svm.tesselation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.maths.Range;
|
||||
import org.jzy3d.plot3d.builder.Mapper;
|
||||
import org.jzy3d.plot3d.builder.concrete.OrthonormalGrid;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
public class SvmGrid extends OrthonormalGrid {
|
||||
public SvmGrid(Range xyrange, int xysteps) {
|
||||
super(xyrange, xysteps);
|
||||
}
|
||||
|
||||
public SvmGrid(Range xrange, int xsteps, Range yrange, int ysteps) {
|
||||
super(xrange, xsteps, yrange, ysteps);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Coord3d> apply(Mapper mapper) {
|
||||
if (mapper instanceof SvmMapper) {
|
||||
SvmMapper svmm = (SvmMapper) mapper;
|
||||
|
||||
Vector<svm_node[]> nodes = Conversion.toDataset(xrange, yrange, xsteps, ysteps);
|
||||
double[] out = svmm.f(nodes);
|
||||
|
||||
List<Coord3d> output = new ArrayList<Coord3d>(xsteps * ysteps);
|
||||
for (int i = 0; i < out.length; i++)
|
||||
output.add(new Coord3d(nodes.get(i)[0].value, nodes.get(i)[1].value, out[i]));
|
||||
return output;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,54 +1,54 @@
|
|||
package org.jzy3d.svm.tesselation;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.SVM;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.plot3d.builder.Mapper;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
|
||||
public class SvmMapper extends Mapper {
|
||||
|
||||
public SvmMapper(Coord3d[] input) {
|
||||
this(input, new Parameters());
|
||||
}
|
||||
|
||||
public SvmMapper(Coord3d[] input, Parameters parameters) {
|
||||
params = parameters;
|
||||
svm = new SVM();
|
||||
svm.train(Conversion.copyInputs(input), Conversion.copyTargets(input), params);
|
||||
}
|
||||
|
||||
public SvmMapper(RegressionSVM svm) {
|
||||
this.svm = svm;
|
||||
}
|
||||
|
||||
public SVM getSvm() {
|
||||
return svm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double f(double x, double y) {
|
||||
output = svm.apply(Conversion.toDataset(Conversion.toVector(x, y)));
|
||||
return output[0];
|
||||
}
|
||||
|
||||
|
||||
public double[] f(Vector<svm_node[]> nodes) {
|
||||
output = svm.apply(nodes);
|
||||
return output;
|
||||
}
|
||||
|
||||
public double[] getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
protected double[] output;
|
||||
protected Parameters params;
|
||||
protected SVM svm;
|
||||
}
|
||||
package org.jzy3d.svm.tesselation;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.SVM;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.plot3d.builder.Mapper;
|
||||
import org.jzy3d.svm.utils.Conversion;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
|
||||
public class SvmMapper extends Mapper {
|
||||
|
||||
public SvmMapper(Coord3d[] input) {
|
||||
this(input, new Parameters());
|
||||
}
|
||||
|
||||
public SvmMapper(Coord3d[] input, Parameters parameters) {
|
||||
params = parameters;
|
||||
svm = new SVM();
|
||||
svm.train(Conversion.copyInputs(input), Conversion.copyTargets(input), params);
|
||||
}
|
||||
|
||||
public SvmMapper(RegressionSVM svm) {
|
||||
this.svm = svm;
|
||||
}
|
||||
|
||||
public SVM getSvm() {
|
||||
return svm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double f(double x, double y) {
|
||||
output = svm.apply(Conversion.toDataset(Conversion.toVector(x, y)));
|
||||
return output[0];
|
||||
}
|
||||
|
||||
|
||||
public double[] f(Vector<svm_node[]> nodes) {
|
||||
output = svm.apply(nodes);
|
||||
return output;
|
||||
}
|
||||
|
||||
public double[] getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
protected double[] output;
|
||||
protected Parameters params;
|
||||
protected SVM svm;
|
||||
}
|
||||
|
|
|
@ -1,115 +1,115 @@
|
|||
package org.jzy3d.svm.utils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.jzy3d.maths.BoundingBox3d;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.maths.Range;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
/** Helpers to convert Jzy3d datatypes to libsvm datatypes */
|
||||
public class Conversion extends org.instantsvm.utils.Conversion {
|
||||
public static Vector<svm_node[]> copyInputs(Coord3d[] input) {
|
||||
Vector<svm_node[]> inputs = new Vector<svm_node[]>(input.length);
|
||||
for (int i = 0; i < input.length; i++)
|
||||
inputs.add(toVector(input[i]));
|
||||
return inputs;
|
||||
}
|
||||
|
||||
public static Vector<Double> copyTargets(Coord3d[] input) {
|
||||
Vector<Double> targets = new Vector<Double>(input.length);
|
||||
for (int i = 0; i < input.length; i++)
|
||||
targets.add((double) input[i].z);
|
||||
return targets;
|
||||
}
|
||||
|
||||
public static Vector<svm_node[]> toDataset(Range xrange, Range yrange, int xsteps, int ysteps) {
|
||||
double xstep = xrange.getRange() / (double) (xsteps - 1);
|
||||
double ystep = yrange.getRange() / (double) (ysteps - 1);
|
||||
|
||||
Vector<svm_node[]> output = new Vector<svm_node[]>(xsteps * ysteps);
|
||||
for (int xi = 0; xi < xsteps; xi++) {
|
||||
for (int yi = 0; yi < ysteps; yi++) {
|
||||
double x = xrange.getMin() + xi * xstep;
|
||||
double y = yrange.getMin() + yi * ystep;
|
||||
output.add(toVector(x, y));
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static BoundingBox3d getBounds(Coord3d[] coords) {
|
||||
BoundingBox3d box = new BoundingBox3d();
|
||||
for (int i = 0; i < coords.length; i++) {
|
||||
box.add(coords[i]);
|
||||
}
|
||||
return box;
|
||||
}
|
||||
|
||||
public static Coord3d[] toArray(List<Coord3d> list) {
|
||||
Coord3d[] points = new Coord3d[list.size()];
|
||||
int k = 0;
|
||||
for (Coord3d c : list) {
|
||||
points[k++] = c;
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
public static RegressionInputs toRegressionInputs(List<Coord3d> list) {
|
||||
return new RegressionInputs(toXVector(list), toYVector(list));
|
||||
}
|
||||
|
||||
public static RegressionInputs toRegressionInputs(Coord3d[] list) {
|
||||
return new RegressionInputs(toXVector(list), toYVector(list));
|
||||
}
|
||||
|
||||
public static svm_node[] toVector(Coord3d c) {
|
||||
svm_node[] vec = new svm_node[2];
|
||||
vec[0] = toVector(0, c.x);
|
||||
vec[1] = toVector(1, c.y);
|
||||
return vec;
|
||||
}
|
||||
|
||||
public static Vector<svm_node[]> toXVector(List<Coord3d> list) {
|
||||
Vector<svm_node[]> v = new Vector<svm_node[]>(list.size());
|
||||
for (Coord3d c : list)
|
||||
v.add(toVector(c));
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector<Double> toYVector(List<Coord3d> list) {
|
||||
Vector<Double> v = new Vector<Double>(list.size());
|
||||
for (Coord3d c : list)
|
||||
v.add((double) c.z);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector<svm_node[]> toXVector(Coord3d[] list) {
|
||||
Vector<svm_node[]> v = new Vector<svm_node[]>(list.length);
|
||||
for (Coord3d c : list)
|
||||
v.add(toVector(c));
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector<Double> toYVector(Coord3d[] list) {
|
||||
Vector<Double> v = new Vector<Double>(list.length);
|
||||
for (Coord3d c : list)
|
||||
v.add((double) c.z);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Coord3d[] toCoord3d(RegressionInputs inputs) {
|
||||
Vector<svm_node[]> x = inputs.getX();
|
||||
Vector<Double> y = inputs.getY();
|
||||
|
||||
int n = x.size();
|
||||
|
||||
Coord3d[] output = new Coord3d[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
output[i] = new Coord3d(x.get(i)[0].value, x.get(i)[1].value, y.get(i));
|
||||
return output;
|
||||
}
|
||||
}
|
||||
package org.jzy3d.svm.utils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.jzy3d.maths.BoundingBox3d;
|
||||
import org.jzy3d.maths.Coord3d;
|
||||
import org.jzy3d.maths.Range;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
/** Helpers to convert Jzy3d datatypes to libsvm datatypes */
|
||||
public class Conversion extends org.instantsvm.utils.Conversion {
|
||||
public static Vector<svm_node[]> copyInputs(Coord3d[] input) {
|
||||
Vector<svm_node[]> inputs = new Vector<svm_node[]>(input.length);
|
||||
for (int i = 0; i < input.length; i++)
|
||||
inputs.add(toVector(input[i]));
|
||||
return inputs;
|
||||
}
|
||||
|
||||
public static Vector<Double> copyTargets(Coord3d[] input) {
|
||||
Vector<Double> targets = new Vector<Double>(input.length);
|
||||
for (int i = 0; i < input.length; i++)
|
||||
targets.add((double) input[i].z);
|
||||
return targets;
|
||||
}
|
||||
|
||||
public static Vector<svm_node[]> toDataset(Range xrange, Range yrange, int xsteps, int ysteps) {
|
||||
double xstep = xrange.getRange() / (double) (xsteps - 1);
|
||||
double ystep = yrange.getRange() / (double) (ysteps - 1);
|
||||
|
||||
Vector<svm_node[]> output = new Vector<svm_node[]>(xsteps * ysteps);
|
||||
for (int xi = 0; xi < xsteps; xi++) {
|
||||
for (int yi = 0; yi < ysteps; yi++) {
|
||||
double x = xrange.getMin() + xi * xstep;
|
||||
double y = yrange.getMin() + yi * ystep;
|
||||
output.add(toVector(x, y));
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static BoundingBox3d getBounds(Coord3d[] coords) {
|
||||
BoundingBox3d box = new BoundingBox3d();
|
||||
for (int i = 0; i < coords.length; i++) {
|
||||
box.add(coords[i]);
|
||||
}
|
||||
return box;
|
||||
}
|
||||
|
||||
public static Coord3d[] toArray(List<Coord3d> list) {
|
||||
Coord3d[] points = new Coord3d[list.size()];
|
||||
int k = 0;
|
||||
for (Coord3d c : list) {
|
||||
points[k++] = c;
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
public static RegressionInputs toRegressionInputs(List<Coord3d> list) {
|
||||
return new RegressionInputs(toXVector(list), toYVector(list));
|
||||
}
|
||||
|
||||
public static RegressionInputs toRegressionInputs(Coord3d[] list) {
|
||||
return new RegressionInputs(toXVector(list), toYVector(list));
|
||||
}
|
||||
|
||||
public static svm_node[] toVector(Coord3d c) {
|
||||
svm_node[] vec = new svm_node[2];
|
||||
vec[0] = toVector(0, c.x);
|
||||
vec[1] = toVector(1, c.y);
|
||||
return vec;
|
||||
}
|
||||
|
||||
public static Vector<svm_node[]> toXVector(List<Coord3d> list) {
|
||||
Vector<svm_node[]> v = new Vector<svm_node[]>(list.size());
|
||||
for (Coord3d c : list)
|
||||
v.add(toVector(c));
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector<Double> toYVector(List<Coord3d> list) {
|
||||
Vector<Double> v = new Vector<Double>(list.size());
|
||||
for (Coord3d c : list)
|
||||
v.add((double) c.z);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector<svm_node[]> toXVector(Coord3d[] list) {
|
||||
Vector<svm_node[]> v = new Vector<svm_node[]>(list.length);
|
||||
for (Coord3d c : list)
|
||||
v.add(toVector(c));
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector<Double> toYVector(Coord3d[] list) {
|
||||
Vector<Double> v = new Vector<Double>(list.length);
|
||||
for (Coord3d c : list)
|
||||
v.add((double) c.z);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Coord3d[] toCoord3d(RegressionInputs inputs) {
|
||||
Vector<svm_node[]> x = inputs.getX();
|
||||
Vector<Double> y = inputs.getY();
|
||||
|
||||
int n = x.size();
|
||||
|
||||
Coord3d[] output = new Coord3d[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
output[i] = new Coord3d(x.get(i)[0].value, x.get(i)[1].value, y.get(i));
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,2 +1,2 @@
|
|||
http://archive.ics.uci.edu/ml/
|
||||
http://archive.ics.uci.edu/ml/
|
||||
kaviar: http://trac.clermont.cemagref.fr/projets/Kaviar/wiki
|
|
@ -1,22 +1,22 @@
|
|||
|
||||
|
||||
Parameter hierarchy
|
||||
by svm
|
||||
have kernel param object
|
||||
|
||||
UI
|
||||
parameters through interface
|
||||
selection of input dataset / model
|
||||
train/apply on auto grid
|
||||
|
||||
IO
|
||||
inputs
|
||||
save trained model
|
||||
remain compatible with libsvm format
|
||||
support csv format
|
||||
|
||||
|
||||
Scaling facilities
|
||||
progress monitor?
|
||||
std out?
|
||||
|
||||
|
||||
Parameter hierarchy
|
||||
by svm
|
||||
have kernel param object
|
||||
|
||||
UI
|
||||
parameters through interface
|
||||
selection of input dataset / model
|
||||
train/apply on auto grid
|
||||
|
||||
IO
|
||||
inputs
|
||||
save trained model
|
||||
remain compatible with libsvm format
|
||||
support csv format
|
||||
|
||||
|
||||
Scaling facilities
|
||||
progress monitor?
|
||||
std out?
|
||||
support vectors?
|
|
@ -1,37 +1,37 @@
|
|||
package org.instantsvm.demos.regression;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionParameters;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.instantsvm.utils.LibSvmConsole;
|
||||
import org.instantsvm.utils.LibSvmIO;
|
||||
|
||||
public class RegressionDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
regressionTrial("bodyfat");
|
||||
}
|
||||
|
||||
public static void regressionTrial(String name) throws IOException {
|
||||
RegressionInputs inputs =
|
||||
LibSvmIO.loadRegression(LibSvmIO.DIR_DATASETS + name + "/inputs.lsvm");
|
||||
RegressionParameters p = new RegressionParameters();
|
||||
|
||||
// Train and save
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
svm.train(inputs, p);
|
||||
svm.save(LibSvmIO.DIR_MODELS + name + "/", "model.lsvm");
|
||||
svm = null;
|
||||
|
||||
// Reload and apply
|
||||
svm = new RegressionSVM(LibSvmIO.DIR_MODELS + name + "/model.lsvm"); // TODO: does not reload
|
||||
// params correctly
|
||||
|
||||
// Print results and parameters
|
||||
LibSvmConsole.print(svm.apply(inputs.getX()));
|
||||
System.out.println();
|
||||
p.print();
|
||||
svm.getParameters().print();
|
||||
}
|
||||
}
|
||||
package org.instantsvm.demos.regression;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionParameters;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.instantsvm.utils.LibSvmConsole;
|
||||
import org.instantsvm.utils.LibSvmIO;
|
||||
|
||||
public class RegressionDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
regressionTrial("bodyfat");
|
||||
}
|
||||
|
||||
public static void regressionTrial(String name) throws IOException {
|
||||
RegressionInputs inputs =
|
||||
LibSvmIO.loadRegression(LibSvmIO.DIR_DATASETS + name + "/inputs.lsvm");
|
||||
RegressionParameters p = new RegressionParameters();
|
||||
|
||||
// Train and save
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
svm.train(inputs, p);
|
||||
svm.save(LibSvmIO.DIR_MODELS + name + "/", "model.lsvm");
|
||||
svm = null;
|
||||
|
||||
// Reload and apply
|
||||
svm = new RegressionSVM(LibSvmIO.DIR_MODELS + name + "/model.lsvm"); // TODO: does not reload
|
||||
// params correctly
|
||||
|
||||
// Print results and parameters
|
||||
LibSvmConsole.print(svm.apply(inputs.getX()));
|
||||
System.out.println();
|
||||
p.print();
|
||||
svm.getParameters().print();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,46 +1,46 @@
|
|||
package org.instantsvm.demos.regression;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.XValResult;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionParameters;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.instantsvm.utils.LibSvmConsole;
|
||||
import org.instantsvm.utils.LibSvmIO;
|
||||
|
||||
public class XValDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
regressionTrial("radial");
|
||||
}
|
||||
|
||||
public static void regressionTrial(String name) throws IOException {
|
||||
RegressionInputs inputs = LibSvmIO.loadRegression(LibSvmIO.DIR_DATASETS + name + "/inputs.csv");
|
||||
RegressionParameters p = new RegressionParameters();
|
||||
|
||||
scale(inputs, 1000, 1000, 1);
|
||||
|
||||
// Train and save
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
XValResult r = svm.xval(inputs.getX(), inputs.getY(), p, 3, 0, 10000, 6, 0, 5, 6);
|
||||
System.out.println("Best parameters:" + r);
|
||||
LibSvmConsole.print(r.errors);
|
||||
/*
|
||||
* svm.train(inputs, p); svm.save(LibSvmIO.DIR_MODELS + name + "/", "model.lsvm"); svm = null;
|
||||
*
|
||||
* // Reload and apply svm = new RegressionSVM(LibSvmIO.DIR_MODELS + name + "/model.lsvm"); //
|
||||
* TODO: does not reload params correctly
|
||||
*
|
||||
* // Print results and parameters LibSvmConsole.print( svm.apply( inputs.getX() ) );
|
||||
* System.out.println(); p.print(); svm.getParameters().print();
|
||||
*/
|
||||
}
|
||||
|
||||
public static void scale(RegressionInputs inputs, float xfact, float yfact, float zfact) {
|
||||
for (int i = 0; i < inputs.getX().size(); i++) {
|
||||
inputs.getX().get(i)[0].value *= xfact;
|
||||
inputs.getX().get(i)[1].value *= yfact;
|
||||
inputs.getY().set(i, inputs.getY().get(i) * zfact);
|
||||
}
|
||||
}
|
||||
}
|
||||
package org.instantsvm.demos.regression;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.XValResult;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionParameters;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
import org.instantsvm.utils.LibSvmConsole;
|
||||
import org.instantsvm.utils.LibSvmIO;
|
||||
|
||||
public class XValDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
regressionTrial("radial");
|
||||
}
|
||||
|
||||
public static void regressionTrial(String name) throws IOException {
|
||||
RegressionInputs inputs = LibSvmIO.loadRegression(LibSvmIO.DIR_DATASETS + name + "/inputs.csv");
|
||||
RegressionParameters p = new RegressionParameters();
|
||||
|
||||
scale(inputs, 1000, 1000, 1);
|
||||
|
||||
// Train and save
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
XValResult r = svm.xval(inputs.getX(), inputs.getY(), p, 3, 0, 10000, 6, 0, 5, 6);
|
||||
System.out.println("Best parameters:" + r);
|
||||
LibSvmConsole.print(r.errors);
|
||||
/*
|
||||
* svm.train(inputs, p); svm.save(LibSvmIO.DIR_MODELS + name + "/", "model.lsvm"); svm = null;
|
||||
*
|
||||
* // Reload and apply svm = new RegressionSVM(LibSvmIO.DIR_MODELS + name + "/model.lsvm"); //
|
||||
* TODO: does not reload params correctly
|
||||
*
|
||||
* // Print results and parameters LibSvmConsole.print( svm.apply( inputs.getX() ) );
|
||||
* System.out.println(); p.print(); svm.getParameters().print();
|
||||
*/
|
||||
}
|
||||
|
||||
public static void scale(RegressionInputs inputs, float xfact, float yfact, float zfact) {
|
||||
for (int i = 0; i < inputs.getX().size(); i++) {
|
||||
inputs.getX().get(i)[0].value *= xfact;
|
||||
inputs.getX().get(i)[1].value *= yfact;
|
||||
inputs.getY().set(i, inputs.getY().get(i) * zfact);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,73 +1,73 @@
|
|||
package org.instantsvm;
|
||||
|
||||
import org.instantsvm.utils.LibSvmConsole;
|
||||
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
/**
|
||||
* options: -s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-class SVM 3 --
|
||||
* epsilon-SVR 4 -- nu-SVR -t kernel_type : set type of kernel function (default 2) 0 -- linear:
|
||||
* u'*v 1 -- polynomial: (gamma*u'*v + coef0)^degree 2 -- radial basis function: exp(-gamma*|u-v|^2)
|
||||
* 3 -- sigmoid: tanh(gamma*u'*v + coef0) 4 -- precomputed kernel (kernel values in
|
||||
* training_set_file) -d degree : set degree in kernel function (default 3) -g gamma : set gamma in
|
||||
* kernel function (default 1/num_features) -r coef0 : set coef0 in kernel function (default 0) -c
|
||||
* cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1) -n nu : set the
|
||||
* parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5) -p epsilon : set the epsilon in
|
||||
* loss function of epsilon-SVR (default 0.1) -m cachesize : set cache memory size in MB (default
|
||||
* 100) -e epsilon : set tolerance of termination criterion (default 0.001) -h shrinking : whether
|
||||
* to use the shrinking heuristics, 0 or 1 (default 1) -b probability_estimates : whether to train a
|
||||
* SVC or SVR model for probability estimates, 0 or 1 (default 0) -wi weight : set the parameter C
|
||||
* of class i to weight*C, for C-SVC (default 1) -v n : n-fold cross validation mode -q : quiet mode
|
||||
* (no outputs)
|
||||
*/
|
||||
public class Parameters {
|
||||
public Parameters() {
|
||||
param = new svm_parameter();
|
||||
defaults();
|
||||
}
|
||||
|
||||
public Parameters(svm_parameter param) {
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
public Parameters(double eps, double C, double gamma) {
|
||||
param = new svm_parameter();
|
||||
defaults();
|
||||
param.eps = eps;
|
||||
param.C = C;
|
||||
param.gamma = gamma;
|
||||
}
|
||||
|
||||
public void setParam(svm_parameter param) {
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
public svm_parameter getParam() {
|
||||
return param;
|
||||
}
|
||||
|
||||
protected void defaults() {
|
||||
param.svm_type = svm_parameter.C_SVC;
|
||||
param.kernel_type = svm_parameter.RBF;
|
||||
param.degree = 3;
|
||||
param.gamma = 0; // 1/num_features
|
||||
param.coef0 = 0;
|
||||
param.nu = 0.5;
|
||||
param.cache_size = 100;
|
||||
param.C = 1;
|
||||
param.eps = 1e-3;
|
||||
param.p = 0.1;
|
||||
param.shrinking = 0;
|
||||
param.probability = 0;
|
||||
param.nr_weight = 0;
|
||||
param.weight_label = new int[0];
|
||||
param.weight = new double[0];
|
||||
}
|
||||
|
||||
public void print() {
|
||||
System.out.println(this.getClass().getSimpleName() + ":");
|
||||
LibSvmConsole.print(param);
|
||||
}
|
||||
|
||||
protected svm_parameter param;
|
||||
}
|
||||
package org.instantsvm;
|
||||
|
||||
import org.instantsvm.utils.LibSvmConsole;
|
||||
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
/**
|
||||
* options: -s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-class SVM 3 --
|
||||
* epsilon-SVR 4 -- nu-SVR -t kernel_type : set type of kernel function (default 2) 0 -- linear:
|
||||
* u'*v 1 -- polynomial: (gamma*u'*v + coef0)^degree 2 -- radial basis function: exp(-gamma*|u-v|^2)
|
||||
* 3 -- sigmoid: tanh(gamma*u'*v + coef0) 4 -- precomputed kernel (kernel values in
|
||||
* training_set_file) -d degree : set degree in kernel function (default 3) -g gamma : set gamma in
|
||||
* kernel function (default 1/num_features) -r coef0 : set coef0 in kernel function (default 0) -c
|
||||
* cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1) -n nu : set the
|
||||
* parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5) -p epsilon : set the epsilon in
|
||||
* loss function of epsilon-SVR (default 0.1) -m cachesize : set cache memory size in MB (default
|
||||
* 100) -e epsilon : set tolerance of termination criterion (default 0.001) -h shrinking : whether
|
||||
* to use the shrinking heuristics, 0 or 1 (default 1) -b probability_estimates : whether to train a
|
||||
* SVC or SVR model for probability estimates, 0 or 1 (default 0) -wi weight : set the parameter C
|
||||
* of class i to weight*C, for C-SVC (default 1) -v n : n-fold cross validation mode -q : quiet mode
|
||||
* (no outputs)
|
||||
*/
|
||||
public class Parameters {
|
||||
public Parameters() {
|
||||
param = new svm_parameter();
|
||||
defaults();
|
||||
}
|
||||
|
||||
public Parameters(svm_parameter param) {
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
public Parameters(double eps, double C, double gamma) {
|
||||
param = new svm_parameter();
|
||||
defaults();
|
||||
param.eps = eps;
|
||||
param.C = C;
|
||||
param.gamma = gamma;
|
||||
}
|
||||
|
||||
public void setParam(svm_parameter param) {
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
public svm_parameter getParam() {
|
||||
return param;
|
||||
}
|
||||
|
||||
protected void defaults() {
|
||||
param.svm_type = svm_parameter.C_SVC;
|
||||
param.kernel_type = svm_parameter.RBF;
|
||||
param.degree = 3;
|
||||
param.gamma = 0; // 1/num_features
|
||||
param.coef0 = 0;
|
||||
param.nu = 0.5;
|
||||
param.cache_size = 100;
|
||||
param.C = 1;
|
||||
param.eps = 1e-3;
|
||||
param.p = 0.1;
|
||||
param.shrinking = 0;
|
||||
param.probability = 0;
|
||||
param.nr_weight = 0;
|
||||
param.weight_label = new int[0];
|
||||
param.weight = new double[0];
|
||||
}
|
||||
|
||||
public void print() {
|
||||
System.out.println(this.getClass().getSimpleName() + ":");
|
||||
LibSvmConsole.print(param);
|
||||
}
|
||||
|
||||
protected svm_parameter param;
|
||||
}
|
||||
|
|
|
@ -1,266 +1,266 @@
|
|||
package org.instantsvm;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.utils.LibSvmConsole;
|
||||
import org.instantsvm.utils.LibSvmIO;
|
||||
|
||||
import libsvm.svm;
|
||||
import libsvm.svm_model;
|
||||
import libsvm.svm_node;
|
||||
import libsvm.svm_parameter;
|
||||
import libsvm.svm_problem;
|
||||
|
||||
public class SVM {
|
||||
public SVM() {}
|
||||
|
||||
public SVM(String filename) throws IOException {
|
||||
this.load(filename);
|
||||
}
|
||||
|
||||
public SVM(svm_model model) {
|
||||
mountModel(model);
|
||||
}
|
||||
|
||||
public void train(Vector<svm_node[]> vx, Vector<Double> vy) {
|
||||
train(vx, vy, getDefaultParameters());
|
||||
}
|
||||
|
||||
public void train(Vector<svm_node[]> vx, Vector<Double> vy, Parameters parameters) {
|
||||
this.parameters = parameters;
|
||||
this.param = parameters.getParam();
|
||||
|
||||
load(vx, vy);
|
||||
model = svm.svm_train(prob, param);
|
||||
parameters.setParam(param);
|
||||
}
|
||||
|
||||
public XValResult xval(Vector<svm_node[]> vx, Vector<Double> vy, Parameters parameters,
|
||||
int nrfold, double cMin, double cMax, int cSteps, double gMin, double gMax, int gSteps) {
|
||||
this.parameters = parameters;
|
||||
this.param = parameters.getParam();
|
||||
|
||||
load(vx, vy);
|
||||
|
||||
|
||||
double[][] errors = new double[cSteps][gSteps];
|
||||
double cRange = cMax - cMin;
|
||||
double gRange = gMax - gMin;
|
||||
double cStep = cRange / cSteps;
|
||||
double gStep = gRange / gSteps;
|
||||
double bestC = 0;
|
||||
double bestG = 0;
|
||||
|
||||
double min = Double.POSITIVE_INFINITY;
|
||||
for (int i = 0; i < cSteps; i++) {
|
||||
for (int j = 0; j < gSteps; j++) {
|
||||
param.C = cMin + cStep * i;
|
||||
param.gamma = gMin + gStep * j;
|
||||
|
||||
errors[i][j] = do_cross_validation(nrfold);
|
||||
|
||||
if (errors[i][j] < min) {
|
||||
min = errors[i][j];
|
||||
bestC = param.C;
|
||||
bestG = param.gamma;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new XValResult(errors, bestC, bestG);
|
||||
}
|
||||
|
||||
public double[] apply(Vector<svm_node[]> x) {
|
||||
return predict(x, null, 0);
|
||||
}
|
||||
|
||||
public Parameters getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public svm_node[][] getSupportVectors() {
|
||||
return model.SV;
|
||||
}
|
||||
|
||||
public double[][] getCoefs() {
|
||||
return model.sv_coef;
|
||||
}
|
||||
|
||||
public void save(String directory, String filename) throws IOException {
|
||||
File dir = new File(directory);
|
||||
if (!dir.exists())
|
||||
dir.mkdirs();
|
||||
svm.svm_save_model(directory + filename, model);
|
||||
}
|
||||
|
||||
public void save(String filename) throws IOException {
|
||||
svm.svm_save_model(filename, model);
|
||||
}
|
||||
|
||||
public void load(String filename) throws IOException {
|
||||
mountModel(LibSvmIO.loadModel(filename));
|
||||
}
|
||||
|
||||
public void print() {
|
||||
System.out.println("Support vectors (" + model.SV.length + "):");
|
||||
LibSvmConsole.print(model.SV);
|
||||
System.out.println("Coefficients (" + model.sv_coef.length + "):");
|
||||
LibSvmConsole.print(model.sv_coef);
|
||||
}
|
||||
|
||||
public Parameters getDefaultParameters() {
|
||||
return new Parameters();
|
||||
}
|
||||
|
||||
/*******************************************************/
|
||||
|
||||
protected void mountModel(svm_model model) {
|
||||
this.model = model;
|
||||
this.parameters = new Parameters(model.param);
|
||||
}
|
||||
|
||||
protected void load(Vector<svm_node[]> vx, Vector<Double> vy) {
|
||||
int max_index = 0;
|
||||
|
||||
prob = new svm_problem();
|
||||
prob.l = vy.size();
|
||||
prob.x = new svm_node[prob.l][];
|
||||
for (int i = 0; i < prob.l; i++)
|
||||
prob.x[i] = vx.elementAt(i);
|
||||
prob.y = new double[prob.l];
|
||||
for (int i = 0; i < prob.l; i++)
|
||||
prob.y[i] = vy.elementAt(i);
|
||||
|
||||
if (param.gamma == 0 && max_index > 0)
|
||||
param.gamma = 1.0 / max_index;
|
||||
|
||||
if (param.kernel_type == svm_parameter.PRECOMPUTED)
|
||||
for (int i = 0; i < prob.l; i++) {
|
||||
if (prob.x[i][0].index != 0) {
|
||||
System.err.print("Wrong kernel matrix: first column must be 0:sample_serial_number\n");
|
||||
}
|
||||
if ((int) prob.x[i][0].value <= 0 || (int) prob.x[i][0].value > max_index) {
|
||||
System.err.print("Wrong input format: sample_serial_number out of range\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected double[] predict(Vector<svm_node[]> x, double[] targets, int predict_probability) {
|
||||
double[] output = new double[x.size()];
|
||||
|
||||
int correct = 0;
|
||||
int total = 0;
|
||||
double error = 0;
|
||||
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
|
||||
|
||||
int svm_type = svm.svm_get_svm_type(model);
|
||||
int nr_class = svm.svm_get_nr_class(model);
|
||||
double[] prob_estimates = null;
|
||||
|
||||
if (predict_probability == 1) {
|
||||
if (svm_type == svm_parameter.EPSILON_SVR || svm_type == svm_parameter.NU_SVR) {
|
||||
System.out.print(
|
||||
"Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma="
|
||||
+ svm.svm_get_svr_probability(model) + "\n");
|
||||
} else {
|
||||
int[] labels = new int[nr_class];
|
||||
svm.svm_get_labels(model, labels);
|
||||
prob_estimates = new double[nr_class];
|
||||
// output.writeBytes("labels");
|
||||
// for(int j=0;j<nr_class;j++)
|
||||
// output.writeBytes(" "+labels[j]);
|
||||
// output.writeBytes("\n");
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < x.size(); i++) {
|
||||
double v;
|
||||
if (predict_probability == 1
|
||||
&& (svm_type == svm_parameter.C_SVC || svm_type == svm_parameter.NU_SVC)) {
|
||||
v = svm.svm_predict_probability(model, x.get(i), prob_estimates);
|
||||
// output.writeBytes(v+" ");
|
||||
// for(int j=0;j<nr_class;j++)
|
||||
// output.writeBytes(prob_estimates[j]+" ");
|
||||
// output.writeBytes("\n");
|
||||
} else {
|
||||
v = svm.svm_predict(model, x.get(i));
|
||||
// output.writeBytes(v+"\n");
|
||||
// System.out.println(x.get(i)[0].value + " " + x.get(i)[1].value + " " + v);
|
||||
}
|
||||
|
||||
output[i] = v;
|
||||
|
||||
if (targets != null) {
|
||||
if (v == targets[i])
|
||||
++correct;
|
||||
error += (v - targets[i]) * (v - targets[i]);
|
||||
sumv += v;
|
||||
sumy += targets[i];
|
||||
sumvv += v * v;
|
||||
sumyy += targets[i] * targets[i];
|
||||
sumvy += v * targets[i];
|
||||
++total;
|
||||
}
|
||||
|
||||
}
|
||||
if (true) {
|
||||
if (svm_type == svm_parameter.EPSILON_SVR || svm_type == svm_parameter.NU_SVR) {
|
||||
System.out.print("Mean squared error = " + error / total + " (regression)\n");
|
||||
System.out.print("Squared correlation coefficient = "
|
||||
+ ((total * sumvy - sumv * sumy) * (total * sumvy - sumv * sumy))
|
||||
/ ((total * sumvv - sumv * sumv) * (total * sumyy - sumy * sumy))
|
||||
+ " (regression)\n");
|
||||
} else
|
||||
System.out.print("Accuracy = " + (double) correct / total * 100 + "% (" + correct + "/"
|
||||
+ total + ") (classification)\n");
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
protected double do_cross_validation(int nr_fold) {
|
||||
int i;
|
||||
int total_correct = 0;
|
||||
double total_error = 0;
|
||||
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
|
||||
double[] target = new double[prob.l];
|
||||
|
||||
svm.svm_cross_validation(prob, param, nr_fold, target);
|
||||
if (param.svm_type == svm_parameter.EPSILON_SVR || param.svm_type == svm_parameter.NU_SVR) {
|
||||
for (i = 0; i < prob.l; i++) {
|
||||
double y = prob.y[i];
|
||||
double v = target[i];
|
||||
total_error += (v - y) * (v - y);
|
||||
sumv += v;
|
||||
sumy += y;
|
||||
sumvv += v * v;
|
||||
sumyy += y * y;
|
||||
sumvy += v * y;
|
||||
}
|
||||
System.out.print("Cross Validation Mean squared error = " + total_error / prob.l + "\n");
|
||||
System.out.print("Cross Validation Squared correlation coefficient = "
|
||||
+ ((prob.l * sumvy - sumv * sumy) * (prob.l * sumvy - sumv * sumy))
|
||||
/ ((prob.l * sumvv - sumv * sumv) * (prob.l * sumyy - sumy * sumy))
|
||||
+ "\n");
|
||||
return total_error;
|
||||
} else {
|
||||
for (i = 0; i < prob.l; i++)
|
||||
if (target[i] == prob.y[i])
|
||||
++total_correct;
|
||||
System.out.print("Cross Validation Accuracy = " + 100.0 * total_correct / prob.l + "%\n");
|
||||
|
||||
return 100.0 * total_correct / prob.l;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************/
|
||||
|
||||
protected svm_parameter param;
|
||||
protected svm_problem prob;
|
||||
protected svm_model model;
|
||||
// protected int cross_validation;
|
||||
// protected int nr_fold;
|
||||
protected Parameters parameters;
|
||||
}
|
||||
package org.instantsvm;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.utils.LibSvmConsole;
|
||||
import org.instantsvm.utils.LibSvmIO;
|
||||
|
||||
import libsvm.svm;
|
||||
import libsvm.svm_model;
|
||||
import libsvm.svm_node;
|
||||
import libsvm.svm_parameter;
|
||||
import libsvm.svm_problem;
|
||||
|
||||
public class SVM {
|
||||
public SVM() {}
|
||||
|
||||
public SVM(String filename) throws IOException {
|
||||
this.load(filename);
|
||||
}
|
||||
|
||||
public SVM(svm_model model) {
|
||||
mountModel(model);
|
||||
}
|
||||
|
||||
public void train(Vector<svm_node[]> vx, Vector<Double> vy) {
|
||||
train(vx, vy, getDefaultParameters());
|
||||
}
|
||||
|
||||
public void train(Vector<svm_node[]> vx, Vector<Double> vy, Parameters parameters) {
|
||||
this.parameters = parameters;
|
||||
this.param = parameters.getParam();
|
||||
|
||||
load(vx, vy);
|
||||
model = svm.svm_train(prob, param);
|
||||
parameters.setParam(param);
|
||||
}
|
||||
|
||||
public XValResult xval(Vector<svm_node[]> vx, Vector<Double> vy, Parameters parameters,
|
||||
int nrfold, double cMin, double cMax, int cSteps, double gMin, double gMax, int gSteps) {
|
||||
this.parameters = parameters;
|
||||
this.param = parameters.getParam();
|
||||
|
||||
load(vx, vy);
|
||||
|
||||
|
||||
double[][] errors = new double[cSteps][gSteps];
|
||||
double cRange = cMax - cMin;
|
||||
double gRange = gMax - gMin;
|
||||
double cStep = cRange / cSteps;
|
||||
double gStep = gRange / gSteps;
|
||||
double bestC = 0;
|
||||
double bestG = 0;
|
||||
|
||||
double min = Double.POSITIVE_INFINITY;
|
||||
for (int i = 0; i < cSteps; i++) {
|
||||
for (int j = 0; j < gSteps; j++) {
|
||||
param.C = cMin + cStep * i;
|
||||
param.gamma = gMin + gStep * j;
|
||||
|
||||
errors[i][j] = do_cross_validation(nrfold);
|
||||
|
||||
if (errors[i][j] < min) {
|
||||
min = errors[i][j];
|
||||
bestC = param.C;
|
||||
bestG = param.gamma;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new XValResult(errors, bestC, bestG);
|
||||
}
|
||||
|
||||
public double[] apply(Vector<svm_node[]> x) {
|
||||
return predict(x, null, 0);
|
||||
}
|
||||
|
||||
public Parameters getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public svm_node[][] getSupportVectors() {
|
||||
return model.SV;
|
||||
}
|
||||
|
||||
public double[][] getCoefs() {
|
||||
return model.sv_coef;
|
||||
}
|
||||
|
||||
public void save(String directory, String filename) throws IOException {
|
||||
File dir = new File(directory);
|
||||
if (!dir.exists())
|
||||
dir.mkdirs();
|
||||
svm.svm_save_model(directory + filename, model);
|
||||
}
|
||||
|
||||
public void save(String filename) throws IOException {
|
||||
svm.svm_save_model(filename, model);
|
||||
}
|
||||
|
||||
public void load(String filename) throws IOException {
|
||||
mountModel(LibSvmIO.loadModel(filename));
|
||||
}
|
||||
|
||||
public void print() {
|
||||
System.out.println("Support vectors (" + model.SV.length + "):");
|
||||
LibSvmConsole.print(model.SV);
|
||||
System.out.println("Coefficients (" + model.sv_coef.length + "):");
|
||||
LibSvmConsole.print(model.sv_coef);
|
||||
}
|
||||
|
||||
public Parameters getDefaultParameters() {
|
||||
return new Parameters();
|
||||
}
|
||||
|
||||
/*******************************************************/
|
||||
|
||||
protected void mountModel(svm_model model) {
|
||||
this.model = model;
|
||||
this.parameters = new Parameters(model.param);
|
||||
}
|
||||
|
||||
protected void load(Vector<svm_node[]> vx, Vector<Double> vy) {
|
||||
int max_index = 0;
|
||||
|
||||
prob = new svm_problem();
|
||||
prob.l = vy.size();
|
||||
prob.x = new svm_node[prob.l][];
|
||||
for (int i = 0; i < prob.l; i++)
|
||||
prob.x[i] = vx.elementAt(i);
|
||||
prob.y = new double[prob.l];
|
||||
for (int i = 0; i < prob.l; i++)
|
||||
prob.y[i] = vy.elementAt(i);
|
||||
|
||||
if (param.gamma == 0 && max_index > 0)
|
||||
param.gamma = 1.0 / max_index;
|
||||
|
||||
if (param.kernel_type == svm_parameter.PRECOMPUTED)
|
||||
for (int i = 0; i < prob.l; i++) {
|
||||
if (prob.x[i][0].index != 0) {
|
||||
System.err.print("Wrong kernel matrix: first column must be 0:sample_serial_number\n");
|
||||
}
|
||||
if ((int) prob.x[i][0].value <= 0 || (int) prob.x[i][0].value > max_index) {
|
||||
System.err.print("Wrong input format: sample_serial_number out of range\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected double[] predict(Vector<svm_node[]> x, double[] targets, int predict_probability) {
|
||||
double[] output = new double[x.size()];
|
||||
|
||||
int correct = 0;
|
||||
int total = 0;
|
||||
double error = 0;
|
||||
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
|
||||
|
||||
int svm_type = svm.svm_get_svm_type(model);
|
||||
int nr_class = svm.svm_get_nr_class(model);
|
||||
double[] prob_estimates = null;
|
||||
|
||||
if (predict_probability == 1) {
|
||||
if (svm_type == svm_parameter.EPSILON_SVR || svm_type == svm_parameter.NU_SVR) {
|
||||
System.out.print(
|
||||
"Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma="
|
||||
+ svm.svm_get_svr_probability(model) + "\n");
|
||||
} else {
|
||||
int[] labels = new int[nr_class];
|
||||
svm.svm_get_labels(model, labels);
|
||||
prob_estimates = new double[nr_class];
|
||||
// output.writeBytes("labels");
|
||||
// for(int j=0;j<nr_class;j++)
|
||||
// output.writeBytes(" "+labels[j]);
|
||||
// output.writeBytes("\n");
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < x.size(); i++) {
|
||||
double v;
|
||||
if (predict_probability == 1
|
||||
&& (svm_type == svm_parameter.C_SVC || svm_type == svm_parameter.NU_SVC)) {
|
||||
v = svm.svm_predict_probability(model, x.get(i), prob_estimates);
|
||||
// output.writeBytes(v+" ");
|
||||
// for(int j=0;j<nr_class;j++)
|
||||
// output.writeBytes(prob_estimates[j]+" ");
|
||||
// output.writeBytes("\n");
|
||||
} else {
|
||||
v = svm.svm_predict(model, x.get(i));
|
||||
// output.writeBytes(v+"\n");
|
||||
// System.out.println(x.get(i)[0].value + " " + x.get(i)[1].value + " " + v);
|
||||
}
|
||||
|
||||
output[i] = v;
|
||||
|
||||
if (targets != null) {
|
||||
if (v == targets[i])
|
||||
++correct;
|
||||
error += (v - targets[i]) * (v - targets[i]);
|
||||
sumv += v;
|
||||
sumy += targets[i];
|
||||
sumvv += v * v;
|
||||
sumyy += targets[i] * targets[i];
|
||||
sumvy += v * targets[i];
|
||||
++total;
|
||||
}
|
||||
|
||||
}
|
||||
if (true) {
|
||||
if (svm_type == svm_parameter.EPSILON_SVR || svm_type == svm_parameter.NU_SVR) {
|
||||
System.out.print("Mean squared error = " + error / total + " (regression)\n");
|
||||
System.out.print("Squared correlation coefficient = "
|
||||
+ ((total * sumvy - sumv * sumy) * (total * sumvy - sumv * sumy))
|
||||
/ ((total * sumvv - sumv * sumv) * (total * sumyy - sumy * sumy))
|
||||
+ " (regression)\n");
|
||||
} else
|
||||
System.out.print("Accuracy = " + (double) correct / total * 100 + "% (" + correct + "/"
|
||||
+ total + ") (classification)\n");
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
protected double do_cross_validation(int nr_fold) {
|
||||
int i;
|
||||
int total_correct = 0;
|
||||
double total_error = 0;
|
||||
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
|
||||
double[] target = new double[prob.l];
|
||||
|
||||
svm.svm_cross_validation(prob, param, nr_fold, target);
|
||||
if (param.svm_type == svm_parameter.EPSILON_SVR || param.svm_type == svm_parameter.NU_SVR) {
|
||||
for (i = 0; i < prob.l; i++) {
|
||||
double y = prob.y[i];
|
||||
double v = target[i];
|
||||
total_error += (v - y) * (v - y);
|
||||
sumv += v;
|
||||
sumy += y;
|
||||
sumvv += v * v;
|
||||
sumyy += y * y;
|
||||
sumvy += v * y;
|
||||
}
|
||||
System.out.print("Cross Validation Mean squared error = " + total_error / prob.l + "\n");
|
||||
System.out.print("Cross Validation Squared correlation coefficient = "
|
||||
+ ((prob.l * sumvy - sumv * sumy) * (prob.l * sumvy - sumv * sumy))
|
||||
/ ((prob.l * sumvv - sumv * sumv) * (prob.l * sumyy - sumy * sumy))
|
||||
+ "\n");
|
||||
return total_error;
|
||||
} else {
|
||||
for (i = 0; i < prob.l; i++)
|
||||
if (target[i] == prob.y[i])
|
||||
++total_correct;
|
||||
System.out.print("Cross Validation Accuracy = " + 100.0 * total_correct / prob.l + "%\n");
|
||||
|
||||
return 100.0 * total_correct / prob.l;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************/
|
||||
|
||||
protected svm_parameter param;
|
||||
protected svm_problem prob;
|
||||
protected svm_model model;
|
||||
// protected int cross_validation;
|
||||
// protected int nr_fold;
|
||||
protected Parameters parameters;
|
||||
}
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
package org.instantsvm;
|
||||
|
||||
public class XValResult {
|
||||
public XValResult(double[][] errors, double bestC, double bestG) {
|
||||
this.errors = errors;
|
||||
this.bestC = bestC;
|
||||
this.bestG = bestG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "C = " + bestC + " gamma=" + bestG;
|
||||
}
|
||||
|
||||
public double bestC;
|
||||
public double bestG;
|
||||
public double[][] errors;
|
||||
|
||||
}
|
||||
package org.instantsvm;
|
||||
|
||||
public class XValResult {
|
||||
public XValResult(double[][] errors, double bestC, double bestG) {
|
||||
this.errors = errors;
|
||||
this.bestC = bestC;
|
||||
this.bestG = bestG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "C = " + bestC + " gamma=" + bestG;
|
||||
}
|
||||
|
||||
public double bestC;
|
||||
public double bestG;
|
||||
public double[][] errors;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,55 +1,55 @@
|
|||
package org.instantsvm.regression;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.utils.LibSvmIO;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
public class RegressionInputs {
|
||||
public RegressionInputs() {}
|
||||
|
||||
public RegressionInputs(String filename) throws IOException {
|
||||
load(filename);
|
||||
}
|
||||
|
||||
public RegressionInputs(Vector<svm_node[]> x, Vector<Double> y) {
|
||||
setValues(x, y);
|
||||
}
|
||||
|
||||
public void setValues(Vector<svm_node[]> x, Vector<Double> y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Vector<svm_node[]> getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Vector<Double> getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void scale(float xfact, float yfact, float zfact) {
|
||||
for (int i = 0; i < x.size(); i++) {
|
||||
x.get(i)[0].value *= xfact;
|
||||
x.get(i)[1].value *= yfact;
|
||||
if (zfact != 1)
|
||||
y.set(i, y.get(i) * zfact);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @see {@link LibSvmIO.loadRegression(String filename). */
|
||||
public void load(String filename) throws IOException {
|
||||
RegressionInputs i = LibSvmIO.loadRegression(filename);
|
||||
x = i.x;
|
||||
y = i.y;
|
||||
i = null;
|
||||
}
|
||||
|
||||
protected Vector<svm_node[]> x;
|
||||
protected Vector<Double> y;
|
||||
}
|
||||
package org.instantsvm.regression;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.utils.LibSvmIO;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
public class RegressionInputs {
|
||||
public RegressionInputs() {}
|
||||
|
||||
public RegressionInputs(String filename) throws IOException {
|
||||
load(filename);
|
||||
}
|
||||
|
||||
public RegressionInputs(Vector<svm_node[]> x, Vector<Double> y) {
|
||||
setValues(x, y);
|
||||
}
|
||||
|
||||
public void setValues(Vector<svm_node[]> x, Vector<Double> y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Vector<svm_node[]> getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Vector<Double> getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void scale(float xfact, float yfact, float zfact) {
|
||||
for (int i = 0; i < x.size(); i++) {
|
||||
x.get(i)[0].value *= xfact;
|
||||
x.get(i)[1].value *= yfact;
|
||||
if (zfact != 1)
|
||||
y.set(i, y.get(i) * zfact);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @see {@link LibSvmIO.loadRegression(String filename). */
|
||||
public void load(String filename) throws IOException {
|
||||
RegressionInputs i = LibSvmIO.loadRegression(filename);
|
||||
x = i.x;
|
||||
y = i.y;
|
||||
i = null;
|
||||
}
|
||||
|
||||
protected Vector<svm_node[]> x;
|
||||
protected Vector<Double> y;
|
||||
}
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
package org.instantsvm.regression;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
public class RegressionParameters extends Parameters {
|
||||
public RegressionParameters() {
|
||||
super();
|
||||
}
|
||||
|
||||
public RegressionParameters(double eps, double C, double gamma) {
|
||||
super(eps, C, gamma);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void defaults() {
|
||||
super.defaults();
|
||||
param.svm_type = svm_parameter.NU_SVR; // epsilon-SVR, nu-SVR
|
||||
param.kernel_type = 2;
|
||||
param.gamma = 0.5;
|
||||
param.C = 10;
|
||||
param.eps = 0.1;
|
||||
}
|
||||
}
|
||||
package org.instantsvm.regression;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
public class RegressionParameters extends Parameters {
|
||||
public RegressionParameters() {
|
||||
super();
|
||||
}
|
||||
|
||||
public RegressionParameters(double eps, double C, double gamma) {
|
||||
super(eps, C, gamma);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void defaults() {
|
||||
super.defaults();
|
||||
param.svm_type = svm_parameter.NU_SVR; // epsilon-SVR, nu-SVR
|
||||
param.kernel_type = 2;
|
||||
param.gamma = 0.5;
|
||||
param.C = 10;
|
||||
param.eps = 0.1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
package org.instantsvm.regression;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.SVM;
|
||||
|
||||
import libsvm.svm_model;
|
||||
|
||||
public class RegressionSVM extends SVM {
|
||||
public RegressionSVM() {
|
||||
super();
|
||||
}
|
||||
|
||||
public RegressionSVM(String filename) throws IOException {
|
||||
super(filename);
|
||||
}
|
||||
|
||||
public RegressionSVM(svm_model model) {
|
||||
super(model);
|
||||
}
|
||||
|
||||
public void train(RegressionInputs inputs, Parameters parameters) {
|
||||
train(inputs.getX(), inputs.getY(), parameters);
|
||||
}
|
||||
|
||||
public void train(RegressionInputs inputs) {
|
||||
train(inputs.getX(), inputs.getY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameters getDefaultParameters() {
|
||||
return new RegressionParameters();
|
||||
}
|
||||
}
|
||||
package org.instantsvm.regression;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.SVM;
|
||||
|
||||
import libsvm.svm_model;
|
||||
|
||||
public class RegressionSVM extends SVM {
|
||||
public RegressionSVM() {
|
||||
super();
|
||||
}
|
||||
|
||||
public RegressionSVM(String filename) throws IOException {
|
||||
super(filename);
|
||||
}
|
||||
|
||||
public RegressionSVM(svm_model model) {
|
||||
super(model);
|
||||
}
|
||||
|
||||
public void train(RegressionInputs inputs, Parameters parameters) {
|
||||
train(inputs.getX(), inputs.getY(), parameters);
|
||||
}
|
||||
|
||||
public void train(RegressionInputs inputs) {
|
||||
train(inputs.getX(), inputs.getY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameters getDefaultParameters() {
|
||||
return new RegressionParameters();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +1,28 @@
|
|||
package org.instantsvm.utils;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
public class Conversion {
|
||||
public static Vector<svm_node[]> toDataset(svm_node[] node) {
|
||||
Vector<svm_node[]> v = new Vector<svm_node[]>(1);
|
||||
v.add(node);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static svm_node[] toVector(double x, double y) {
|
||||
svm_node[] vec = new svm_node[2];
|
||||
vec[0] = toVector(0, x);
|
||||
vec[1] = toVector(1, y);
|
||||
return vec;
|
||||
}
|
||||
|
||||
public static svm_node toVector(int index, double value) {
|
||||
svm_node node = new svm_node();
|
||||
node.index = index;
|
||||
node.value = value;
|
||||
return node;
|
||||
}
|
||||
|
||||
}
|
||||
package org.instantsvm.utils;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import libsvm.svm_node;
|
||||
|
||||
public class Conversion {
|
||||
public static Vector<svm_node[]> toDataset(svm_node[] node) {
|
||||
Vector<svm_node[]> v = new Vector<svm_node[]>(1);
|
||||
v.add(node);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static svm_node[] toVector(double x, double y) {
|
||||
svm_node[] vec = new svm_node[2];
|
||||
vec[0] = toVector(0, x);
|
||||
vec[1] = toVector(1, y);
|
||||
return vec;
|
||||
}
|
||||
|
||||
public static svm_node toVector(int index, double value) {
|
||||
svm_node node = new svm_node();
|
||||
node.index = index;
|
||||
node.value = value;
|
||||
return node;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,48 +1,48 @@
|
|||
package org.instantsvm.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
|
||||
/**
|
||||
* LazySVM either reload an existing model, or train and save a model in InstantSVM library.
|
||||
*
|
||||
* @author Martin Pernollet
|
||||
*/
|
||||
public class LazySVM {
|
||||
public static RegressionSVM loadOrTrainRegression(String name, Parameters params)
|
||||
throws IOException {
|
||||
return loadOrTrainRegression(name, params,
|
||||
new RegressionInputs(LibSvmIO.defaultInputPath(name)));
|
||||
}
|
||||
|
||||
public static RegressionSVM loadOrTrainRegression(String name, Parameters params,
|
||||
RegressionInputs inputs) throws IOException {
|
||||
String modelFile = LibSvmIO.defaultModelPath(name);
|
||||
File f = new File(modelFile);
|
||||
|
||||
if (f.exists())
|
||||
return new RegressionSVM(modelFile);
|
||||
else
|
||||
return trainAndSaveRegression(name, inputs);
|
||||
}
|
||||
|
||||
public static RegressionSVM trainAndSaveRegression(String name, RegressionInputs inputs)
|
||||
throws IOException {
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
svm.train(inputs);
|
||||
svm.save(LibSvmIO.DIR_MODELS + name + "/", LibSvmIO.DEF_MODEL_NAME);
|
||||
return svm;
|
||||
}
|
||||
|
||||
public static RegressionSVM trainAndSaveRegression(String name, Parameters params,
|
||||
RegressionInputs inputs) throws IOException {
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
svm.train(inputs, params);
|
||||
svm.save(LibSvmIO.DIR_MODELS + name + "/", LibSvmIO.DEF_MODEL_NAME);
|
||||
return svm;
|
||||
}
|
||||
}
|
||||
package org.instantsvm.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.instantsvm.Parameters;
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
import org.instantsvm.regression.RegressionSVM;
|
||||
|
||||
/**
|
||||
* LazySVM either reload an existing model, or train and save a model in InstantSVM library.
|
||||
*
|
||||
* @author Martin Pernollet
|
||||
*/
|
||||
public class LazySVM {
|
||||
public static RegressionSVM loadOrTrainRegression(String name, Parameters params)
|
||||
throws IOException {
|
||||
return loadOrTrainRegression(name, params,
|
||||
new RegressionInputs(LibSvmIO.defaultInputPath(name)));
|
||||
}
|
||||
|
||||
public static RegressionSVM loadOrTrainRegression(String name, Parameters params,
|
||||
RegressionInputs inputs) throws IOException {
|
||||
String modelFile = LibSvmIO.defaultModelPath(name);
|
||||
File f = new File(modelFile);
|
||||
|
||||
if (f.exists())
|
||||
return new RegressionSVM(modelFile);
|
||||
else
|
||||
return trainAndSaveRegression(name, inputs);
|
||||
}
|
||||
|
||||
public static RegressionSVM trainAndSaveRegression(String name, RegressionInputs inputs)
|
||||
throws IOException {
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
svm.train(inputs);
|
||||
svm.save(LibSvmIO.DIR_MODELS + name + "/", LibSvmIO.DEF_MODEL_NAME);
|
||||
return svm;
|
||||
}
|
||||
|
||||
public static RegressionSVM trainAndSaveRegression(String name, Parameters params,
|
||||
RegressionInputs inputs) throws IOException {
|
||||
RegressionSVM svm = new RegressionSVM();
|
||||
svm.train(inputs, params);
|
||||
svm.save(LibSvmIO.DIR_MODELS + name + "/", LibSvmIO.DEF_MODEL_NAME);
|
||||
return svm;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,106 +1,106 @@
|
|||
package org.instantsvm.utils;
|
||||
|
||||
import libsvm.svm_node;
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
public class LibSvmConsole {
|
||||
public static void print(svm_parameter param) {
|
||||
System.out.println("svm_type = " + param.svm_type);
|
||||
System.out.println("kernel_type = " + param.kernel_type);
|
||||
System.out.println("degree = " + param.degree);
|
||||
System.out.println("gamma = " + param.gamma); // 1/num_features
|
||||
System.out.println("coef0 = " + param.coef0);
|
||||
System.out.println("nu = " + param.nu);
|
||||
System.out.println("cache_size = " + param.cache_size);
|
||||
System.out.println("C = " + param.C);
|
||||
System.out.println("eps = " + param.eps);
|
||||
System.out.println("p = " + param.p);
|
||||
System.out.println("shrinking = " + param.shrinking);
|
||||
System.out.println("probability = " + param.probability);
|
||||
System.out.println("nr_weight = " + param.nr_weight);
|
||||
|
||||
System.out.print("weight_label = ");
|
||||
if (param.weight_label != null)
|
||||
LibSvmConsole.print(param.weight_label);
|
||||
else
|
||||
System.out.println("null");
|
||||
|
||||
System.out.print("weight = ");
|
||||
if (param.weight != null)
|
||||
LibSvmConsole.print(param.weight);
|
||||
else
|
||||
System.out.println("null");
|
||||
}
|
||||
|
||||
public static void print(svm_node[][] nodes, double value) {
|
||||
for (int i = 0; i < nodes.length; i++)
|
||||
print(nodes[i]);
|
||||
|
||||
System.out.println("| " + value);
|
||||
}
|
||||
|
||||
public static void print(svm_node[][] nodes) {
|
||||
for (int i = 0; i < nodes.length; i++)
|
||||
println(nodes[i]);
|
||||
}
|
||||
|
||||
public static void println(svm_node[] nodes) {
|
||||
print(nodes);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void print(svm_node[] nodes) {
|
||||
for (int i = 0; i < nodes.length; i++)
|
||||
System.out.print(nodes[i].value + " | ");
|
||||
}
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
public static void print(double input[]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
System.out.print(input[i] + "|");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public static void print(float input[]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
System.out.print(input[i] + "|");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public static void print(int input[]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
System.out.print(input[i] + "|");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public static void print(double input[][]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
for (int j = 0; j < input[i].length; j++) {
|
||||
System.out.print(input[i][j] + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void print(float input[][]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
for (int j = 0; j < input[i].length; j++) {
|
||||
System.out.print(input[i][j] + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void print(int input[][]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
for (int j = 0; j < input[i].length; j++) {
|
||||
System.out.print(input[i][j] + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
package org.instantsvm.utils;
|
||||
|
||||
import libsvm.svm_node;
|
||||
import libsvm.svm_parameter;
|
||||
|
||||
public class LibSvmConsole {
|
||||
public static void print(svm_parameter param) {
|
||||
System.out.println("svm_type = " + param.svm_type);
|
||||
System.out.println("kernel_type = " + param.kernel_type);
|
||||
System.out.println("degree = " + param.degree);
|
||||
System.out.println("gamma = " + param.gamma); // 1/num_features
|
||||
System.out.println("coef0 = " + param.coef0);
|
||||
System.out.println("nu = " + param.nu);
|
||||
System.out.println("cache_size = " + param.cache_size);
|
||||
System.out.println("C = " + param.C);
|
||||
System.out.println("eps = " + param.eps);
|
||||
System.out.println("p = " + param.p);
|
||||
System.out.println("shrinking = " + param.shrinking);
|
||||
System.out.println("probability = " + param.probability);
|
||||
System.out.println("nr_weight = " + param.nr_weight);
|
||||
|
||||
System.out.print("weight_label = ");
|
||||
if (param.weight_label != null)
|
||||
LibSvmConsole.print(param.weight_label);
|
||||
else
|
||||
System.out.println("null");
|
||||
|
||||
System.out.print("weight = ");
|
||||
if (param.weight != null)
|
||||
LibSvmConsole.print(param.weight);
|
||||
else
|
||||
System.out.println("null");
|
||||
}
|
||||
|
||||
public static void print(svm_node[][] nodes, double value) {
|
||||
for (int i = 0; i < nodes.length; i++)
|
||||
print(nodes[i]);
|
||||
|
||||
System.out.println("| " + value);
|
||||
}
|
||||
|
||||
public static void print(svm_node[][] nodes) {
|
||||
for (int i = 0; i < nodes.length; i++)
|
||||
println(nodes[i]);
|
||||
}
|
||||
|
||||
public static void println(svm_node[] nodes) {
|
||||
print(nodes);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void print(svm_node[] nodes) {
|
||||
for (int i = 0; i < nodes.length; i++)
|
||||
System.out.print(nodes[i].value + " | ");
|
||||
}
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
public static void print(double input[]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
System.out.print(input[i] + "|");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public static void print(float input[]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
System.out.print(input[i] + "|");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public static void print(int input[]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
System.out.print(input[i] + "|");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public static void print(double input[][]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
for (int j = 0; j < input[i].length; j++) {
|
||||
System.out.print(input[i][j] + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void print(float input[][]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
for (int j = 0; j < input[i].length; j++) {
|
||||
System.out.print(input[i][j] + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void print(int input[][]) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
for (int j = 0; j < input[i].length; j++) {
|
||||
System.out.print(input[i][j] + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,132 +1,132 @@
|
|||
package org.instantsvm.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
|
||||
import au.com.bytecode.opencsv.CSVReader;
|
||||
import libsvm.svm;
|
||||
import libsvm.svm_model;
|
||||
import libsvm.svm_node;
|
||||
|
||||
public class LibSvmIO {
|
||||
public static final String DIR_DATASETS = "data/datasets/";
|
||||
public static final String DIR_MODELS = "data/models/";
|
||||
public static final String DEF_INPUT_NAME = "inputs.lsvm";
|
||||
public static final String DEF_CSV_INPUT_NAME = "inputs.csv";
|
||||
public static final String DEF_MODEL_NAME = "model.lsvm";
|
||||
|
||||
public static String defaultInputPath(String name) {
|
||||
return defaultInputPath(name, false);
|
||||
}
|
||||
|
||||
public static String defaultInputPath(String name, boolean isCsv) {
|
||||
if (isCsv)
|
||||
return defaultInputDirectory(name) + LibSvmIO.DEF_CSV_INPUT_NAME;
|
||||
else
|
||||
return defaultInputDirectory(name) + LibSvmIO.DEF_INPUT_NAME;
|
||||
}
|
||||
|
||||
public static String defaultModelPath(String name) {
|
||||
return defaultModelDirectory(name) + LibSvmIO.DEF_MODEL_NAME;
|
||||
}
|
||||
|
||||
public static String defaultModelDirectory(String name) {
|
||||
return LibSvmIO.DIR_MODELS + name + "/";
|
||||
}
|
||||
|
||||
public static String defaultInputDirectory(String name) {
|
||||
return LibSvmIO.DIR_DATASETS + name + "/";
|
||||
}
|
||||
|
||||
/** Load CSV format if file ends with ".csv", or LIBSVM data format otherwise. */
|
||||
public static RegressionInputs loadRegression(String filename) throws IOException {
|
||||
if (filename.endsWith(".csv")) {
|
||||
return loadRegressionCsv(filename);
|
||||
} else {
|
||||
return loadRegressionOriginalFormat(filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************/
|
||||
|
||||
public static RegressionInputs loadRegressionOriginalFormat(String filename) throws IOException {
|
||||
BufferedReader fp = new BufferedReader(new FileReader(filename));
|
||||
Vector<Double> vy = new Vector<Double>();
|
||||
Vector<svm_node[]> vx = new Vector<svm_node[]>();
|
||||
|
||||
while (true) {
|
||||
String line = fp.readLine();
|
||||
if (line == null)
|
||||
break;
|
||||
|
||||
StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
|
||||
|
||||
// load y value
|
||||
vy.addElement(atof(st.nextToken()));
|
||||
|
||||
// load x vector
|
||||
int m = st.countTokens() / 2;
|
||||
svm_node[] x = new svm_node[m];
|
||||
for (int j = 0; j < m; j++) {
|
||||
x[j] = new svm_node();
|
||||
x[j].index = atoi(st.nextToken());
|
||||
x[j].value = atof(st.nextToken());
|
||||
}
|
||||
vx.addElement(x);
|
||||
}
|
||||
fp.close();
|
||||
|
||||
return new RegressionInputs(vx, vy);
|
||||
}
|
||||
|
||||
public static svm_model loadModel(String filename) throws IOException {
|
||||
return svm.svm_load_model(filename);
|
||||
}
|
||||
|
||||
protected static double atof(String s) {
|
||||
double d = Double.valueOf(s).doubleValue();
|
||||
if (Double.isNaN(d) || Double.isInfinite(d))
|
||||
System.err.print("NaN or Infinity in input\n");
|
||||
return (d);
|
||||
}
|
||||
|
||||
protected static int atoi(String s) {
|
||||
return Integer.parseInt(s);
|
||||
}
|
||||
|
||||
/******************************************************/
|
||||
|
||||
public static RegressionInputs loadRegressionCsv(String filename) throws IOException {
|
||||
CSVReader reader = new CSVReader(new FileReader(filename));
|
||||
String[] nextLine;
|
||||
|
||||
Vector<svm_node[]> x = new Vector<svm_node[]>();
|
||||
Vector<Double> y = new Vector<Double>();
|
||||
while ((nextLine = reader.readNext()) != null) {
|
||||
int m = nextLine.length;
|
||||
|
||||
svm_node[] xk = new svm_node[m - 1];
|
||||
for (int i = 0; i < m - 1; i++)
|
||||
xk[i] = Conversion.toVector(i, Double.parseDouble(nextLine[i]));
|
||||
|
||||
x.add(xk);
|
||||
y.add(Double.parseDouble(nextLine[m - 1]));
|
||||
}
|
||||
reader.close();
|
||||
return new RegressionInputs(x, y);
|
||||
}
|
||||
|
||||
protected static int readNLines(String filename) throws IOException {
|
||||
CSVReader reader = new CSVReader(new FileReader(filename));
|
||||
int n = 0;
|
||||
while (reader.readNext() != null)
|
||||
n++;
|
||||
reader.close();
|
||||
return n;
|
||||
}
|
||||
}
|
||||
package org.instantsvm.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.instantsvm.regression.RegressionInputs;
|
||||
|
||||
import au.com.bytecode.opencsv.CSVReader;
|
||||
import libsvm.svm;
|
||||
import libsvm.svm_model;
|
||||
import libsvm.svm_node;
|
||||
|
||||
public class LibSvmIO {
|
||||
public static final String DIR_DATASETS = "data/datasets/";
|
||||
public static final String DIR_MODELS = "data/models/";
|
||||
public static final String DEF_INPUT_NAME = "inputs.lsvm";
|
||||
public static final String DEF_CSV_INPUT_NAME = "inputs.csv";
|
||||
public static final String DEF_MODEL_NAME = "model.lsvm";
|
||||
|
||||
public static String defaultInputPath(String name) {
|
||||
return defaultInputPath(name, false);
|
||||
}
|
||||
|
||||
public static String defaultInputPath(String name, boolean isCsv) {
|
||||
if (isCsv)
|
||||
return defaultInputDirectory(name) + LibSvmIO.DEF_CSV_INPUT_NAME;
|
||||
else
|
||||
return defaultInputDirectory(name) + LibSvmIO.DEF_INPUT_NAME;
|
||||
}
|
||||
|
||||
public static String defaultModelPath(String name) {
|
||||
return defaultModelDirectory(name) + LibSvmIO.DEF_MODEL_NAME;
|
||||
}
|
||||
|
||||
public static String defaultModelDirectory(String name) {
|
||||
return LibSvmIO.DIR_MODELS + name + "/";
|
||||
}
|
||||
|
||||
public static String defaultInputDirectory(String name) {
|
||||
return LibSvmIO.DIR_DATASETS + name + "/";
|
||||
}
|
||||
|
||||
/** Load CSV format if file ends with ".csv", or LIBSVM data format otherwise. */
|
||||
public static RegressionInputs loadRegression(String filename) throws IOException {
|
||||
if (filename.endsWith(".csv")) {
|
||||
return loadRegressionCsv(filename);
|
||||
} else {
|
||||
return loadRegressionOriginalFormat(filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************/
|
||||
|
||||
public static RegressionInputs loadRegressionOriginalFormat(String filename) throws IOException {
|
||||
BufferedReader fp = new BufferedReader(new FileReader(filename));
|
||||
Vector<Double> vy = new Vector<Double>();
|
||||
Vector<svm_node[]> vx = new Vector<svm_node[]>();
|
||||
|
||||
while (true) {
|
||||
String line = fp.readLine();
|
||||
if (line == null)
|
||||
break;
|
||||
|
||||
StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
|
||||
|
||||
// load y value
|
||||
vy.addElement(atof(st.nextToken()));
|
||||
|
||||
// load x vector
|
||||
int m = st.countTokens() / 2;
|
||||
svm_node[] x = new svm_node[m];
|
||||
for (int j = 0; j < m; j++) {
|
||||
x[j] = new svm_node();
|
||||
x[j].index = atoi(st.nextToken());
|
||||
x[j].value = atof(st.nextToken());
|
||||
}
|
||||
vx.addElement(x);
|
||||
}
|
||||
fp.close();
|
||||
|
||||
return new RegressionInputs(vx, vy);
|
||||
}
|
||||
|
||||
public static svm_model loadModel(String filename) throws IOException {
|
||||
return svm.svm_load_model(filename);
|
||||
}
|
||||
|
||||
protected static double atof(String s) {
|
||||
double d = Double.valueOf(s).doubleValue();
|
||||
if (Double.isNaN(d) || Double.isInfinite(d))
|
||||
System.err.print("NaN or Infinity in input\n");
|
||||
return (d);
|
||||
}
|
||||
|
||||
protected static int atoi(String s) {
|
||||
return Integer.parseInt(s);
|
||||
}
|
||||
|
||||
/******************************************************/
|
||||
|
||||
public static RegressionInputs loadRegressionCsv(String filename) throws IOException {
|
||||
CSVReader reader = new CSVReader(new FileReader(filename));
|
||||
String[] nextLine;
|
||||
|
||||
Vector<svm_node[]> x = new Vector<svm_node[]>();
|
||||
Vector<Double> y = new Vector<Double>();
|
||||
while ((nextLine = reader.readNext()) != null) {
|
||||
int m = nextLine.length;
|
||||
|
||||
svm_node[] xk = new svm_node[m - 1];
|
||||
for (int i = 0; i < m - 1; i++)
|
||||
xk[i] = Conversion.toVector(i, Double.parseDouble(nextLine[i]));
|
||||
|
||||
x.add(xk);
|
||||
y.add(Double.parseDouble(nextLine[m - 1]));
|
||||
}
|
||||
reader.close();
|
||||
return new RegressionInputs(x, y);
|
||||
}
|
||||
|
||||
protected static int readNLines(String filename) throws IOException {
|
||||
CSVReader reader = new CSVReader(new FileReader(filename));
|
||||
int n = 0;
|
||||
while (reader.readNext() != null)
|
||||
n++;
|
||||
reader.close();
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,50 +1,50 @@
|
|||
package org.jzy3d.demos.volume;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.jzy3d.analysis.AWTAbstractAnalysis;
|
||||
import org.jzy3d.analysis.AnalysisLauncher;
|
||||
import org.jzy3d.chart.factories.AWTChartFactory;
|
||||
import org.jzy3d.colors.Color;
|
||||
import org.jzy3d.colors.ColorMapper;
|
||||
import org.jzy3d.colors.colormaps.ColorMapRainbow;
|
||||
import org.jzy3d.maths.BoundingBox3d;
|
||||
import org.jzy3d.plot3d.primitives.volume.Texture3D;
|
||||
import org.jzy3d.plot3d.rendering.canvas.Quality;
|
||||
|
||||
import com.jogamp.opengl.util.GLBuffers;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jacok Filik
|
||||
*
|
||||
*/
|
||||
public class BasicVolumeDemo extends AWTAbstractAnalysis {
|
||||
public static void main(String[] args) throws Exception {
|
||||
AnalysisLauncher.open(new BasicVolumeDemo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
ColorMapper colorMapper = new ColorMapper(new ColorMapRainbow(), 0, 1, new Color(1, 1, 1, .5f));
|
||||
|
||||
ByteBuffer buffer = GLBuffers.newDirectByteBuffer(10 * 10 * 10 * 4);
|
||||
// make some kind of volume
|
||||
for (float x = 0; x < 2; x += 0.2) {
|
||||
for (float y = 0; y < 2; y += 0.2) {
|
||||
for (float z = 0; z < 2; z += 0.2) {
|
||||
buffer.putFloat((float) Math.sin(x * y * z));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Texture3D volume = new Texture3D(buffer, new int[] {10, 10, 10}, (float) 0, (float) 1,
|
||||
colorMapper, new BoundingBox3d(1, 10, 1, 10, 1, 10));
|
||||
|
||||
// Create a chart
|
||||
chart = AWTChartFactory.chart(Quality.Intermediate);
|
||||
chart.getScene().getGraph().add(volume);
|
||||
chart.getView();
|
||||
}
|
||||
}
|
||||
package org.jzy3d.demos.volume;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.jzy3d.analysis.AWTAbstractAnalysis;
|
||||
import org.jzy3d.analysis.AnalysisLauncher;
|
||||
import org.jzy3d.chart.factories.AWTChartFactory;
|
||||
import org.jzy3d.colors.Color;
|
||||
import org.jzy3d.colors.ColorMapper;
|
||||
import org.jzy3d.colors.colormaps.ColorMapRainbow;
|
||||
import org.jzy3d.maths.BoundingBox3d;
|
||||
import org.jzy3d.plot3d.primitives.volume.Texture3D;
|
||||
import org.jzy3d.plot3d.rendering.canvas.Quality;
|
||||
|
||||
import com.jogamp.opengl.util.GLBuffers;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jacok Filik
|
||||
*
|
||||
*/
|
||||
public class BasicVolumeDemo extends AWTAbstractAnalysis {
|
||||
public static void main(String[] args) throws Exception {
|
||||
AnalysisLauncher.open(new BasicVolumeDemo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
ColorMapper colorMapper = new ColorMapper(new ColorMapRainbow(), 0, 1, new Color(1, 1, 1, .5f));
|
||||
|
||||
ByteBuffer buffer = GLBuffers.newDirectByteBuffer(10 * 10 * 10 * 4);
|
||||
// make some kind of volume
|
||||
for (float x = 0; x < 2; x += 0.2) {
|
||||
for (float y = 0; y < 2; y += 0.2) {
|
||||
for (float z = 0; z < 2; z += 0.2) {
|
||||
buffer.putFloat((float) Math.sin(x * y * z));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Texture3D volume = new Texture3D(buffer, new int[] {10, 10, 10}, (float) 0, (float) 1,
|
||||
colorMapper, new BoundingBox3d(1, 10, 1, 10, 1, 10));
|
||||
|
||||
// Create a chart
|
||||
chart = AWTChartFactory.chart(Quality.Intermediate);
|
||||
chart.getScene().getGraph().add(volume);
|
||||
chart.getView();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,69 +1,69 @@
|
|||
package org.jzy3d.demos.waterfall;
|
||||
|
||||
import org.jzy3d.analysis.AWTAbstractAnalysis;
|
||||
import org.jzy3d.analysis.AnalysisLauncher;
|
||||
import org.jzy3d.chart.factories.AWTChartFactory;
|
||||
import org.jzy3d.colors.Color;
|
||||
import org.jzy3d.colors.ColorMapper;
|
||||
import org.jzy3d.colors.colormaps.ColorMapRainbow;
|
||||
import org.jzy3d.plot3d.primitives.vbo.ShaderWaterfallDrawableVBO;
|
||||
import org.jzy3d.plot3d.primitives.vbo.ShaderWaterfallVBOBuilder;
|
||||
import org.jzy3d.plot3d.rendering.canvas.Quality;
|
||||
|
||||
/**
|
||||
* Demonstrate how to use vertex and fragment shaders.
|
||||
*
|
||||
* @author Jacok Filik
|
||||
*/
|
||||
public class BigWaterfallDemo extends AWTAbstractAnalysis {
|
||||
public static void main(String[] args) throws Exception {
|
||||
AnalysisLauncher.open(new BigWaterfallDemo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
float[] x = new float[1000];
|
||||
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
x[i] = -3f + 6f * ((float) i / (x.length - 1));
|
||||
}
|
||||
|
||||
float[] y = new float[100];
|
||||
|
||||
for (int i = 0; i < y.length; i++) {
|
||||
y[i] = -3f + 2f * ((float) i / (y.length - 1));
|
||||
}
|
||||
|
||||
float[] z = getZ(x, y);
|
||||
|
||||
ColorMapper colorMapper =
|
||||
new ColorMapper(new ColorMapRainbow(), -2, 2, new Color(1, 1, 1, .5f));
|
||||
|
||||
ShaderWaterfallVBOBuilder builder = new ShaderWaterfallVBOBuilder(x, y, z,
|
||||
new ColorMapper(new ColorMapRainbow(), -1, 1, new Color(1, 1, 1, .5f)));
|
||||
ShaderWaterfallDrawableVBO shape = new ShaderWaterfallDrawableVBO(builder, colorMapper);
|
||||
builder.earlyInitalise(shape);
|
||||
|
||||
|
||||
// Create a chart
|
||||
chart = AWTChartFactory.chart(Quality.Intermediate);
|
||||
chart.getScene().getGraph().add(shape);
|
||||
chart.getView();
|
||||
}
|
||||
|
||||
private float[] getZ(float[] x, float[] y) {
|
||||
float[] z = new float[x.length * y.length];
|
||||
|
||||
for (int i = 0; i < y.length; i++) {
|
||||
for (int j = 0; j < x.length; j++) {
|
||||
z[j + (x.length * i)] = (float) f((double) x[j], (double) y[i]);
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
private double f(double x, double y) {
|
||||
return x * Math.sin(x * y);
|
||||
}
|
||||
}
|
||||
package org.jzy3d.demos.waterfall;
|
||||
|
||||
import org.jzy3d.analysis.AWTAbstractAnalysis;
|
||||
import org.jzy3d.analysis.AnalysisLauncher;
|
||||
import org.jzy3d.chart.factories.AWTChartFactory;
|
||||
import org.jzy3d.colors.Color;
|
||||
import org.jzy3d.colors.ColorMapper;
|
||||
import org.jzy3d.colors.colormaps.ColorMapRainbow;
|
||||
import org.jzy3d.plot3d.primitives.vbo.ShaderWaterfallDrawableVBO;
|
||||
import org.jzy3d.plot3d.primitives.vbo.ShaderWaterfallVBOBuilder;
|
||||
import org.jzy3d.plot3d.rendering.canvas.Quality;
|
||||
|
||||
/**
|
||||
* Demonstrate how to use vertex and fragment shaders.
|
||||
*
|
||||
* @author Jacok Filik
|
||||
*/
|
||||
public class BigWaterfallDemo extends AWTAbstractAnalysis {
|
||||
public static void main(String[] args) throws Exception {
|
||||
AnalysisLauncher.open(new BigWaterfallDemo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
float[] x = new float[1000];
|
||||
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
x[i] = -3f + 6f * ((float) i / (x.length - 1));
|
||||
}
|
||||
|
||||
float[] y = new float[100];
|
||||
|
||||
for (int i = 0; i < y.length; i++) {
|
||||
y[i] = -3f + 2f * ((float) i / (y.length - 1));
|
||||
}
|
||||
|
||||
float[] z = getZ(x, y);
|
||||
|
||||
ColorMapper colorMapper =
|
||||
new ColorMapper(new ColorMapRainbow(), -2, 2, new Color(1, 1, 1, .5f));
|
||||
|
||||
ShaderWaterfallVBOBuilder builder = new ShaderWaterfallVBOBuilder(x, y, z,
|
||||
new ColorMapper(new ColorMapRainbow(), -1, 1, new Color(1, 1, 1, .5f)));
|
||||
ShaderWaterfallDrawableVBO shape = new ShaderWaterfallDrawableVBO(builder, colorMapper);
|
||||
builder.earlyInitalise(shape);
|
||||
|
||||
|
||||
// Create a chart
|
||||
chart = AWTChartFactory.chart(Quality.Intermediate);
|
||||
chart.getScene().getGraph().add(shape);
|
||||
chart.getView();
|
||||
}
|
||||
|
||||
private float[] getZ(float[] x, float[] y) {
|
||||
float[] z = new float[x.length * y.length];
|
||||
|
||||
for (int i = 0; i < y.length; i++) {
|
||||
for (int j = 0; j < x.length; j++) {
|
||||
z[j + (x.length * i)] = (float) f((double) x[j], (double) y[i]);
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
private double f(double x, double y) {
|
||||
return x * Math.sin(x * y);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue