検索

369.5. サンプル

download PDF

369.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 {

        // 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");
    }

369.5.2. モデルの構築

モデルを構築するときは、まず使用する分類アルゴリズムを選択してから、いくつかのデータでトレーニングします。結果は、後で目に見えないデータを分類するために使用できるトレーニング済みのモデルです。

ここでは、10 フォールドクロスバリデーションを使用して J48 をトレーニングします。

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();
}

369.5.3. クラスの予測

ここでは、Processor を使用して、エンドポイント URI から直接利用できない機能にアクセスします。

直接ここに来て、この構文が少し圧倒的に見える場合、Nessus API Concepts のセクションを簡単に見ておくとよいでしょう。

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();
}
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.