将 double[] 数组添加到 weka 实例
Add double[] array to weka instances
[更新]
我是 weka 的新手。我想将我的 double[] array
添加到我的 weka Instances dataRaw
但我不知道该怎么做。
这是我的代码:
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
看起来像这样:
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]
只是基本解释:-
首先,要进行分类,您需要一个模型,而要获得一个模型,您需要在具有属性和类索引的数据上训练算法。
Attributes 是 "Type of data",假设如果您有员工数据,那么 name、desgination、age、salary 等是属性或简单来说是 csv 文件中的列名.
数据类型可以是数字(整数或实数)或标称表示普通字符串。
Classindex 是您希望算法根据训练实例 predict/classify 的 attribute/column 索引。例如,您可以使用年龄和职位来预测薪水。
生成模型后,您可以在该模型上通过发送类似格式的数据进行分类(预测),这意味着使用相同属性和类索引创建的实例。
您需要确定要 运行 的算法以及要预测的 attribute/column 索引。
[注意 :- 有些算法只适用于数字数据,有些算法只适用于标称数据,有些算法适用于两种类型的数据。因此,您应该根据数据类型选择一种算法。在选择算法之前,您还应该检查其他内容,但最基本的是数据类型。]
我建议您在尝试 运行 算法之前先了解一下 machine learning and weka。
您可以尝试的示例代码,我假设您的类索引为 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);
[更新]
我是 weka 的新手。我想将我的 double[] array
添加到我的 weka Instances dataRaw
但我不知道该怎么做。
这是我的代码:
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
看起来像这样:
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]
只是基本解释:- 首先,要进行分类,您需要一个模型,而要获得一个模型,您需要在具有属性和类索引的数据上训练算法。
Attributes 是 "Type of data",假设如果您有员工数据,那么 name、desgination、age、salary 等是属性或简单来说是 csv 文件中的列名.
数据类型可以是数字(整数或实数)或标称表示普通字符串。
Classindex 是您希望算法根据训练实例 predict/classify 的 attribute/column 索引。例如,您可以使用年龄和职位来预测薪水。
生成模型后,您可以在该模型上通过发送类似格式的数据进行分类(预测),这意味着使用相同属性和类索引创建的实例。
您需要确定要 运行 的算法以及要预测的 attribute/column 索引。
[注意 :- 有些算法只适用于数字数据,有些算法只适用于标称数据,有些算法适用于两种类型的数据。因此,您应该根据数据类型选择一种算法。在选择算法之前,您还应该检查其他内容,但最基本的是数据类型。]
我建议您在尝试 运行 算法之前先了解一下 machine learning and weka。
您可以尝试的示例代码,我假设您的类索引为 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);