I'm searching for a library or an example on how to implement in java a likelihood ratio test like in matlab.
I have two different vector of double values and want to receive a scalar value.
Every value correspond to a feature for my machine learning algorithm so one the first vector is the training pattern and the second one a test.
Could you please help me?
On matlab i just use division on two matrix like LR= test_matrix/training_matrix
I've tryied with apache mahout but i'm not sure i'm using it correctly.
Here the code:
FastByIDMap<FastByIDMap<Long>> timestamps = new FastByIDMap<>();
Collection<Preference> prefs = new ArrayList<>(2);
FastByIDMap<Collection<Preference>> data = new FastByIDMap<>(); //Preferecens for user0
Preference newPrefs = new GenericPreference(0, 0, (float) -0.5);
Preference pref = new GenericPreference(0, 1, 50);
Preference pref2 = new GenericPreference(0, 2, 51);
prefs.add(newPrefs);
prefs.add(pref);
prefs.add(pref2);
data.put(0, prefs);
Collection<Preference> prefs_1 = new ArrayList<>(2);
newPrefs = new GenericPreference(1, 0, (float) -0.5);
pref = new GenericPreference(1, 1, 50);
pref2 = new GenericPreference(1, 2, 51);
prefs_1.add(newPrefs);
prefs_1.add(pref);
prefs_1.add(pref2);
data.put(1, prefs_1);
GenericDataModel model = new GenericDataModel(GenericDataModel.toDataMap(data, true), timestamps);
FastByIDMap<PreferenceArray> us = model.getRawUserData();
System.out.println("us:"+ us.toString());
LogLikelihoodSimilarity l = new LogLikelihoodSimilarity(model);
System.out.println(l.userSimilarity(0, 1));
In this case, user similarity alwasy return 0.
Related
I am developing an Application in Android Studio to that prints how many of each item you can buy with the given amount of currency. It printed flawlessly when run as a Java program in Eclipse but I can not get it to print more than one line in the TextView Box.
I've noticed it will pick the most Expensive item you can afford 1 of and print it alone, leading me to believe it runs through the list and only prints the last one that passes as affordable. I've read about needing to use a StringBuilder and such but have found little information on how to convert my Array.asList over to this. Here is my code.
gCalc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText diamondInput = (EditText) findViewById(R.id.diamondInput);
try {
int diamonds = Integer.parseInt(diamondInput.getText().toString());
List<Gifts> gift = Arrays.asList(new Gifts[]{new Gifts("Gold star", 10), new Gifts("Love Bear", 10), new Gifts("Lillies", 10), new Gifts("Box Of Chocolate", 20), new Gifts("Taco", 20), new Gifts("Thumbs Up", 30), new Gifts("Panda", 40), new Gifts("Beer", 40), new Gifts("Patriot", 52), new Gifts("Eagle", 52), new Gifts("Gold Chain", 80), new Gifts("Roses", 100), new Gifts("Champagne", 100), new Gifts("Snow", 100), new Gifts("Candy", 100), new Gifts("Kiss", 200), new Gifts("Candy Hearts", 250), new Gifts("Peach", 300), new Gifts("EggPlant", 300), new Gifts("Fireworks", 500), new Gifts("GemDrop", 600), new Gifts("Crown", 600), new Gifts("Cupcakes", 700), new Gifts("Heart Balloon", 800), new Gifts("Sports Car", 1000), new Gifts("Smoke Rings", 1000), new Gifts("purple Diamond", 2500), new Gifts("Cupid", 5000), new Gifts("Gold Watch", 5000), new Gifts("Castle", 5000), new Gifts("Yacht", 10000), new Gifts("Jet", 20000)});
double coins = (double) diamonds / 2.5D;
Iterator var5 = gift.iterator();
while(var5.hasNext()) {
Gifts Gifts = (Gifts)var5.next();
int qty = (int)Gifts.getQty(coins);
if(qty > 0) {
result.setText("You can buy " + qty + " " + Gifts.name);
}
}
}
catch (Exception e) {
// friendly error to the user: field is incorrect
}
I need it to print as this EX:
You can buy X amount of Y
You can buy X amount of Y
You can buy X amount of Y
:end
Printing every item that can be bought and it's quantity.
If you look into Android API documentation (https://developer.android.com/reference/android/widget/TextView.html) , this is what it states:
void setText (CharSequence text) - sets the text to be displayed. - this means that each time you call this method, new text overrides the old one.
It seems that you are looking for append() method:
void append(CharSequence text) - convenience method to append the specified text to the TextView's display buffer, upgrading it to EDITABLE if it was not already editable. append() method doesn't override previously set text.
Another way to append text to the previously stored in TextView is to combine these methods:
result.setText(result.getText() + "text that you want to append to the previous one")
I am working on a project and I need some examples how to implement RandomForest in Java with weka? I did it with IBk(), it worked. If I do it with RandomForest in the same way, it does not work.
Does anyone have a simple example for me how to implement RandomForest and how to get probability for each class (i did it with IBk withclassifier.distributionForInstance(instance) Function and it returned me probabilities for each class). How can I do it for RandomForest? I will need to get probability of every tree and to combine it?
//example
ConverrterUtils.DataSource source = new ConverterUtils.DataSource ("..../edit.arff);
Instances dataset = source.getDataSet();
dataset.setClassIndex(dataset.numAttributes() - 1);
IBk classifier = new IBk(5); classifier.buildClassifier(dataset);
Instance instance = new SparseInstance(2);
instance.setValue(0, 65) //example data
instance.setValue(1, 120); //example data
double[] prediction = classifier.distributionForInstance(instance);
//now I get the probability for the first class
System.out.println("Prediction for the first class is: "+prediction[0]);
You can calculate the the infogain while buidling the Model in the RandomForest. It is much slower and requires alot of memory while buidling model. I am not so sure about the documentation. you can add options or setValues while buiilding the model.
//numFolds in number of crossvalidations usually between 1-10
//br is your bufferReader
Instances trainData = new Instances(br);
trainData.setClassIndex(trainData.numAttributes() - 1);
RandomForest rf = new RandomForest();
rf.setNumTrees(50);
//You can set the options here
String[] options = new String[2];
options[0] = "-R";
rf.setOptions(options);
rf.buildClassifier(trainData);
weka.filters.supervised.attribute.AttributeSelection as = new weka.filters.supervised.attribute.AttributeSelection();
Ranker ranker = new Ranker();
InfoGainAttributeEval infoGainAttrEval = new InfoGainAttributeEval();
as.setEvaluator(infoGainAttrEval);
as.setSearch(ranker);
as.setInputFormat(trainData);
trainData = Filter.useFilter(trainData, as);
Evaluation evaluation = new Evaluation(trainData);
evaluation.crossValidateModel(rf, trainData, numFolds, new Random(1));
// Using HashMap to store the infogain values of the attributes
int count = 0;
Map<String, Double> infogainscores = new HashMap<String, Double>();
for (int i = 0; i < trainData.numAttributes(); i++) {
String t_attr = trainData.attribute(i).name();
//System.out.println(i+trainData.attribute(i).name());
double infogain = infoGainAttrEval.evaluateAttribute(i);
if(infogain != 0){
//System.out.println(t_attr + "= "+ infogain);
infogainscores.put(t_attr, infogain);
count = count+1;
}
}
//iterating over the hashmap
Iterator it = infogainscores.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey()+" = "+pair.getValue());
System.out.println(pair.getKey()+" = "+pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
I am using this command line
java -cp weka.jar weka.classifiers.trees.RandomForest -T tdata.arff -l rndforrest.model -p 0 > data.out
But I want to do it in java without using files, everything should be on the fly. The model can be loaded once at the beginning and the tdata.arff should be one data row for which I need the prediction (classification?).
Like this:
weka.classifiers.Classifier rndForrest = (weka.classifiers.Classifier)weka.core.SerializationHelper.read("rndforrest.model");
var dataInst = new weka.core.Instance(1, new double[] { 0, 9, -96, 62, 1, 200, 35, 1 });
double pred = rndForrest.classifyInstance(dataInst);
I get an error
Instance doesn't have access to a dataset!
Thank you for help.
edit: my code
Stopwatch sw = new Stopwatch();
sw.Start();
var values = new double[] { 0, 9, -96, 62, 1, 200, 35, 0 };
weka.classifiers.Classifier rndForrest = (weka.classifiers.Classifier)weka.core.SerializationHelper.read("rndforrest.model");
var dataInst = new weka.core.Instance(1, values);
FastVector atts = new FastVector();
for(int i=0; i < values.Length; i++) {
atts.addElement(new weka.core.Attribute("att" + i));
}
weka.core.Instances data = new Instances("MyRelation", atts, 0);
data.add(dataInst);
data.setClassIndex(data.numAttributes() - 1);
double pred = rndForrest.classifyInstance(data.firstInstance());
Console.WriteLine("prediction is " + pred);
Console.WriteLine(sw.ElapsedMilliseconds);
Well, the error says it, doesn't it?
Instances doesn't have access to a dataset!
The Javadoc for the constructor you use says:
public Instance(double weight, double[] attValues)
Constructor that inititalizes instance variable with given values. Reference to the dataset is set to null. (ie. the instance doesn't have access to information about the attribute types)
Every Instance has to belong to a data set (Instances), because in Weka each value of an instance is stored as a double value. Additional information is needed to determine how to interpret that double value (e.g. as double, string, nominal, ...) and this information is provided through the data set.
You need to do something like:
FastVector atts = new FastVector();
// assuming all your eight attributes are numeric
for( int i = 1; i <= 8; i++ ) {
atts.addElement(new Attribute("att" + i)); // - numeric
}
Instances data = new Instances("MyRelation", atts, 0);
data.add(dataInst);
(Also see Creating an ARFF file for additional examples on how to create attributes of a certain type)
I am doing a classification with weka, I tried to use the filter.removeuseless, but with the same arff file, I found some differences betweeen using that in the code and in the GUI. In the code I invoked it in this way:
Normalize norm = new Normalize();
norm.setInputFormat(train);
Instances train_norm = Filter.useFilter(train, norm);
RemoveUseless ru = new RemoveUseless();
ru.setInputFormat(train_norm);
Instances train_new = Filter.useFilter(train_norm, ru);
Ranker rank = new Ranker();
InfoGainAttributeEval eval = new InfoGainAttributeEval();
eval.buildEvaluator(train_new);
The result is "strange" because the filter deleted a lot of attributes which the GUI kept as informative for the classification. (The filter in the GUI worked very well). What is the problem? Am I using it well in the code?
i solved like this:
Normalize norm = new Normalize();
norm.setInputFormat(train);
train = Filter.useFilter(train, norm);
RemoveUseless ru = new RemoveUseless();
ru.setInputFormat(train);
train = Filter.useFilter(train, ru);
Ranker rank = new Ranker();
InfoGainAttributeEval eval = new InfoGainAttributeEval();
eval.buildEvaluator(train);
Currently I am loading one tile data-layer over an OSMdroid basemap with
final MapTileProviderBasic tileProvider =
new MapTileProviderBasic(getApplicationContext());
final ITileSource tileSource =
new XYTileSource("MyCustomTiles", null, 1, 16, 256, ".png",
"http://a.url.to/custom-tiles/");
tileProvider.setTileSource(tileSource);
final TilesOverlay tilesOverlay =
new TilesOverlay(tileProvider, this.getBaseContext());
tilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT);
osmv.getOverlays().add(tilesOverlay);
Is it possible to render multiple data layers on top of each other over the BaseMap or can I only display one data layer at a time?
I found this example for GoogleMaps, but haven't found some example OSMdroid code dealing with multipe tileSources at a time.
Yes, of course you can. You just have to add another TilesOverlay to the map. The overlays(also tilesOverlays) get drawn consecutively, starting at the list's lowest index(=0).
Here's an example:
//create the first tilesOverlay
final MapTileProviderBasic tileProvider = new MapTileProviderBasic(getApplicationContext());
final ITileSource tileSource = new XYTileSource("MyCustomTiles", null, 1, 16, 256, ".png",
"http://a.url.to/custom-tiles/");
tileProvider.setTileSource(tileSource);
final TilesOverlay tilesOverlay = new TilesOverlay(tileProvider, this.getBaseContext());
tilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT);
//create the second one
final MapTileProviderBasic anotherTileProvider = new MapTileProviderBasic(getApplicationContext());
final ITileSource anotherTileSource = new XYTileSource("MyCustomTiles", null, 1, 16, 256, ".png",
"http://a.secondurl.to/custom-tiles/");
anotherTileProvider.setTileSource(anotherTileSource);
final TilesOverlay secondTilesOverlay = new TilesOverlay(anotherTileProvider, this.getBaseContext());
secondTilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT);
// add the first tilesOverlay to the list
osmv.getOverlays().add(tilesOverlay);
// add the second tilesOverlay to the list
osmv.getOverlays().add(secondTilesOverlay);