java代碼實現將時間序列數據集(UCR)轉化爲weka能識別的.arff文件

之前發過一篇轉.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 block
						e1.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();

    }
	

	}
	



好了,就醬紫





























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章