java - 将 double[] 数组添加到 weka 实例

标签 java arrays 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]

最佳答案

只是基本解释:- 首先,要进行分类,您需要一个模型,并且要获得模型,您需要对具有属性和 classIndex 的数据训练算法。

属性是“数据类型”,假设您有员工数据,那么姓名、目的地、年龄、薪水等都是属性,或者简单来说就是 csv 文件中的列名称。

数据类型可以是数字(整数或实数)或标称,即普通字符串。

类索引是您希望算法根据训练实例进行预测/分类的属性/列索引。例如,您可以使用年龄和职务来预测薪资。

生成模型后,在该模型上,您可以通过发送类似格式的数据(即使用相同属性和类索引创建的实例)来进行分类(预测)。

您需要确定要运行哪种算法以及要预测哪个属性/列索引。

[注意:- 有些算法仅适用于数值数据,而其他一些算法仅适用于名义数据,有些算法适用于两种类型的数据。因此,您应该根据数据类型选择算法。在选择算法之前,您还应该检查其他一些内容,但基本的是数据类型。]

我建议你浏览一下machine learningweka在尝试运行算法之前。

您可以尝试的示例代码,我假设您的类索引为 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);

了解更多信息:- How to use weka programmatically

关于java - 将 double[] 数组添加到 weka 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38968732/

相关文章:

Javascript - 如何混合具有特定排序顺序的两个对象数组?

java - 如何在使用 Weka Java API 执行线性回归时处理字符串值

java - Hadoop 使用 KeyValueTextInputFormat

java - isEmpty() 与优先级队列中的 size() == 0 有何不同

arrays - 您如何解决 "The parameters (number[]) don' t 与 SpreadsheetApp.Range.setValues 的方法签名匹配”错误

c - C中的二维字符数组初始化

java - 从 Weka Java API 获取欧几里得距离

java - 如何从 weka API 计算置信度?

java - 从数据流中的 PubsubMessage 获取属性

java - Spring测试不将数据存储在数据库中