Java Code Examples for org.apache.ignite.Ignition#start()
The following examples show how to use
org.apache.ignite.Ignition#start() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComputeContinuousMapperExample.java From ignite with Apache License 2.0 | 5 votes |
/** * Executes example. * * @param args Command line arguments, none required. * @throws IgniteException If example execution failed. */ public static void main(String[] args) throws IgniteException { System.out.println(); System.out.println(">>> Compute continuous mapper example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { int phraseLen = ignite.compute().execute(ContinuousMapperTask.class, "Hello Continuous Mapper"); System.out.println(); System.out.println(">>> Total number of characters in the phrase is '" + phraseLen + "'."); } }
Example 2
Source File: ClientConfigurationTest.java From ignite with Apache License 2.0 | 5 votes |
/** * Test check the case when {@link IgniteConfiguration#getRebalanceThreadPoolSize()} is equal to {@link * IgniteConfiguration#getSystemThreadPoolSize()} */ @Test public void testRebalanceThreadPoolSize() { GridStringLogger gridStrLog = new GridStringLogger(); gridStrLog.logLength(1024 * 100); IgniteConfiguration cci = Config.getServerConfiguration().setClientMode(true); cci.setRebalanceThreadPoolSize(cci.getSystemThreadPoolSize()); cci.setGridLogger(gridStrLog); try ( Ignite si = Ignition.start(Config.getServerConfiguration()); Ignite ci = Ignition.start(cci)) { Set<ClusterNode> collect = si.cluster().nodes().stream() .filter(new Predicate<ClusterNode>() { @Override public boolean test(ClusterNode clusterNode) { return clusterNode.isClient(); } }) .collect(Collectors.toSet()); String log = gridStrLog.toString(); boolean containsMsg = log.contains("Setting the rebalance pool size has no effect on the client mode"); Assert.assertTrue(containsMsg); Assert.assertEquals(1, collect.size()); } }
Example 3
Source File: IgniteAtomicLongExample.java From ignite with Apache License 2.0 | 5 votes |
/** * Executes example. * * @param args Command line arguments, none required. * @throws Exception If example execution failed. */ public static void main(String[] args) throws Exception { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(); System.out.println(">>> Atomic long example started."); // Make name for atomic long (by which it will be known in the cluster). String atomicName = UUID.randomUUID().toString(); // Initialize atomic long. final IgniteAtomicLong atomicLong = ignite.atomicLong(atomicName, 0, true); System.out.println(); System.out.println("Atomic long initial value : " + atomicLong.get() + '.'); // Try increment atomic long from all nodes. // Note that this node is also part of the ignite cluster. ignite.compute().broadcast(new IgniteCallable<Object>() { @Override public Object call() { for (int i = 0; i < RETRIES; i++) System.out.println("AtomicLong value has been incremented: " + atomicLong.incrementAndGet()); return null; } }); System.out.println(); System.out.println("Atomic long value after successful CAS: " + atomicLong.get()); } }
Example 4
Source File: IgniteAtomicSequenceExample.java From ignite with Apache License 2.0 | 5 votes |
/** * Executes example. * * @param args Command line arguments, none required. * @throws Exception If example execution failed. */ public static void main(String[] args) throws Exception { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(); System.out.println(">>> Cache atomic sequence example started."); // Try increment atomic sequence on all cluster nodes. Note that this node is also part of the cluster. ignite.compute().broadcast(new SequenceClosure("example-sequence")); System.out.println(); System.out.println("Finished atomic sequence example..."); System.out.println("Check all nodes for output (this node is also part of the cluster)."); System.out.println(); } }
Example 5
Source File: TestGetIgniteCache.java From nifi with Apache License 2.0 | 4 votes |
@BeforeClass public static void setUpClass() { if (preJava11) { ignite = Ignition.start("test-ignite.xml"); } }
Example 6
Source File: CompoundNaiveBayesExample.java From ignite with Apache License 2.0 | 4 votes |
/** Run example. */ public static void main(String[] args) throws IOException { System.out.println(); System.out.println(">>> Compound Naive Bayes classification model over partitioned dataset usage example started."); // Start ignite grid. try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); IgniteCache<Integer, Vector> dataCache = new SandboxMLCache(ignite) .fillCacheWith(MLSandboxDatasets.MIXED_DATASET); double[] priorProbabilities = new double[] {.5, .5}; double[][] thresholds = new double[][] {{.5}, {.5}, {.5}, {.5}, {.5}}; System.out.println(">>> Create new naive Bayes classification trainer object."); CompoundNaiveBayesTrainer trainer = new CompoundNaiveBayesTrainer() .withPriorProbabilities(priorProbabilities) .withGaussianNaiveBayesTrainer(new GaussianNaiveBayesTrainer()) .withGaussianFeatureIdsToSkip(asList(3, 4, 5, 6, 7)) .withDiscreteNaiveBayesTrainer(new DiscreteNaiveBayesTrainer() .setBucketThresholds(thresholds)) .withDiscreteFeatureIdsToSkip(asList(0, 1, 2)); System.out.println(">>> Perform the training to get the model."); Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>() .labeled(Vectorizer.LabelCoordinate.FIRST); CompoundNaiveBayesModel mdl = trainer.fit(ignite, dataCache, vectorizer); System.out.println(">>> Compound Naive Bayes model: " + mdl); double accuracy = Evaluator.evaluate( dataCache, mdl, vectorizer, MetricName.ACCURACY ); System.out.println("\n>>> Accuracy " + accuracy); System.out.println(">>> Compound Naive bayes model over partitioned dataset usage example completed."); } }
Example 7
Source File: KMeansClusterizationExample.java From ignite with Apache License 2.0 | 4 votes |
/** * Run example. */ public static void main(String[] args) throws IOException { System.out.println(); System.out.println(">>> KMeans clustering algorithm over cached dataset usage example started."); // Start ignite grid. try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); IgniteCache<Integer, Vector> dataCache = null; try { dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.TWO_CLASSED_IRIS); Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST); KMeansTrainer trainer = new KMeansTrainer(); KMeansModel mdl = trainer.fit( ignite, dataCache, vectorizer ); System.out.println(">>> KMeans centroids"); Tracer.showAscii(mdl.getCenters()[0]); Tracer.showAscii(mdl.getCenters()[1]); System.out.println(">>>"); System.out.println(">>> --------------------------------------------"); System.out.println(">>> | Predicted cluster\t| Erased class label\t|"); System.out.println(">>> --------------------------------------------"); try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) { for (Cache.Entry<Integer, Vector> observation : observations) { Vector val = observation.getValue(); Vector inputs = val.copyOfRange(1, val.size()); double groundTruth = val.get(0); double prediction = mdl.predict(inputs); System.out.printf(">>> | %.4f\t\t\t| %.4f\t\t|\n", prediction, groundTruth); } System.out.println(">>> ---------------------------------"); System.out.println(">>> KMeans clustering algorithm over cached dataset usage example completed."); } } finally { if (dataCache != null) dataCache.destroy(); } } finally { System.out.flush(); } }
Example 8
Source File: FraudDetectionExample.java From ignite with Apache License 2.0 | 4 votes |
/** Run example. */ public static void main(String[] args) throws IOException { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); IgniteCache<Integer, Vector> dataCache = null; try { System.out.println(">>> Fill dataset cache."); dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.FRAUD_DETECTION); // This vectorizer works with values in cache of Vector class. Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>() .labeled(Vectorizer.LabelCoordinate.LAST); // LAST means "label are stored at last coordinate of vector" // Splits dataset to train and test samples with 80/20 proportion. TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.8); System.out.println(">>> Perform logistic regression."); trainAndEstimateModel(ignite, dataCache, new LogisticRegressionSGDTrainer() .withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0)), vectorizer, split ); System.out.println("\n\n>>> Perform decision tree classifier."); trainAndEstimateModel(ignite, dataCache, new DecisionTreeClassificationTrainer() .withMaxDeep(10.) .withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0)), vectorizer, split ); } finally { if (dataCache != null) dataCache.destroy(); } } finally { System.out.flush(); } }
Example 9
Source File: DecisionTreeClassificationTrainerExample.java From ignite with Apache License 2.0 | 4 votes |
/** * Executes example. * * @param args Command line arguments, none required. */ public static void main(String... args) { System.out.println(">>> Decision tree classification trainer example started."); // Start ignite grid. try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); // Create cache with training data. CacheConfiguration<Integer, LabeledVector<Double>> trainingSetCfg = new CacheConfiguration<>(); trainingSetCfg.setName("TRAINING_SET"); trainingSetCfg.setAffinity(new RendezvousAffinityFunction(false, 10)); IgniteCache<Integer, LabeledVector<Double>> trainingSet = null; try { trainingSet = ignite.createCache(trainingSetCfg); Random rnd = new Random(0); // Fill training data. for (int i = 0; i < 1000; i++) trainingSet.put(i, generatePoint(rnd)); // Create classification trainer. DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(4, 0); // Train decision tree model. LabeledDummyVectorizer<Integer, Double> vectorizer = new LabeledDummyVectorizer<>(); DecisionTreeNode mdl = trainer.fit( ignite, trainingSet, vectorizer ); System.out.println(">>> Decision tree classification model: " + mdl); // Calculate score. int correctPredictions = 0; for (int i = 0; i < 1000; i++) { LabeledVector<Double> pnt = generatePoint(rnd); double prediction = mdl.predict(pnt.features()); double lbl = pnt.label(); if (i % 50 == 1) System.out.printf(">>> test #: %d\t\t predicted: %.4f\t\tlabel: %.4f\n", i, prediction, lbl); if (Precision.equals(prediction, lbl, Precision.EPSILON)) correctPredictions++; } System.out.println(">>> Accuracy: " + correctPredictions / 10.0 + "%"); System.out.println(">>> Decision tree classification trainer example completed."); } finally { trainingSet.destroy(); } } finally { System.out.flush(); } }
Example 10
Source File: CacheAsyncApiExample.java From ignite with Apache License 2.0 | 4 votes |
/** * Executes example. * * @param args Command line arguments, none required. * @throws IgniteException If example execution failed. */ public static void main(String[] args) throws IgniteException { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(); System.out.println(">>> Cache asynchronous API example started."); CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>(); cfg.setCacheMode(CacheMode.PARTITIONED); cfg.setName(CACHE_NAME); // Auto-close cache at the end of the example. try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cfg)) { // Enable asynchronous mode. IgniteCache<Integer, String> asyncCache = cache.withAsync(); Collection<IgniteFuture<?>> futs = new ArrayList<>(); // Execute several puts asynchronously. for (int i = 0; i < 10; i++) { asyncCache.put(i, String.valueOf(i)); futs.add(asyncCache.future()); } // Wait for completion of all futures. futs.forEach(IgniteFuture::get); // Execute get operation asynchronously. asyncCache.get(1); // Asynchronously wait for result. asyncCache.<String>future().listen(fut -> System.out.println("Get operation completed [value=" + fut.get() + ']')); } finally { // Distributed cache could be removed from cluster only by #destroyCache() call. ignite.destroyCache(CACHE_NAME); } } }
Example 11
Source File: GBTRegressionFromSparkExample.java From ignite with Apache License 2.0 | 4 votes |
/** * Run example. */ public static void main(String[] args) throws FileNotFoundException { System.out.println(); System.out.println(">>> GBT Regression model loaded from Spark through serialization over partitioned dataset usage example started."); // Start ignite grid. try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); IgniteCache<Integer, Vector> dataCache = null; try { dataCache = TitanicUtils.readPassengersWithoutNulls(ignite); final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 1, 5, 6).labeled(4); ModelsComposition mdl = (ModelsComposition)SparkModelParser.parse( SPARK_MDL_PATH, SupportedSparkModels.GRADIENT_BOOSTED_TREES_REGRESSION, env ); System.out.println(">>> GBT Regression model: " + mdl); System.out.println(">>> ---------------------------------"); System.out.println(">>> | Prediction\t| Ground Truth\t|"); System.out.println(">>> ---------------------------------"); try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) { for (Cache.Entry<Integer, Vector> observation : observations) { LabeledVector<Double> lv = vectorizer.apply(observation.getKey(), observation.getValue()); Vector inputs = lv.features(); double groundTruth = lv.label(); double prediction = mdl.predict(inputs); System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth); } } System.out.println(">>> ---------------------------------"); } finally { dataCache.destroy(); } } }
Example 12
Source File: Step_1_Read_and_Learn.java From ignite with Apache License 2.0 | 4 votes |
/** * Run example. */ public static void main(String[] args) { System.out.println(); System.out.println(">>> Tutorial step 1 (read and learn) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { try { IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengersWithoutNulls(ignite); final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 5, 6).labeled(1); DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); DecisionTreeNode mdl = trainer.fit( ignite, dataCache, vectorizer ); System.out.println("\n>>> Trained model: " + mdl); double accuracy = Evaluator.evaluate( dataCache, mdl, vectorizer, new Accuracy<>() ); System.out.println("\n>>> Accuracy " + accuracy); System.out.println("\n>>> Test Error " + (1 - accuracy)); System.out.println(">>> Tutorial step 1 (read and learn) example completed."); } catch (FileNotFoundException e) { e.printStackTrace(); } } finally { System.out.flush(); } }
Example 13
Source File: Step_2_Imputing.java From ignite with Apache License 2.0 | 4 votes |
/** * Run example. */ public static void main(String[] args) { System.out.println(); System.out.println(">>> Tutorial step 2 (imputing) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { try { IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite); final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 5, 6).labeled(1); Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>() .fit(ignite, dataCache, vectorizer // "pclass", "sibsp", "parch" ); DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); // Train decision tree model. DecisionTreeNode mdl = trainer.fit( ignite, dataCache, vectorizer ); System.out.println("\n>>> Trained model: " + mdl); double accuracy = Evaluator.evaluate( dataCache, mdl, imputingPreprocessor, new Accuracy<>() ); System.out.println("\n>>> Accuracy " + accuracy); System.out.println("\n>>> Test Error " + (1 - accuracy)); System.out.println(">>> Tutorial step 2 (imputing) example completed."); } catch (FileNotFoundException e) { e.printStackTrace(); } } finally { System.out.flush(); } }
Example 14
Source File: IgniteAbstractOsgiContextActivator.java From ignite with Apache License 2.0 | 4 votes |
/** * Method invoked by OSGi to start the bundle. It starts the specified Ignite configuration. * * @param ctx Bundle context. * @throws Exception */ @Override public final void start(BundleContext ctx) throws Exception { // Ensure that no other instances are running. if (IgniteOsgiUtils.gridCount() > 0) { throw new IgniteException("Failed to start Ignite instance (another instance is already running " + "and ignite-osgi is currently limited to a single instance per container)."); } bundleCtx = ctx; // Start the Ignite configuration specified by the user. IgniteConfiguration cfg = igniteConfiguration(); A.notNull(cfg, "Ignite configuration"); // Override the classloader with the classloading strategy chosen by the user. ClassLoader clsLdr; if (classLoadingStrategy() == OsgiClassLoadingStrategyType.BUNDLE_DELEGATING) clsLdr = new BundleDelegatingClassLoader(bundleCtx.getBundle(), Ignite.class.getClassLoader()); else clsLdr = new ContainerSweepClassLoader(bundleCtx.getBundle(), Ignite.class.getClassLoader()); cfg.setClassLoader(clsLdr); onBeforeStart(ctx); // Start Ignite. try { ignite = Ignition.start(cfg); } catch (Throwable t) { U.error(log, "Failed to start Ignite via OSGi Activator [errMsg=" + t.getMessage() + ']', t); onAfterStart(ctx, t); return; } log = ignite.log(); if (log.isInfoEnabled()) log.info("Started Ignite from OSGi Activator [name=" + ignite.name() + ']'); // Add into Ignite's OSGi registry. IgniteOsgiUtils.classloaders().put(ignite, clsLdr); // Export Ignite as a service. exportOsgiService(ignite); onAfterStart(ctx, null); }
Example 15
Source File: IgniteCacheExample.java From tutorials with MIT License | 4 votes |
private static void customInitialization() { IgniteConfiguration configuration = new IgniteConfiguration(); configuration.setLifecycleBeans(new CustomLifecycleBean()); Ignite ignite = Ignition.start(configuration); }
Example 16
Source File: SpringApplicationConfiguration.java From ignite with Apache License 2.0 | 4 votes |
/** * Creating Apache Ignite instance bean. A bean will be passed to {@link IgniteRepositoryFactoryBean} to initialize * all Ignite based Spring Data repositories and connect to a cluster. */ @Bean public Ignite igniteInstance() { return Ignition.start("examples/config/spring/example-spring-data.xml"); }
Example 17
Source File: DecisionTreeClassificationTrainerSQLTableExample.java From ignite with Apache License 2.0 | 4 votes |
/** * Run example. */ public static void main(String[] args) throws IgniteCheckedException, IOException { System.out.println(">>> Decision tree classification trainer example started."); // Start ignite grid. try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); // Dummy cache is required to perform SQL queries. CacheConfiguration<?, ?> cacheCfg = new CacheConfiguration<>(DUMMY_CACHE_NAME) .setSqlSchema("PUBLIC"); IgniteCache<?, ?> cache = null; try { cache = ignite.getOrCreateCache(cacheCfg); System.out.println(">>> Creating table with training data..."); cache.query(new SqlFieldsQuery("create table titanic_train (\n" + " passengerid int primary key,\n" + " pclass int,\n" + " survived int,\n" + " name varchar(255),\n" + " sex varchar(255),\n" + " age float,\n" + " sibsp int,\n" + " parch int,\n" + " ticket varchar(255),\n" + " fare float,\n" + " cabin varchar(255),\n" + " embarked varchar(255)\n" + ") with \"template=partitioned\";")).getAll(); System.out.println(">>> Creating table with test data..."); cache.query(new SqlFieldsQuery("create table titanic_test (\n" + " passengerid int primary key,\n" + " pclass int,\n" + " survived int,\n" + " name varchar(255),\n" + " sex varchar(255),\n" + " age float,\n" + " sibsp int,\n" + " parch int,\n" + " ticket varchar(255),\n" + " fare float,\n" + " cabin varchar(255),\n" + " embarked varchar(255)\n" + ") with \"template=partitioned\";")).getAll(); loadTitanicDatasets(ignite, cache); System.out.println(">>> Prepare trainer..."); DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(4, 0); System.out.println(">>> Perform training..."); DecisionTreeNode mdl = trainer.fit( new SqlDatasetBuilder(ignite, "SQL_PUBLIC_TITANIC_TRAIN"), new BinaryObjectVectorizer<>("pclass", "age", "sibsp", "parch", "fare") .withFeature("sex", BinaryObjectVectorizer.Mapping.create().map("male", 1.0).defaultValue(0.0)) .labeled("survived") ); System.out.println("Tree is here: " + mdl.toString(true)); System.out.println(">>> Perform inference..."); try (QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("select " + "pclass, " + "sex, " + "age, " + "sibsp, " + "parch, " + "fare from titanic_test"))) { for (List<?> passenger : cursor) { Vector input = VectorUtils.of(new Double[]{ asDouble(passenger.get(0)), "male".equals(passenger.get(1)) ? 1.0 : 0.0, asDouble(passenger.get(2)), asDouble(passenger.get(3)), asDouble(passenger.get(4)), asDouble(passenger.get(5)), }); double prediction = mdl.predict(input); System.out.printf("Passenger %s will %s.\n", passenger, prediction == 0 ? "die" : "survive"); } } System.out.println(">>> Example completed."); } finally { cache.query(new SqlFieldsQuery("DROP TABLE titanic_train")); cache.query(new SqlFieldsQuery("DROP TABLE titanic_test")); cache.destroy(); } } finally { System.out.flush(); } }
Example 18
Source File: TcpDiscoveryVmIpFinderSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** * */ @Test public void testUnregistration() throws Exception { Ignition.start(config("server1", false, false)); int srvSize = sharedStaticIpFinder.getRegisteredAddresses().size(); Ignition.start(config("server2", false, false)); Ignition.start(config("client1", true, false)); assertEquals(2 * srvSize, sharedStaticIpFinder.getRegisteredAddresses().size()); Ignition.start(config("client2", true, false)); Ignition.start(config("client3", true, false)); assertEquals(2 * srvSize, sharedStaticIpFinder.getRegisteredAddresses().size()); Ignition.start(config("client4", true, true)); assertEquals(3 * srvSize, sharedStaticIpFinder.getRegisteredAddresses().size()); Ignition.stop("client1", true); Ignition.stop("client2", true); Ignition.stop("client3", true); assertEquals(3 * srvSize, sharedStaticIpFinder.getRegisteredAddresses().size()); Ignition.stop("client4", true); GridTestUtils.waitForCondition(new GridAbsPredicate() { @Override public boolean apply() { return 2 == G.allGrids().size(); } }, 10000); Ignition.stop("server1", true); Ignition.stop("server2", true); boolean res = GridTestUtils.waitForCondition(new GridAbsPredicate() { @Override public boolean apply() { return G.allGrids().isEmpty(); } }, 10000); assertTrue(res); assertTrue(3 * srvSize >= sharedStaticIpFinder.getRegisteredAddresses().size()); }
Example 19
Source File: Step_4_Add_age_fare.java From ignite with Apache License 2.0 | 4 votes |
/** * Run example. */ public static void main(String[] args) { System.out.println(); System.out.println(">>> Tutorial step 4 (add age and fare) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { try { IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite); // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare". final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 3, 4, 5, 6, 8, 10).labeled(1); Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>() .withEncoderType(EncoderType.STRING_ENCODER) .withEncodedFeature(1) .withEncodedFeature(6) // <--- Changed index here. .fit(ignite, dataCache, vectorizer ); Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>() .fit(ignite, dataCache, strEncoderPreprocessor ); DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); // Train decision tree model. DecisionTreeNode mdl = trainer.fit( ignite, dataCache, imputingPreprocessor ); System.out.println("\n>>> Trained model: " + mdl); double accuracy = Evaluator.evaluate( dataCache, mdl, imputingPreprocessor, new Accuracy<>() ); System.out.println("\n>>> Accuracy " + accuracy); System.out.println("\n>>> Test Error " + (1 - accuracy)); System.out.println(">>> Tutorial step 4 (add age and fare) example completed."); } catch (FileNotFoundException e) { e.printStackTrace(); } } finally { System.out.flush(); } }
Example 20
Source File: GmmClusterizationExample.java From ignite with Apache License 2.0 | 4 votes |
/** * Runs example. * * @param args Command line arguments. */ public static void main(String[] args) { System.out.println(); System.out.println(">>> GMM clustering algorithm over cached dataset usage example started."); // Start ignite grid. try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); long seed = 0; IgniteCache<Integer, LabeledVector<Double>> dataCache = null; try { dataCache = ignite.createCache( new CacheConfiguration<Integer, LabeledVector<Double>>("GMM_EXAMPLE_CACHE") .setAffinity(new RendezvousAffinityFunction(false, 10)) ); // Dataset consists of three gaussians where two from them are rotated onto PI/4. DataStreamGenerator dataStream = new VectorGeneratorsFamily.Builder().add( RandomProducer.vectorize( new GaussRandomProducer(0, 2., seed++), new GaussRandomProducer(0, 3., seed++) ).rotate(Math.PI / 4).move(VectorUtils.of(10., 10.))).add( RandomProducer.vectorize( new GaussRandomProducer(0, 1., seed++), new GaussRandomProducer(0, 2., seed++) ).rotate(-Math.PI / 4).move(VectorUtils.of(-10., 10.))).add( RandomProducer.vectorize( new GaussRandomProducer(0, 3., seed++), new GaussRandomProducer(0, 3., seed++) ).move(VectorUtils.of(0., -10.)) ).build(seed++).asDataStream(); AtomicInteger keyGen = new AtomicInteger(); dataStream.fillCacheWithCustomKey(50000, dataCache, v -> keyGen.getAndIncrement()); GmmTrainer trainer = new GmmTrainer(1); GmmModel mdl = trainer .withMaxCountIterations(10) .withMaxCountOfClusters(4) .withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(seed)) .fit(ignite, dataCache, new LabeledDummyVectorizer<>()); System.out.println(">>> GMM means and covariances"); for (int i = 0; i < mdl.countOfComponents(); i++) { MultivariateGaussianDistribution distribution = mdl.distributions().get(i); System.out.println(); System.out.println("============"); System.out.println("Component #" + i); System.out.println("============"); System.out.println("Mean vector = "); Tracer.showAscii(distribution.mean()); System.out.println(); System.out.println("Covariance matrix = "); Tracer.showAscii(distribution.covariance()); } System.out.println(">>>"); } finally { if (dataCache != null) dataCache.destroy(); } } finally { System.out.flush(); } }