[Update]
I'm new in weka. I want to add my double[] array
to my weka Instances dataRaw
but I have no idea how to do it.
This is my Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.core.DenseInstance;
import weka.core.Instances;
public class SVMTest
{
private Connection connect;
public SVMTest() throws Exception
{
try
{
String jdbcDriver ="org.gjt.mm.mysql.Driver";
String jdbcURL = "jdbc:mysql://localhost:3306/xign?";
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/myDB?"
+ "user=" + "root" + "&password=" +
"xxx###111");
} catch (ClassNotFoundException ex)
{
Logger.getLogger(SVMTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
public ArrayList<Double[]> loadValues(String generatedString) throws SQLException
{
ArrayList<Double[]> pictures = new ArrayList<>();
PreparedStatement ps = null;
ResultSet rs = null;
Double picture[] = new Double[3];
try
{
ps = connect.prepareStatement("SELECT X, Y, Z FROM myDB.Sensor WHERE key = ?");
ps.setString(1, generatedString);
rs = ps.executeQuery();
while(rs.next())
{
picture[0] = (rs.getDouble("X") * 100000);
picture[1] = (rs.getDouble("Y") * 100000);
picture[2] = (rs.getDouble("Z") * 100000);
pictures.add(picture);
picture = new Long[3];
}
}
catch (SQLException ex)
{
Logger.getLogger(SVMTest.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
if(rs != null )
try{ rs.close(); }
catch(SQLException ex) { ex.printStackTrace(); }
if(ps != null)
try{ ps.close(); }
catch(SQLException ex) { ex.printStackTrace(); }
}
return pictures;
}
public double [] toRawArray(Double[] array)
{
double[] out = new double[array.length];
for(int i = 0; i < array.length; i++)
{
out[i] = array[i];
}
return out;
}
public static void main(String[] args) throws Exception
{
SVMTest svm = new SVMTest();
ArrayList<Double[]> myValues = svm.loadValues("123456ASDF");
//at this point I want to add ArrayList<Double[]> myValues to
//weka Instances to classify the data but I don't really have
//an idea
Instances dataRaw = new Instances(?????); <--Error
for(Double[] a : myValues)
{
DenseInstance myDense = new DenseInstance(1.0, toRawArray(a));
dataRaw.add((Instance)myDense.dataset());
}
}
}
Double[] a
looks like this:
for(Double[] a : alValues)
{
for(Double b : a))
{
System.out.print("[" + b + "]");
}
System.out.println();
}
//Output:
//[-1198.54][8534.44][4293.29]
//[-994.13][8812.43][3534.66]
//[-818.84][9026.96][2915.99]
//[-670.76][9186.82][2436.73]
Just basic explanation :- First, to classify you need a model and to get a model you need to train an algorithm on data with attributes and classIndex.
Attributes is "Type of data", assuming if you have data of employees then name, desgination, age , salary etc are attributes or in simple terms column names in csv file.
Type of data can be Numeric (Integer or Real) or Nominal meaning normal string.
Classindex is the attribute/column index which you want your algorithm to predict/classify based on training instances. For example you can predict salary using age and designation.
After model is generated, on that model you can do classification (prediction) by sending data in similar format meaning instance created with same attributes and classindex.
You need to be sure about which algorithm you want to run and which attribute/column index you want to predict.
[Note :- There are algorithms which work with only numeric data and some other algorithms work only on Nominal data and some algorithm will work on both types of data. So you should pick an algorithm depending on type of data. There are other stuffs which you should check before selecting an algorithm but basic is the type of data.]
I suggest you to go through about machine learning and weka before attempting to run an algorithm.
Sample code which you can try and I am assuming your classindex to be z
:-
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(new Attribute("x"));
attributes.add(new Attribute("y"));
attributes.add(new Attribute("z"));
Instances dataRaw = new Instances("TestInstances", attributes , 0);
dataRaw.setClassIndex(dataRaw.numAttributes() - 1); // Assuming z (z on lastindex) as classindex
for (Double[] a: myValues) {
dataRaw.add(new DenseInstance(1.0, a));
}
// Then train or build the algorithm/model on instances (dataRaw) created above.
MultilayerPerceptron mlp = new MultilayerPerceptron(); // Sample algorithm, go through about neural networks to use this or replace with appropriate algorithm.
mlp.buildClassifier(dataRaw);
// Create a test instance,I think you can create testinstance without
// classindex value but cross check in weka as I forgot about it.
double[] values = new double[]{-818.84, 9186.82, 2436.73}; // sample values
DenseInstance testInstance = new DenseInstance(1.0, values);
testInstance.setDataset(dataRaw); // To associate with instances object
// now you can clasify
double classify = mlp.classifyInstance(testInstance);
For more information :- How to use weka programmatically