368.5. 샘플
368.5.1. 읽기 + 필터 + 쓰기 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
첫 번째 예제는 파일 구성 요소가 있는 CSV 파일을 읽은 다음 Weka에 전달하는 방법을 보여줍니다. Weka에서 몇 가지 필터를 데이터 세트에 적용한 다음 쓰기를 위해 파일 구성 요소에 전달합니다.
@Override public void configure() throws Exception { // Use the file component to read the CSV file from("file:src/test/resources/data?fileName=sfny.csv") // Convert the 'in_sf' attribute to nominal .to("weka:filter?apply=NumericToNominal -R first") // Move the 'in_sf' attribute to the end .to("weka:filter?apply=Reorder -R 2-last,1") // Rename the relation .to("weka:filter?apply=RenameRelation -modify sfny") // Use the file component to write the Arff file .to("file:target/data?fileName=sfny.arff") }
@Override
public void configure() throws Exception {
// Use the file component to read the CSV file
from("file:src/test/resources/data?fileName=sfny.csv")
// Convert the 'in_sf' attribute to nominal
.to("weka:filter?apply=NumericToNominal -R first")
// Move the 'in_sf' attribute to the end
.to("weka:filter?apply=Reorder -R 2-last,1")
// Rename the relation
.to("weka:filter?apply=RenameRelation -modify sfny")
// Use the file component to write the Arff file
.to("file:target/data?fileName=sfny.arff")
}
여기서는 파일 구성 요소를 사용하지 않고 위와 동일한 작업을 수행합니다.
@Override public void configure() throws Exception { // Initiate the route from somewhere .from("...") // Use Weka to read the CSV file .to("weka:read?path=src/test/resources/data/sfny.csv") // Convert the 'in_sf' attribute to nominal .to("weka:filter?apply=NumericToNominal -R first") // Move the 'in_sf' attribute to the end .to("weka:filter?apply=Reorder -R 2-last,1") // Rename the relation .to("weka:filter?apply=RenameRelation -modify sfny") // Use Weka to write the Arff file .to("weka:write?path=target/data/sfny.arff"); }
@Override
public void configure() throws Exception {
// Initiate the route from somewhere
.from("...")
// Use Weka to read the CSV file
.to("weka:read?path=src/test/resources/data/sfny.csv")
// Convert the 'in_sf' attribute to nominal
.to("weka:filter?apply=NumericToNominal -R first")
// Move the 'in_sf' attribute to the end
.to("weka:filter?apply=Reorder -R 2-last,1")
// Rename the relation
.to("weka:filter?apply=RenameRelation -modify sfny")
// Use Weka to write the Arff file
.to("weka:write?path=target/data/sfny.arff");
}
이 예에서 클라이언트는 입력 경로 또는 기타 지원되는 유형을 제공합니다. 지원되는 입력 유형 세트에 대한 WekaTypeConverters
를 확인합니다.
@Override public void configure() throws Exception { // Initiate the route from somewhere .from("...") // Convert the 'in_sf' attribute to nominal .to("weka:filter?apply=NumericToNominal -R first") // Move the 'in_sf' attribute to the end .to("weka:filter?apply=Reorder -R 2-last,1") // Rename the relation .to("weka:filter?apply=RenameRelation -modify sfny") // Use Weka to write the Arff file .to("weka:write?path=target/data/sfny.arff"); }
@Override
public void configure() throws Exception {
// Initiate the route from somewhere
.from("...")
// Convert the 'in_sf' attribute to nominal
.to("weka:filter?apply=NumericToNominal -R first")
// Move the 'in_sf' attribute to the end
.to("weka:filter?apply=Reorder -R 2-last,1")
// Rename the relation
.to("weka:filter?apply=RenameRelation -modify sfny")
// Use Weka to write the Arff file
.to("weka:write?path=target/data/sfny.arff");
}
368.5.2. 모델 빌드 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
모델을 빌드할 때 먼저 사용할 분류 알고리즘을 선택한 다음 일부 데이터로 교육합니다. 그 결과는 나중에 보이지 않는 데이터를 분류하는 데 사용할 수 있는 숙련된 모델입니다.
여기에서는 J48을 10개의 교차 무효화로 교육합니다.
try (CamelContext camelctx = new DefaultCamelContext()) { camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { // Use the file component to read the training data from("file:src/test/resources/data?fileName=sfny-train.arff") // Build a J48 classifier using cross-validation with 10 folds .to("weka:model?build=J48&xval=true&folds=10&seed=1") // Persist the J48 model .to("weka:model?saveTo=src/test/resources/data/sfny-j48.model") } }); camelctx.start(); }
try (CamelContext camelctx = new DefaultCamelContext()) {
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
// Use the file component to read the training data
from("file:src/test/resources/data?fileName=sfny-train.arff")
// Build a J48 classifier using cross-validation with 10 folds
.to("weka:model?build=J48&xval=true&folds=10&seed=1")
// Persist the J48 model
.to("weka:model?saveTo=src/test/resources/data/sfny-j48.model")
}
});
camelctx.start();
}
368.5.3. 클래스 예측 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
여기에서는 Processor
를 사용하여 엔드포인트 URI에서 직접 사용할 수 없는 기능에 액세스합니다.
여기에 직접 와서이 구문이 약간 불안정해 보이는 경우 Nessus API 개념에 대한 섹션을 간략하게 살펴보고자 할 수 있습니다.
try (CamelContext camelctx = new DefaultCamelContext()) { camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { // Use the file component to read the test data from("file:src/test/resources/data?fileName=sfny-test.arff") // Remove the class attribute .to("weka:filter?apply=Remove -R last") // Add the 'prediction' placeholder attribute .to("weka:filter?apply=Add -N predicted -T NOM -L 0,1") // Rename the relation .to("weka:filter?apply=RenameRelation -modify sfny-predicted") // Load an already existing model .to("weka:model?loadFrom=src/test/resources/data/sfny-j48.model") // Use a processor to do the prediction .process(new Processor() { public void process(Exchange exchange) throws Exception { Dataset dataset = exchange.getMessage().getBody(Dataset.class); dataset.applyToInstances(new NominalPredictor()); } }) // Write the data file .to("weka:write?path=src/test/resources/data/sfny-predicted.arff") } }); camelctx.start(); }
try (CamelContext camelctx = new DefaultCamelContext()) {
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
// Use the file component to read the test data
from("file:src/test/resources/data?fileName=sfny-test.arff")
// Remove the class attribute
.to("weka:filter?apply=Remove -R last")
// Add the 'prediction' placeholder attribute
.to("weka:filter?apply=Add -N predicted -T NOM -L 0,1")
// Rename the relation
.to("weka:filter?apply=RenameRelation -modify sfny-predicted")
// Load an already existing model
.to("weka:model?loadFrom=src/test/resources/data/sfny-j48.model")
// Use a processor to do the prediction
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
Dataset dataset = exchange.getMessage().getBody(Dataset.class);
dataset.applyToInstances(new NominalPredictor());
}
})
// Write the data file
.to("weka:write?path=src/test/resources/data/sfny-predicted.arff")
}
});
camelctx.start();
}