Jubatus: Classify Example

1.create a mvn project with pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>fifo</groupId>
	<artifactId>jubatusTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<java.source.level>1.7</java.source.level>
		<java.target.level>1.7</java.target.level>
		<maven.compiler.target>1.7</maven.compiler.target>
		<maven.compiler.source>1.7</maven.compiler.source>

		<slf4j.version>1.6.2</slf4j.version>
		<log4j.version>1.2.17</log4j.version>
	</properties>

	<repositories>
		<repository>
			<id>jubat.us</id>
			<name>Jubatus Repository for Maven</name>
			<url>http://download.jubat.us/maven</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>us.jubat</groupId>
			<artifactId>jubatus</artifactId>
			<version>0.7.1</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-shade-plugin</artifactId>
					<version>2.3</version>
					<executions>
						<execution>
							<phase>package</phase>
							<goals>
								<goal>shade</goal>
							</goals>
						</execution>
					</executions>
				</plugin>

			</plugins>
		</pluginManagement>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

 

2. create a class

package jubatusTest;

import java.util.Arrays;
import java.util.List;

import us.jubat.classifier.ClassifierClient;
import us.jubat.classifier.EstimateResult;
import us.jubat.classifier.LabeledDatum;
import us.jubat.common.Datum;

public class GenderMain {

    private static Datum makeDatum(String hair, String top, String bottom, double height) {
	return new Datum().addString("hair", hair).addString("top", top).addString("bottom", bottom)
		.addNumber("height", height);
    }

    private static LabeledDatum makeTrainDatum(String label, String hair, String top, String bottom, double height) {
	return new LabeledDatum(label, makeDatum(hair, top, bottom, height));
    }

    public static void main(String[] args) throws Exception {
	String host = "127.0.0.1";
	int port = 9199;
	String name = "test";

	ClassifierClient client = new ClassifierClient(host, port, name, 1);

	LabeledDatum[] trainData = { //
	makeTrainDatum("male", "short", "sweater", "jeans", 1.70),
		makeTrainDatum("female", "long", "shirt", "skirt", 1.56),
		makeTrainDatum("male", "short", "jacket", "chino", 1.65),
		makeTrainDatum("female", "short", "T shirt", "jeans", 1.72),
		makeTrainDatum("male", "long", "T shirt", "jeans", 1.82),
		makeTrainDatum("female", "long", "jacket", "skirt", 1.43), };

	client.train(Arrays.asList(trainData));

	Datum[] testData = { //
	makeDatum("short", "T shirt", "jeans", 1.81), makeDatum("long", "shirt", "skirt", 1.50), };

	List<List<EstimateResult>> results = client.classify(Arrays.asList(testData));

	for (List<EstimateResult> result : results) {
	    for (EstimateResult r : result) {
		System.out.printf("%s %f\n", r.label, r.score);
	    }
	    System.out.println();
	}

	System.exit(0);
    }
}

 

and configure file gender.json

{
  "method": "AROW",
  "converter": {
    "num_filter_types": {},
    "num_filter_rules": [],
    "string_filter_types": {},
    "string_filter_rules": [],
    "num_types": {},
    "num_rules": [],
    "string_types": {
      "unigram": { "method": "ngram", "char_num": "1" }
    },
    "string_rules": [
      { "key": "*", "type": "unigram", "sample_weight": "bin", "global_weight": "bin" }
    ]
  },
  "parameter": {
    "regularization_weight" : 1.0
  }
}

 

 

3.start Jubatus standlone server

#source /opt/jubatus/profile
#jubaclassifier -f /path/to/gender.json

 

4.run GenderMain  in eclipse

the result is

female -0.081500
male 0.474875

female 0.491617
male -0.336581

 

 

 

 

References

http://jubat.us/en/tutorial/classifier.html

 http://jubat.us/en/tutorial/classifier_java.html

你可能感兴趣的:(example)