java代码实现将时间序列数据集(UCR)转化为weka能识别的.arff文件

来源:互联网 发布:电脑未识别的网络 编辑:程序博客网 时间:2024/06/05 08:19

之前发过一篇转.arff文件的博客,后来发现是错的。。。,weka虽然能识别,但是并不能进行聚类或者分类,原因是我没有提取数据集中的类别信息,也就是说我将其中的类标也看成一个属性了。。。而且为了方便转换,做了一个再简单不过的界面,(主要是需要转换的文件太多了,不想一个一个改路径。。。)如下图,直接选中就可以转换了:



第一个显示你要选择的文件,第二个显示你是否转换成功。


这里我用的环境是Eclipse+window buider swing,window buider没装的可以百度一下,都有的,是一个图形界面的插件。装好以后选择File  --> New  ->  other  --> window buider  -->  swing desinger -->    application window

建好以后把下面的代码复制全部粘贴进去即可(全选覆盖它自动生成的代码)

步骤:

首先你得将数据集保存为.txt文件,因为初始的UCR数据集我的电脑是不能识别为一个.xxx文件的,需要转为.txt文件,不要用记事本,卡出翔,推荐用Node pade++   用它打开后另存为.txt文件即可,


下面就贴代码了:

package hello;import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.filechooser.FileNameExtensionFilter;import org.eclipse.jface.layout.AbstractColumnLayout;import org.omg.CORBA.PUBLIC_MEMBER;import com.ibm.icu.text.RelativeDateTimeFormatter.AbsoluteUnit;import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFileChooser;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.nio.file.ReadOnlyFileSystemException;import java.util.ArrayList;import java.awt.event.ActionEvent;import javax.swing.JTextField;public class option extends JFrame {private JTextField textField; static ArrayList<String> attributevalues = new ArrayList<>();     static ArrayList<String> attributeclass = new ArrayList<>();/** * Launch the application. */public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {option frame = new option();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public option() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 600, 400);getContentPane().setLayout(null);JButton button = new JButton("\u9009\u62E9\u6587\u4EF6");button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser();             //设置选择器  chooser.setMultiSelectionEnabled(true);             //设为多选int returnVal = chooser.showOpenDialog(button);        //是否打开文件选择框System.out.println("returnVal="+returnVal);if (returnVal == JFileChooser.APPROVE_OPTION) {          //如果符合文件类型String filepath = chooser.getSelectedFile().getAbsolutePath();      //获取绝对路径//System.out.println(filepath);String relativapath = chooser.getSelectedFile().getName();//////System.out.println("You chose to open this file: "+ chooser.getSelectedFile().getName());  //输出相对路径try {fileread_write(filepath,relativapath);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}textField.setText("文件"+relativapath+"转换成功");}}});button.setBounds(201, 70, 93, 23);getContentPane().add(button);textField = new JTextField();textField.setBounds(77, 136, 444, 33);getContentPane().add(textField);textField.setColumns(10);}public static void fileread_write(String filepath,String relativefilepath) throws IOException {        FileReader reader = new FileReader(filepath);        BufferedReader br = new BufferedReader(reader);        StringBuffer sb = new StringBuffer("");        ArrayList<String> attributevalues = new ArrayList<>();        ArrayList<String> attributeclass = new ArrayList<>();        String str = null;        str = br.readLine();        String str2 = str;        String[] attributes = str.split(",");        String[] attributevalue = str.split(",", 0);        attributevalues.add(attributevalue[0]);        while ((str = br.readLine()) != null) {            sb.append(str + "\r\n");            String[] attibutetemp = str.split(",", 0);            attributevalues.add(attibutetemp[0]);        }        br.close();        reader.close();        attributeclass.add(attributevalues.get(0));        //System.out.println(attributeclass.size());        for (int i = 0; i < attributevalues.size(); i++) {            for (int j = 0; j < attributeclass.size(); j++) {                if (j == (attributeclass.size() - 1)) {                    if(attributeclass.get(j).equals(attributevalues.get(i))){                        break;                    }                    else{                        attributeclass.add(attributevalues.get(i));                        // System.out.println(attributeclass.size());                        break;                    }                }                if (attributeclass.get(j).equals(attributevalues.get(i))) {                    break;                }            }        }        //输出到你想保存的路径        FileWriter writer = new FileWriter("E:\\数据挖掘大作业测试数据\\"+relativefilepath.substring(0, relativefilepath.length()-4)+".arff");                BufferedWriter bw = new BufferedWriter(writer);        bw.write("@relation" + " " + "test" + "\r\n");        bw.write("@attribute"+" "+"class"+" "+"{");        for(int i =0;i<attributeclass.size();i++){            if(i==(attributeclass.size()-1)){                bw.write(attributeclass.get(i)+"}"+"\r\n");                break;            }            bw.write(attributeclass.get(i)+",");        }        for (int i = 1; i < attributes.length; i++) {            bw.write("@attribute" + " " + "attribute" + i + " " + "numeric" + "\r\n");        }        bw.write("@data" + "\r\n");        bw.write(str2 + "\r\n");        bw.write(sb.toString());        bw.close();        writer.close();    }}



好了,就酱紫





























0 0