How does java generate R boxplot via RCaller? - java

I want to generate a R boxplot by using rcaller with java.
My code is :
try {
RCaller caller = new RCaller();
caller.setRExecutable("/usr/bin/R");
caller.setGraphicsTheme(new DefaultTheme());
RCode code = new RCode();
code.clear();
File file = code.startPlot();
code.addRCode("boxplot((1:10),main=\"1-10\")");
System.out.println(code.toString());
code.endPlot();
caller.setRCode(code);
caller.runAndReturnResultOnline("boxplot(1:10),main=\"1-10\"");
code.showPlot(file);
But it does not keep run on codecaller.runAndReturnResultOnline("boxplot(1:10),main=\"1-10\"");
i try to use code below ,that can plot a R plot.what differences between them ?
try {
RCaller caller = new RCaller();
caller.setRExecutable("/usr/bin/R");
caller.setGraphicsTheme(new DefaultTheme());
RCode code = new RCode();
code.clear();
double[] numbers = new double[] { 1, 4, 4, 5, 6, 10 };
code.addDoubleArray("x", numbers);
File file = code.startPlot();
System.out.println(file.toString());
code.addRCode("plot.ts(x)");
System.out.println(code.toString());
code.endPlot();
caller.setRCode(code);
caller.runAndReturnResultOnline("plot.ts(x)");
code.showPlot(file);

Replace your line :
code.addRCode("boxplot((1:10),main=\"1-10\")");
By this :
code.addRCode("boxplot(c(1:10),main='1-10')");
Or as in the second example (the working one), you can give your x vector from java and replace this line:
caller.runAndReturnResultOnline("plot.ts(x)");
by
caller.runAndReturnResultOnline("boxplot(x)");
PS: I dont' have java to test.

Related

org.deeplearning4j.exception.DL4JInvalidInputException : Input that is not a matrix; expected matrix (rank 2), got rank 3 array

I have been trying to build a CNN model using dl4j, but it is giving me an error. The code:
RecordReader rr;
rr = new CSVRecordReader();
rr.initialize(new FileSplit(new File(dataLocalPath, nameTrain)));
DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, batchSize, 0, 2);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(123).updater(new Adam(0.1))
.list()
.layer(0,new Convolution1DLayer.Builder().kernelSize(3).padding(1).nIn(371).nOut(64).build())
.layer(1,new Subsampling1DLayer.Builder().kernelSize(3).padding(1).build())
.layer(2,new Convolution1DLayer.Builder().kernelSize(3).activation(Activation.RELU).padding(1).nIn(64)
.nOut(32).build())
.layer(3,new Subsampling1DLayer.Builder().kernelSize(3).padding(1).build())
.layer(4,new DenseLayer.Builder().activation(Activation.RELU).nIn(32).nOut(16).build())
.layer(5,new OutputLayer.Builder(LossFunction.RECONSTRUCTION_CROSSENTROPY).activation(Activation.SIGMOID)
.nIn(16).nOut(2).build())
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
model.setListeners(new ScoreIterationListener(10));
final DataSet trainData = trainIter.next();
INDArray a = trainData.getFeatures();
final INDArray b = trainData.getLabels();
a = a.reshape(new int[] { (int) a.size(0), (int) a.size(1), 1 });
model.fit(a, b);
Added the error below,
Exception in thread "main" org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 3 array with shape [128, 32, 1]. Missing preprocessor or wrong input type? (layer name: layer4, layer index: 4, layer type: DenseLayer)
at org.deeplearning4j.nn.layers.BaseLayer.preOutputWithPreNorm(BaseLayer.java:306)
at org.deeplearning4j.nn.layers.BaseLayer.preOutput(BaseLayer.java:289)
at org.deeplearning4j.nn.layers.BaseLayer.activate(BaseLayer.java:337)
at org.deeplearning4j.nn.layers.AbstractLayer.activate(AbstractLayer.java:257)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.ffToLayerActivationsInWs(MultiLayerNetwork.java:1129)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2741)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2699)
at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:170)
at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:63)
at org.deeplearning4j.optimize.Solver.optimize(Solver.java:52)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fitHelper(MultiLayerNetwork.java:2303)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:2261)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:2248)
at com.rssoftware.efrm.AnnModelFromKeras.trainModel(AnnModelFromKeras.java:73)
at com.rssoftware.efrm.AnnModelFromKeras.main(AnnModelFromKeras.java:89)
I have tried using input pre-processor, CNN to feed forward pre processor, but it is not working.
The expected input shape into a conv1d layer is [minibatchSize, convNIn, length] or [minibatchSize, featuresSize, sequenceLength] in terms of a time series. The reshape in your code sets your length to 1. Maybe you intended to set featuresize/convNIn to 1?

What format is this file written on?

I'm messing around with Auctionator (a WoW addon for the auction house). My application is still in development but out of curious i want to know the name for this format.
D:\Blizzard\World of Warcraft\WTF\Account\54621418#1\SavedVariables\Auctionator.lua
AUCTIONATOR_PRICE_DATABASE = {
["__dbversion"] = 4,
["Ragnaros_Horde"] = {
["Kraken's Eye of Agility"] = {
["mr"] = 6019998,
["cc"] = 3,
["H2935"] = 6019998,
["id"] = "153708:0:0:0:0",
["sc"] = 1,
},
["Tidespray Linen Pants of the Harmonious"] = {
["mr"] = 2930810,
["sc"] = 1,
["id"] = "154689:0:0:0:1715",
["L2926"] = 2930810,
["H2926"] = 19698294,
["cc"] = 4,
},
},
}
I ended up parsing the file with lots of indexOf(..) and Patters and Matchers because i couldn't find this format anywhere. Here's a screenshot of the application if you wanna see it.
A LUA file is a source code file written in Lua, a light-weight programming language designed for extending applications. It can be compiled into a program using an ANSI C compiler.
Your file looks like a table/config details
More you can have a look on https://en.wikipedia.org/wiki/Lua_(programming_language)

Neuroph cannot train set

I've been trying so hard to train a network but I cannot do it. Neuroph Studio doesn't help at all, it always return null when training.
Then I tried this code in a Java app :
// create new perceptron network
NeuralNetwork neuralNetwork = new Perceptron(2, 1);
// create training set
DataSet trainingSet = new DataSet(2, 1);
// add training data to training set (logical OR function)
trainingSet.addRow(new DataSetRow(new double[]{0, 0}, new double[]{0.5d}));
trainingSet.addRow(new DataSetRow(new double[]{0, 1}, new double[]{1}));
trainingSet.addRow(new DataSetRow(new double[]{1, 0}, new double[]{1}));
trainingSet.addRow(new DataSetRow(new double[]{1, 1}, new double[]{1}));
// learn the training set
neuralNetwork.learn(trainingSet);
// save the trained network into file
neuralNetwork.save("or_perceptron.nnet");
// load the saved network
neuralNetwork = NeuralNetwork.createFromFile("or_perceptron.nnet");
// set network input
neuralNetwork.setInput(1, 1);
// calculate network
neuralNetwork.calculate();
// get network output
double[] networkOutput = neuralNetwork.getOutput();
for (double res : networkOutput) {
System.out.println(res);
}
This works, but I want to train something like this:
Input: 0.3 , 0.5
Output : 0.2
It keeps training forever, what is wrong with neuroph, or it doesn't work at all ?
At the end the only thing that worked was to load the training set from external files. Maybe there is another solution, but that was the only thing that worked for me at the end.

Can't get RCaller to to run simple example

I am trying to run this simple example of RCaller on my windows 7 machine.
RCaller caller = new RCaller();
RCode code = new RCode();
caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.2.1\\bin\\Rscript.exe");
double[] numbers = new double[]{1, 4, 3, 5, 6, 10};
code.addDoubleArray("x", numbers);
code.addRCode("my.mean<-mean(x)");
code.addRCode("my.var<-var(x)");
code.addRCode("my.all<-list(mean=my.mean, variance=my.var)");
caller.setRCode(code);
caller.runAndReturnResult("my.all");
double[] results = caller.getParser().getAsDoubleArray("mean");`
System.out.println(results[0]);
This is the error message:
cat(makexml(obj=my.all, name="my.all"), file="C:/Users/Bob Smith/AppData/Local/Temp/Routput8089051805366000971")
rcaller.exception.ParseException: Can not handle R results due to : rcaller.exception.ParseException: Can not parse output: The generated file C:\Users\Bob Smith\AppData\Local\Temp\Routput8089051805366000971 is empty
I've tried several versions of RCaller (this example is 2.2.0) and have received similar errors. Does anyone know how to fix this?

labeling an unlabeled instance in Weka(java code)

I am beginner in java and Weka tool, I want to use Logitboost algorithm with DecisionStump as weak learner in my java code, but I don't know how do this work. I create a vector with six feature(without label feature) and I want feed it into logitboost for labeling and probability of its assignment. Labels are 1 or -1 and train/test data is in an arff file.This is my code, but algorithm always return 0 !
Thanks
double candidate_similarity(ha_nodes ha , WeightMatrix[][] wm , LogitBoost lgb ,ArrayList<Attribute> atts){
LogitBoost lgb = new LogitBoost();
lgb.buildClassifier(newdata);//newdata is an arff file with some labeled data
Evaluation eval = new Evaluation(newdata);
eval.crossValidateModel(lgb, newdata, 10, new Random(1));
try {
feature_vector[0] = IP_sim(Main.a_new.dip, ha.candidate.dip_cand);
feature_vector[1] = IP_sim(Main.a_new.sip, ha.candidate.sip_cand);
feature_vector[2] = IP_s_d_sim(Main.a_new.sip, ha);
feature_vector[3] = Dport_sim(Main.a_new.dport, ha);
freq_weight(Main.a_new.Atype, ha, freq_avg, weight_avg , wm);
feature_vector[4] = weight_avg;
feature_vector[5] = freq_avg;
double[] values = new double[]{feature_vector[0],feature_vector[1],feature_vector[2],feature_vector[3],feature_vector[4],feature_vector[5]};
DenseInstance newInst = new DenseInstance(1.0,values);
Instances dataUnlabeled = new Instances("TestInstances", atts, 0);
dataUnlabeled.add(newInst);
dataUnlabeled.setClassIndex(dataUnlabeled.numAttributes() - 1);
double clslable = lgb.classifyInstance(inst);
} catch (Exception ex) {
//Logger.getLogger(Module2.class.getName()).log(Level.SEVERE, null, ex);
}
return clslable;}
Where did this newdata come from? you need to load the file properly to get a correct classification, use this class to load features from the file:
http://weka.sourceforge.net/doc/weka/core/converters/ArffLoader.html
I'm not posting an example code because I use weka with MATLAB, so I dont have examples in Java.

Categories