더북(TheBook)

데이터 세트 만들기

예제 데이터 세트는 여러 종류의 운송 수단을 분류한 데이터다. 우선 자바 프로그램을 만들어 랜덤하지만 가중치가 있는 데이터를 만들 것이다. 데이터의 운송 수단에는 네 가지 종류, 즉 Bike, Car, Bus, Truck이 있다.

자바 프로그램 코드는 다음과 같다.


importjava.io.BufferedWriter;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.Random;
 
public class MLPData {
 
    private String[] classtype = new String[] { “Bike”, “Car”, “Bus”, “Truck” };
 
    public MLPData() {
 
        Random rand = new Random(System.nanoTime());
 
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(
                    “vehicledata.csv”));
    out.write(“wheels,chassis,pax,vtype\n”);
            for (int i = 0; i< 100; i++) {
                StringBuildersb = new StringBuilder();
                switch (rand.nextInt(3)) {
                case 0:
                    sb.append((rand.nextInt(1) + 1) + ”,”); // 바퀴의 개수
                    sb.append((rand.nextInt(1) + 1) + ”,”); // 섀시의 길이
                    sb.append((rand.nextInt(1) + 1) + ”,”); // 승차 인원수
                    sb.append(classtype[0] + “\n”);
                    break;
                case 1:
                    sb.append((rand.nextInt(2) + 4) + ”,”); // 바퀴의 개수
                    sb.append((rand.nextInt(4) + 1) + ”,”); // 섀시의 길이
                    sb.append((rand.nextInt(4) + 1) + ”,”); // 승차 인원수
                    sb.append(classtype[1] + “\n”);
                    break;
                case 2:
                    sb.append((rand.nextInt(6) + 4) + ”,”);   // 바퀴의 개수
                    sb.append((rand.nextInt(12) + 12) + ”,”); // 섀시의 길이
                    sb.append((rand.nextInt(30) + 10) + ”,”); // 승차 인원수
 
                    sb.append(classtype[2] + “\n”);
                    break;
                case 3:
                    sb.append(“18,”); // 바퀴의 개수
                    sb.append((rand.nextInt(10) + 20) + ”,”); // 섀시의 길이
                    sb.append((rand.nextInt(2) + 1) + ”,”);   // 승차 인원수
                    sb.append(classtype[3] + “\n”);
                    break;
                default:
                    break;
                }
                out.write(sb.toString());
 
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        MLPDatamlp = new MLPData();
    }
}

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.