I am struggling to actually implement the classifying part of my investigations into the possibility of classifying music according to some features of music files.
What I have currently produced is code that reads a table of features from the DB and then puts it back into the DB in another table.
The problem is that I do not know how to work with the instances type. Documentation is crap - I have no clue what to do.
What I want to do: I want to use a given set of music files and compute their feature vectors. After this data has been put into arff, I would manually join it with genre data (the gial i.e.). and then save it into a MySQL table.
AFAIU the chain should be like this:
Read from DB
Somehow train a K-nearest neighbor classifier on a set of the features (related to genre) per music file for a body of 10 files.
Use this to classify a set of files with the same features but unknown genre.
- Somehow output results so that they can be machine-readable in the database.
I have found no examples of the output of the data actually being used for further processing so I cannot further haggle :/
After this has been done, I would like to read it back and conduct a classification on a new body of music (the features I have computed by music or using a sample file set). The results should be put back into the DB in yet another new table, detailing what file has which category (assigned).
Here is my code:
package org.tuhh.cpmgg.weka;
import weka.core.*;
import weka.core.converters.*;
import weka.experiment.InstanceQuery;
import java.io.*;
import java.util.ArrayList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/weka2")
public class weka_chain {
/**
* loads a dataset from mysql db
* @param args the commandline arguments
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String main()
throws Exception {
java.util.List resultList;
/*Gets data from DB*/
InstanceQuery query = new InstanceQuery();
query.setDatabaseURL("jdbc:mysql://127.2.73.130:3306/cpmgg");
query.setUsername("adminnNWqHkW");
query.setPassword("zLlkWsd-NsnQ");
query.setQuery("SELECT * FROM features"); //Read table
Instances data = query.retrieveInstances(); //into data
data.setClassIndex(data.numAttributes() - 1); //sets the number of classes (creates index)
/*Classifiers */
String algorithm = "weka.classifiers.bayes.NaiveBayes"; // Sets the type of classifier (many available)
resultList = new ArrayList();
Weka1 weka;
try {
weka = new Weka1(algorithm, "lol");
resultList = weka.weka(algorithm, data); //Essentially what is happening
/* TODO:
* Define Output so that it is in table form/instance form
* This means creating output using the old applet and somehow (?) distilling it into table shape
*/
/* Saves Results to DB */
DatabaseSaver save = new DatabaseSaver();
// save.setUrl("jdbc:mysql://localhost:3306/weka_test");
save.setUrl("jdbc:mysql://127.2.73.130:3306/cpmgg");
//save.setUrl("jdbc:mysql://localhost:3306/hibernate");
save.setUser("AMDINADMIN");
save.setPassword("PASS_ PASS");
save.setInstances(data); // define outputtype
save.setRelationForTableName(false);
save.setTableName("weka_rslts");
save.connectToDatabase();
save.writeBatch();
return "done";
}
}