{"id":22848395,"url":"https://github.com/feedzai/fos-sample","last_synced_at":"2025-07-03T22:04:36.214Z","repository":{"id":13775891,"uuid":"16470952","full_name":"feedzai/fos-sample","owner":"feedzai","description":"FOS Sample code","archived":false,"fork":false,"pushed_at":"2014-02-03T14:34:17.000Z","size":164,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-03T22:03:50.952Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/feedzai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-02-03T05:45:32.000Z","updated_at":"2015-09-24T16:05:34.000Z","dependencies_parsed_at":"2022-09-23T15:53:10.246Z","dependency_job_id":null,"html_url":"https://github.com/feedzai/fos-sample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/feedzai/fos-sample","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feedzai%2Ffos-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feedzai%2Ffos-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feedzai%2Ffos-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feedzai%2Ffos-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/feedzai","download_url":"https://codeload.github.com/feedzai/fos-sample/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feedzai%2Ffos-sample/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263410758,"owners_count":23462295,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-13T04:11:36.282Z","updated_at":"2025-07-03T22:04:36.161Z","avatar_url":"https://github.com/feedzai.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"FOS Sample code\n\n=========\n\n\nThis project contains FOS sample training and scoring samples.\n\nAll the following samples assume a locally running fos server with the default configuration. Please checkout [fos-core](https://github.com/feedzai/fos-core/blob/master/README.md) readme on how to create and start a fos server bundle\n\n# Training a WEKA classifier\n\nYou can check the full code: [TrainingSample](https://github.com/feedzai/fos-sample/blob/master/src/main/java/com/feedzai/fos/samples/weka/WekaTraining.java).\n\nThe first step is to create a FOS manager. The manager provides an API to manage and train models. \nYou can create a manager by calling the `create` method on the `FOSManagerAdapter`. The method has two parameters:\n\n1. FOS server host. Since we're assuming a locally running server `localhost` is fine.\n2. FOS RMI port\n\n```java\n    FOSManagerAdapter manager = FOSManagerAdapter.create(\"localhost\", 5959);\n```\n\nThe second step is to create a model configuration. This is done by suppling a list of model attributes:\n\n```java\n   List\u003cAttribute\u003e attributes = ImmutableList.of(\n            new NumericAttribute(\"sepalLength\"),\n            new NumericAttribute(\"sepalWidth\"),\n            new NumericAttribute(\"petalLength\"),\n            new NumericAttribute(\"petalWidth\"),\n            new CategoricalAttribute(\"class\",\n                                     ImmutableList.of(\"Iris-setosa\",\n                                                      \"Iris-versicolor\",\n                                                      \"Iris-virginica\")));\n\n```\n\nAnd setting a couple  configuration options:\n\n1. Classifier attributes index -  Property `WekaModelConfig.CLASS_INDEX`\n1. Weka implementation to use - Property `WekaModelConfig.CLASSIFIER_IMPL`\n\n```java\n    Map\u003cString, String\u003e properties = ImmutableMap.of(\n                                          WekaModelConfig.CLASS_INDEX, \"4\",\n                                          WekaModelConfig.CLASSIFIER_IMPL, J48.class.getName());\n```\n\nNow we're ready to create a model configuration:\n\n```java\n    ModelConfig modelConfig = new ModelConfig(attributes, properties);\n```\n\nTrain and create a model. The `trainAndAddFile` receives a `ModelConfig` and a `CSV` file with the training instances.\n\n```java\n        File trainFile = new File(\"iris.data\");\n\n        UUID uuid = manager.trainAndAddFile(modelConfig, trainFile.getAbsolutePath());\n\n        System.out.println(\"Trained model UUID = \" + uuid);\n```\n\nRunning [TrainingSample.java] should display the generated model identifier.\n\n```\nTrained model UUID = f80e7881-e267-4d2b-9262-d58c8adcdae2\n```\n\nKeep note of this model UUID. You'll need it for the next sample.\n\nSo, behind the scenes, FOS trained and persisted a weka model with the specified configuration. If you're curious you may now check the `fos-server/models` folder. It should contain `.model` file with the trained model and a `.header` file with the model configuration:\n\n```json\n{\n   \"storeModel\" : true,\n   \"attributes\" : [\n      {\n         \"name\" : \"sepalLength\",\n         \"@type\" : \".NumericAttribute\"\n      },\n      {\n         \"name\" : \"sepalWidth\",\n         \"@type\" : \".NumericAttribute\"\n      },\n      {\n         \"name\" : \"petalLength\",\n         \"@type\" : \".NumericAttribute\"\n      },\n      {\n         \"name\" : \"petalWidth\",\n         \"@type\" : \".NumericAttribute\"\n      },\n      {\n         \"categoricalInstances\" : [\n            \"Iris-setosa\",\n            \"Iris-versicolor\",\n            \"Iris-virginica\"\n         ],\n         \"unknownReplacement\" : \"__UNKOWN__\",\n         \"name\" : \"class\",\n         \"@type\" : \".CategoricalAttribute\"\n      }\n   ],\n   \"properties\" : {\n      \"classifierimpl\" : \"weka.classifiers.trees.J48\",\n      \"model\" : \"/home/miguel/fos-server/models/f80e7881-e267-4d2b-9262-d58c8adcdae22854550933383138387.model\",\n      \"id\" : \"f80e7881-e267-4d2b-9262-d58c8adcdae2\",\n      \"classIndex\" : \"4\"\n   }\n}\n```\n\n# Scoring with a Weka classifier\n\nThis sample assumes that you've sucessfully trained a model as specified in the previous step.\n\nNow that we have a trained model, we can move on to the [scoring sample](https://github.com/feedzai/fos-sample/blob/master/src/main/java/com/feedzai/fos/samples/weka/WekaScoring.java). \n\nFirst we have to pass the previously trained model UUID: \n\n```java\npublic static void main( String[] args ) throws RemoteException, NotBoundException, FOSException {\n        if (args.length != 1) {\n            System.err.println(\"Please supply the model UUID to score\");\n            return;\n        }\n        UUID modelId = UUID.fromString(args[0]);\n```\n\nThen we obtain a reference to the manager:\n\n```java\n        FOSManagerAdapter manager = FOSManagerAdapter.create(\"localhost\", 5959);\n```\n\nAdd a couple sample scoring instances. These were randomly from the [iris](http://en.wikipedia.org/wiki/Iris_flower_data_set) dataset - one for each flower type:\n\n```java\n        List\u003cObject[]\u003e scorables = Arrays.asList(new Object[][] {\n                {5.8,4.0,1.2,0.2, null}, // 1: Iris-setosa\n                {6.9,3.1,4.9,1.5, null}, // 2: Iris-versicolor\n                {6.0,2.2,5.0,1.5, null}  // 3: Iris-virginica\n        });\n```\n\nThe next step is to obtain a reference to the `Scorer` instance that will performing the scoring.\n\n```java\n        Scorer scorer = manager.getScorer();\n\n```\n\nNow we can score the sample instances and print the predicted score for each instance:\n\n```java\n        System.out.println(\"Probability for \");\n        System.out.println(\"Instance ID |  setosa  |versicolor| virginica\");\n\n        int instanceid = 1;\n        for(double[] score : scorer.score(modelId, scorables)) {\n            System.out.println(String.format(\"Instance %1$d  | %2$f | %3$f | %4$f\",\n                                             instanceid++,\n                                             score[0],\n                                             score[1],\n                                             score[2]));\n        }\n```        \n\nRunning the sample should produce the following output\n\n```\nProbability for \nInstance ID |  setosa  |versicolor| virginica\nInstance 1  | 1.000000 | 0.000000 | 0.000000\nInstance 2  | 0.000000 | 0.979167 | 0.020833\nInstance 3  | 0.000000 | 0.000000 | 1.000000\n```\n\nAs we can see, the algorithm predicted the flower type with a high decree of certainty. After all we're scoring the same instances that were part of the original training :smirk:\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeedzai%2Ffos-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffeedzai%2Ffos-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeedzai%2Ffos-sample/lists"}