IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / examples / src / main / java / org / apache / ignite / examples / ml / regression / linear / LinearRegressionLSQRTrainerExample.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.ignite.examples.ml.regression.linear;
19
20 import java.util.Arrays;
21 import java.util.UUID;
22 import javax.cache.Cache;
23 import org.apache.ignite.Ignite;
24 import org.apache.ignite.IgniteCache;
25 import org.apache.ignite.Ignition;
26 import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
27 import org.apache.ignite.cache.query.QueryCursor;
28 import org.apache.ignite.cache.query.ScanQuery;
29 import org.apache.ignite.configuration.CacheConfiguration;
30 import org.apache.ignite.ml.math.VectorUtils;
31 import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
32 import org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer;
33 import org.apache.ignite.ml.regressions.linear.LinearRegressionModel;
34 import org.apache.ignite.thread.IgniteThread;
35
36 /**
37 * Run linear regression model over cached dataset.
38 *
39 * @see LinearRegressionLSQRTrainer
40 */
41 public class LinearRegressionLSQRTrainerExample {
42 /** */
43 private static final double[][] data = {
44 {8, 78, 284, 9.100000381, 109},
45 {9.300000191, 68, 433, 8.699999809, 144},
46 {7.5, 70, 739, 7.199999809, 113},
47 {8.899999619, 96, 1792, 8.899999619, 97},
48 {10.19999981, 74, 477, 8.300000191, 206},
49 {8.300000191, 111, 362, 10.89999962, 124},
50 {8.800000191, 77, 671, 10, 152},
51 {8.800000191, 168, 636, 9.100000381, 162},
52 {10.69999981, 82, 329, 8.699999809, 150},
53 {11.69999981, 89, 634, 7.599999905, 134},
54 {8.5, 149, 631, 10.80000019, 292},
55 {8.300000191, 60, 257, 9.5, 108},
56 {8.199999809, 96, 284, 8.800000191, 111},
57 {7.900000095, 83, 603, 9.5, 182},
58 {10.30000019, 130, 686, 8.699999809, 129},
59 {7.400000095, 145, 345, 11.19999981, 158},
60 {9.600000381, 112, 1357, 9.699999809, 186},
61 {9.300000191, 131, 544, 9.600000381, 177},
62 {10.60000038, 80, 205, 9.100000381, 127},
63 {9.699999809, 130, 1264, 9.199999809, 179},
64 {11.60000038, 140, 688, 8.300000191, 80},
65 {8.100000381, 154, 354, 8.399999619, 103},
66 {9.800000191, 118, 1632, 9.399999619, 101},
67 {7.400000095, 94, 348, 9.800000191, 117},
68 {9.399999619, 119, 370, 10.39999962, 88},
69 {11.19999981, 153, 648, 9.899999619, 78},
70 {9.100000381, 116, 366, 9.199999809, 102},
71 {10.5, 97, 540, 10.30000019, 95},
72 {11.89999962, 176, 680, 8.899999619, 80},
73 {8.399999619, 75, 345, 9.600000381, 92},
74 {5, 134, 525, 10.30000019, 126},
75 {9.800000191, 161, 870, 10.39999962, 108},
76 {9.800000191, 111, 669, 9.699999809, 77},
77 {10.80000019, 114, 452, 9.600000381, 60},
78 {10.10000038, 142, 430, 10.69999981, 71},
79 {10.89999962, 238, 822, 10.30000019, 86},
80 {9.199999809, 78, 190, 10.69999981, 93},
81 {8.300000191, 196, 867, 9.600000381, 106},
82 {7.300000191, 125, 969, 10.5, 162},
83 {9.399999619, 82, 499, 7.699999809, 95},
84 {9.399999619, 125, 925, 10.19999981, 91},
85 {9.800000191, 129, 353, 9.899999619, 52},
86 {3.599999905, 84, 288, 8.399999619, 110},
87 {8.399999619, 183, 718, 10.39999962, 69},
88 {10.80000019, 119, 540, 9.199999809, 57},
89 {10.10000038, 180, 668, 13, 106},
90 {9, 82, 347, 8.800000191, 40},
91 {10, 71, 345, 9.199999809, 50},
92 {11.30000019, 118, 463, 7.800000191, 35},
93 {11.30000019, 121, 728, 8.199999809, 86},
94 {12.80000019, 68, 383, 7.400000095, 57},
95 {10, 112, 316, 10.39999962, 57},
96 {6.699999809, 109, 388, 8.899999619, 94}
97 };
98
99 /** Run example. */
100 public static void main(String[] args) throws InterruptedException {
101 System.out.println();
102 System.out.println(">>> Linear regression model over cache based dataset usage example started.");
103 // Start ignite grid.
104 try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
105 System.out.println(">>> Ignite grid started.");
106
107 IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(),
108 LinearRegressionLSQRTrainerExample.class.getSimpleName(), () -> {
109 IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
110
111 System.out.println(">>> Create new linear regression trainer object.");
112 LinearRegressionLSQRTrainer trainer = new LinearRegressionLSQRTrainer();
113
114 System.out.println(">>> Perform the training to get the model.");
115 LinearRegressionModel mdl = trainer.fit(
116 ignite,
117 dataCache,
118 (k, v) -> VectorUtils.of(Arrays.copyOfRange(v, 1, v.length)),
119 (k, v) -> v[0]
120 );
121
122 System.out.println(">>> Linear regression model: " + mdl);
123
124 System.out.println(">>> ---------------------------------");
125 System.out.println(">>> | Prediction\t| Ground Truth\t|");
126 System.out.println(">>> ---------------------------------");
127
128 try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
129 for (Cache.Entry<Integer, double[]> observation : observations) {
130 double[] val = observation.getValue();
131 double[] inputs = Arrays.copyOfRange(val, 1, val.length);
132 double groundTruth = val[0];
133
134 double prediction = mdl.apply(new DenseLocalOnHeapVector(inputs));
135
136 System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
137 }
138 }
139
140 System.out.println(">>> ---------------------------------");
141 });
142
143 igniteThread.start();
144
145 igniteThread.join();
146 }
147 }
148
149 /**
150 * Fills cache with data and returns it.
151 *
152 * @param ignite Ignite instance.
153 * @return Filled Ignite Cache.
154 */
155 private static IgniteCache<Integer, double[]> getTestCache(Ignite ignite) {
156 CacheConfiguration<Integer, double[]> cacheConfiguration = new CacheConfiguration<>();
157 cacheConfiguration.setName("TEST_" + UUID.randomUUID());
158 cacheConfiguration.setAffinity(new RendezvousAffinityFunction(false, 10));
159
160 IgniteCache<Integer, double[]> cache = ignite.createCache(cacheConfiguration);
161
162 for (int i = 0; i < data.length; i++)
163 cache.put(i, data[i]);
164
165 return cache;
166 }
167 }