Skip to content

Commit 3ab125e

Browse files
authored
KDTree.fromIterable: default value for splitting strategy changed (#225)
1 parent 780b8a6 commit 3ab125e

File tree

7 files changed

+137
-28
lines changed

7 files changed

+137
-28
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 16.11.2
4+
- `KDTree`:
5+
- `fromIterable` constructor, default value for splitting strategy changed
6+
- Serialization tests added
7+
- `README`:
8+
- Example for KDTree persisting added
9+
310
## 16.11.1
411
- `KDTree` example added to README
512
- `kd_tree` exported as a separate library

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The library is a part of the ecosystem:
1515
**Table of contents**
1616

1717
- [What is ml_algo for](#what-is-ml_algo-for)
18-
- [The library's content](#the-librarys-content)
18+
- [The library content](#the-library-content)
1919
- [Examples](#examples)
2020
- [Logistic regression](#logistic-regression)
2121
- [Linear regression](#linear-regression)
@@ -747,7 +747,7 @@ rest elements, `6.6, 3.0, 4.4, 1.4` look quite similar to our target point - `6.
747747
sense.
748748

749749
If you want to use `KDTree` outside the ml_algo ecosystem, meaning you don't want to use [ml_linalg](https://pub.dev/packages/ml_linalg) and [ml_dataframe](https://pub.dev/packages/ml_dataframe)
750-
packages in your application, you may import only `KDTree` library and use `fromIterable` constructor and `queryIterable`
750+
packages in your application, you may import only [KDTree](https://pub.dev/documentation/ml_algo/latest/kd_tree/kd_tree-library.html) library and use [fromIterable](https://pub.dev/documentation/ml_algo/latest/kd_tree/KDTree/KDTree.fromIterable.html) constructor and [queryIterable](https://pub.dev/documentation/ml_algo/latest/kd_tree/KDTree/queryIterable.html)
751751
method to perform the query:
752752

753753
```dart
@@ -764,6 +764,32 @@ void main() async {
764764
}
765765
```
766766

767+
As usual, we can persist our tree by saving it to a JSON file:
768+
769+
```dart
770+
import 'dart:io';
771+
import 'package:ml_algo/ml_algo.dart';
772+
import 'package:ml_dataframe/ml_dataframe.dart';
773+
774+
void main() async {
775+
final originalData = await loadIrisDataset();
776+
final data = originalData.dropSeries(names: ['Id', 'Species']);
777+
final tree = KDTree(data);
778+
779+
// ...
780+
781+
await tree.saveAsJson('path/to/json/file.json');
782+
783+
// ...
784+
785+
final file = await File('path/to/json/file.json').readAsString();
786+
final encodedTree = jsonDecode(file) as Map<String, dynamic>;
787+
final restoredTree = KDTree.fromJson(encodedTree);
788+
789+
print(restoredTree);
790+
}
791+
```
792+
767793
## Models retraining
768794

769795
Someday our previously shining model can degrade in terms of prediction accuracy - in this case, we can retrain it.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"P":{"DT":"F32","D":[[5.099999904632568,3.5,1.399999976158142,0.20000000298023224],[4.900000095367432,3.0,1.399999976158142,0.20000000298023224],[4.699999809265137,3.200000047683716,1.2999999523162842,0.20000000298023224],[4.599999904632568,3.0999999046325684,1.5,0.20000000298023224],[5.0,3.5999999046325684,1.399999976158142,0.20000000298023224],[5.400000095367432,3.9000000953674316,1.7000000476837158,0.4000000059604645],[4.599999904632568,3.4000000953674316,1.399999976158142,0.30000001192092896],[5.0,3.4000000953674316,1.5,0.20000000298023224],[4.400000095367432,2.9000000953674316,1.399999976158142,0.20000000298023224],[4.900000095367432,3.0999999046325684,1.5,0.10000000149011612],[5.400000095367432,3.700000047683716,1.5,0.20000000298023224],[4.800000190734863,3.4000000953674316,1.600000023841858,0.20000000298023224],[4.800000190734863,3.0,1.399999976158142,0.10000000149011612],[4.300000190734863,3.0,1.100000023841858,0.10000000149011612],[5.800000190734863,4.0,1.2000000476837158,0.20000000298023224],[5.699999809265137,4.400000095367432,1.5,0.4000000059604645],[5.400000095367432,3.9000000953674316,1.2999999523162842,0.4000000059604645],[5.099999904632568,3.5,1.399999976158142,0.30000001192092896],[5.699999809265137,3.799999952316284,1.7000000476837158,0.30000001192092896],[5.099999904632568,3.799999952316284,1.5,0.30000001192092896],[5.400000095367432,3.4000000953674316,1.7000000476837158,0.20000000298023224],[5.099999904632568,3.700000047683716,1.5,0.4000000059604645],[4.599999904632568,3.5999999046325684,1.0,0.20000000298023224],[5.099999904632568,3.299999952316284,1.7000000476837158,0.5],[4.800000190734863,3.4000000953674316,1.899999976158142,0.20000000298023224],[5.0,3.0,1.600000023841858,0.20000000298023224],[5.0,3.4000000953674316,1.600000023841858,0.4000000059604645],[5.199999809265137,3.5,1.5,0.20000000298023224],[5.199999809265137,3.4000000953674316,1.399999976158142,0.20000000298023224],[4.699999809265137,3.200000047683716,1.600000023841858,0.20000000298023224],[4.800000190734863,3.0999999046325684,1.600000023841858,0.20000000298023224],[5.400000095367432,3.4000000953674316,1.5,0.4000000059604645],[5.199999809265137,4.099999904632568,1.5,0.10000000149011612],[5.5,4.199999809265137,1.399999976158142,0.20000000298023224],[4.900000095367432,3.0999999046325684,1.5,0.10000000149011612],[5.0,3.200000047683716,1.2000000476837158,0.20000000298023224],[5.5,3.5,1.2999999523162842,0.20000000298023224],[4.900000095367432,3.0999999046325684,1.5,0.10000000149011612],[4.400000095367432,3.0,1.2999999523162842,0.20000000298023224],[5.099999904632568,3.4000000953674316,1.5,0.20000000298023224],[5.0,3.5,1.2999999523162842,0.30000001192092896],[4.5,2.299999952316284,1.2999999523162842,0.30000001192092896],[4.400000095367432,3.200000047683716,1.2999999523162842,0.20000000298023224],[5.0,3.5,1.600000023841858,0.6000000238418579],[5.099999904632568,3.799999952316284,1.899999976158142,0.4000000059604645],[4.800000190734863,3.0,1.399999976158142,0.30000001192092896],[5.099999904632568,3.799999952316284,1.600000023841858,0.20000000298023224],[4.599999904632568,3.200000047683716,1.399999976158142,0.20000000298023224],[5.300000190734863,3.700000047683716,1.5,0.20000000298023224],[5.0,3.299999952316284,1.399999976158142,0.20000000298023224],[7.0,3.200000047683716,4.699999809265137,1.399999976158142],[6.400000095367432,3.200000047683716,4.5,1.5],[6.900000095367432,3.0999999046325684,4.900000095367432,1.5],[5.5,2.299999952316284,4.0,1.2999999523162842],[6.5,2.799999952316284,4.599999904632568,1.5],[5.699999809265137,2.799999952316284,4.5,1.2999999523162842],[6.300000190734863,3.299999952316284,4.699999809265137,1.600000023841858],[4.900000095367432,2.4000000953674316,3.299999952316284,1.0],[6.599999904632568,2.9000000953674316,4.599999904632568,1.2999999523162842],[5.199999809265137,2.700000047683716,3.9000000953674316,1.399999976158142],[5.0,2.0,3.5,1.0],[5.900000095367432,3.0,4.199999809265137,1.5],[6.0,2.200000047683716,4.0,1.0],[6.099999904632568,2.9000000953674316,4.699999809265137,1.399999976158142],[5.599999904632568,2.9000000953674316,3.5999999046325684,1.2999999523162842],[6.699999809265137,3.0999999046325684,4.400000095367432,1.399999976158142],[5.599999904632568,3.0,4.5,1.5],[5.800000190734863,2.700000047683716,4.099999904632568,1.0],[6.199999809265137,2.200000047683716,4.5,1.5],[5.599999904632568,2.5,3.9000000953674316,1.100000023841858],[5.900000095367432,3.200000047683716,4.800000190734863,1.7999999523162842],[6.099999904632568,2.799999952316284,4.0,1.2999999523162842],[6.300000190734863,2.5,4.900000095367432,1.5],[6.099999904632568,2.799999952316284,4.699999809265137,1.2000000476837158],[6.400000095367432,2.9000000953674316,4.300000190734863,1.2999999523162842],[6.599999904632568,3.0,4.400000095367432,1.399999976158142],[6.800000190734863,2.799999952316284,4.800000190734863,1.399999976158142],[6.699999809265137,3.0,5.0,1.7000000476837158],[6.0,2.9000000953674316,4.5,1.5],[5.699999809265137,2.5999999046325684,3.5,1.0],[5.5,2.4000000953674316,3.799999952316284,1.100000023841858],[5.5,2.4000000953674316,3.700000047683716,1.0],[5.800000190734863,2.700000047683716,3.9000000953674316,1.2000000476837158],[6.0,2.700000047683716,5.099999904632568,1.600000023841858],[5.400000095367432,3.0,4.5,1.5],[6.0,3.4000000953674316,4.5,1.600000023841858],[6.699999809265137,3.0999999046325684,4.699999809265137,1.5],[6.300000190734863,2.299999952316284,4.400000095367432,1.2999999523162842],[5.599999904632568,3.0,4.099999904632568,1.2999999523162842],[5.5,2.5,4.0,1.2999999523162842],[5.5,2.5999999046325684,4.400000095367432,1.2000000476837158],[6.099999904632568,3.0,4.599999904632568,1.399999976158142],[5.800000190734863,2.5999999046325684,4.0,1.2000000476837158],[5.0,2.299999952316284,3.299999952316284,1.0],[5.599999904632568,2.700000047683716,4.199999809265137,1.2999999523162842],[5.699999809265137,3.0,4.199999809265137,1.2000000476837158],[5.699999809265137,2.9000000953674316,4.199999809265137,1.2999999523162842],[6.199999809265137,2.9000000953674316,4.300000190734863,1.2999999523162842],[5.099999904632568,2.5,3.0,1.100000023841858],[5.699999809265137,2.799999952316284,4.099999904632568,1.2999999523162842],[6.300000190734863,3.299999952316284,6.0,2.5],[5.800000190734863,2.700000047683716,5.099999904632568,1.899999976158142],[7.099999904632568,3.0,5.900000095367432,2.0999999046325684],[6.300000190734863,2.9000000953674316,5.599999904632568,1.7999999523162842],[6.5,3.0,5.800000190734863,2.200000047683716],[7.599999904632568,3.0,6.599999904632568,2.0999999046325684],[4.900000095367432,2.5,4.5,1.7000000476837158],[7.300000190734863,2.9000000953674316,6.300000190734863,1.7999999523162842],[6.699999809265137,2.5,5.800000190734863,1.7999999523162842],[7.199999809265137,3.5999999046325684,6.099999904632568,2.5],[6.5,3.200000047683716,5.099999904632568,2.0],[6.400000095367432,2.700000047683716,5.300000190734863,1.899999976158142],[6.800000190734863,3.0,5.5,2.0999999046325684],[5.699999809265137,2.5,5.0,2.0],[5.800000190734863,2.799999952316284,5.099999904632568,2.4000000953674316],[6.400000095367432,3.200000047683716,5.300000190734863,2.299999952316284],[6.5,3.0,5.5,1.7999999523162842],[7.699999809265137,3.799999952316284,6.699999809265137,2.200000047683716],[7.699999809265137,2.5999999046325684,6.900000095367432,2.299999952316284],[6.0,2.200000047683716,5.0,1.5],[6.900000095367432,3.200000047683716,5.699999809265137,2.299999952316284],[5.599999904632568,2.799999952316284,4.900000095367432,2.0],[7.699999809265137,2.799999952316284,6.699999809265137,2.0],[6.300000190734863,2.700000047683716,4.900000095367432,1.7999999523162842],[6.699999809265137,3.299999952316284,5.699999809265137,2.0999999046325684],[7.199999809265137,3.200000047683716,6.0,1.7999999523162842],[6.199999809265137,2.799999952316284,4.800000190734863,1.7999999523162842],[6.099999904632568,3.0,4.900000095367432,1.7999999523162842],[6.400000095367432,2.799999952316284,5.599999904632568,2.0999999046325684],[7.199999809265137,3.0,5.800000190734863,1.600000023841858],[7.400000095367432,2.799999952316284,6.099999904632568,1.899999976158142],[7.900000095367432,3.799999952316284,6.400000095367432,2.0],[6.400000095367432,2.799999952316284,5.599999904632568,2.200000047683716],[6.300000190734863,2.799999952316284,5.099999904632568,1.5],[6.099999904632568,2.5999999046325684,5.599999904632568,1.399999976158142],[7.699999809265137,3.0,6.099999904632568,2.299999952316284],[6.300000190734863,3.4000000953674316,5.599999904632568,2.4000000953674316],[6.400000095367432,3.0999999046325684,5.5,1.7999999523162842],[6.0,3.0,4.800000190734863,1.7999999523162842],[6.900000095367432,3.0999999046325684,5.400000095367432,2.0999999046325684],[6.699999809265137,3.0999999046325684,5.599999904632568,2.4000000953674316],[6.900000095367432,3.0999999046325684,5.099999904632568,2.299999952316284],[5.800000190734863,2.700000047683716,5.099999904632568,1.899999976158142],[6.800000190734863,3.200000047683716,5.900000095367432,2.299999952316284],[6.699999809265137,3.299999952316284,5.699999809265137,2.5],[6.699999809265137,3.0,5.199999809265137,2.299999952316284],[6.300000190734863,2.5,5.0,1.899999976158142],[6.5,3.0,5.199999809265137,2.0],[6.199999809265137,3.4000000953674316,5.400000095367432,2.299999952316284],[5.900000095367432,3.0,5.099999904632568,1.7999999523162842]]},"L":1,"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"L":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[13]},"R":{"I":3,"P":[]},"P":[8]},"R":{"I":2,"P":[3]},"P":[38]},"R":{"I":1,"L":{"I":2,"L":{"I":3,"P":[1]},"R":{"I":3,"P":[]},"P":[25]},"R":{"I":2,"P":[37]},"P":[9]},"P":[12]},"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[93]},"R":{"I":3,"P":[]},"P":[60]},"R":{"I":2,"P":[45]},"P":[57]},"R":{"I":1,"L":{"I":2,"P":[81]},"R":{"I":2,"P":[64]},"P":[79]},"P":[98]},"P":[41]},"R":{"I":3,"L":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[53]},"R":{"I":3,"P":[]},"P":[89]},"R":{"I":2,"P":[90]},"P":[69]},"R":{"I":1,"L":{"I":2,"L":{"I":3,"P":[99]},"R":{"I":3,"P":[]},"P":[94]},"R":{"I":2,"P":[95]},"P":[96]},"P":[88]},"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[59]},"R":{"I":3,"P":[]},"P":[106]},"R":{"I":2,"P":[66]},"P":[84]},"R":{"I":1,"L":{"I":2,"P":[113]},"R":{"I":2,"P":[142]},"P":[101]},"P":[121]},"P":[55]},"P":[80]},"R":{"I":2,"L":{"I":3,"L":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[42]},"R":{"I":3,"P":[]},"P":[47]},"R":{"I":2,"P":[22]},"P":[2]},"R":{"I":1,"L":{"I":2,"L":{"I":3,"P":[28]},"R":{"I":3,"P":[]},"P":[0]},"R":{"I":2,"P":[33]},"P":[36]},"P":[35]},"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[49]},"R":{"I":3,"P":[]},"P":[6]},"R":{"I":2,"P":[40]},"P":[7]},"R":{"I":1,"L":{"I":2,"P":[31]},"R":{"I":2,"P":[16]},"P":[17]},"P":[39]},"P":[4]},"R":{"I":3,"L":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[30]},"R":{"I":3,"P":[]},"P":[29]},"R":{"I":2,"P":[24]},"P":[11]},"R":{"I":1,"L":{"I":2,"L":{"I":3,"P":[27]},"R":{"I":3,"P":[]},"P":[20]},"R":{"I":2,"P":[10]},"P":[48]},"P":[46]},"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[26]},"R":{"I":3,"P":[]},"P":[43]},"R":{"I":2,"P":[44]},"P":[21]},"R":{"I":1,"L":{"I":2,"P":[18]},"R":{"I":2,"P":[15]},"P":[5]},"P":[23]},"P":[19]},"P":[32]},"P":[34]},"R":{"I":1,"L":{"I":2,"L":{"I":3,"L":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[62]},"R":{"I":3,"P":[]},"P":[92]},"R":{"I":2,"P":[73]},"P":[67]},"R":{"I":1,"L":{"I":2,"L":{"I":3,"P":[97]},"R":{"I":3,"P":[]},"P":[87]},"R":{"I":2,"P":[58]},"P":[74]},"P":[71]},"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[68]},"R":{"I":3,"P":[]},"P":[126]},"R":{"I":2,"P":[127]},"P":[78]},"R":{"I":1,"L":{"I":2,"P":[123]},"R":{"I":2,"P":[76]},"P":[54]},"P":[72]},"P":[63]},"R":{"I":3,"L":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[119]},"R":{"I":3,"P":[]},"P":[134]},"R":{"I":2,"P":[133]},"P":[83]},"R":{"I":1,"L":{"I":2,"L":{"I":3,"P":[108]},"R":{"I":3,"P":[]},"P":[107]},"R":{"I":2,"P":[129]},"P":[116]},"P":[103]},"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[114]},"R":{"I":3,"P":[]},"P":[128]},"R":{"I":2,"P":[147]},"P":[132]},"R":{"I":1,"L":{"I":2,"P":[118]},"R":{"I":2,"P":[135]},"P":[122]},"P":[130]},"P":[111]},"P":[146]},"R":{"I":2,"L":{"I":3,"L":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[61]},"R":{"I":3,"P":[]},"P":[91]},"R":{"I":2,"P":[14]},"P":[51]},"R":{"I":1,"L":{"I":2,"L":{"I":3,"P":[65]},"R":{"I":3,"P":[]},"P":[86]},"R":{"I":2,"P":[50]},"P":[52]},"P":[75]},"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[138]},"R":{"I":3,"P":[]},"P":[149]},"R":{"I":2,"P":[56]},"P":[70]},"R":{"I":1,"L":{"I":2,"P":[77]},"R":{"I":2,"P":[141]},"P":[145]},"P":[110]},"P":[85]},"R":{"I":3,"L":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[112]},"R":{"I":3,"P":[]},"P":[137]},"R":{"I":2,"P":[124]},"P":[139]},"R":{"I":1,"L":{"I":2,"P":[105]},"R":{"I":2,"P":[131]},"P":[125]},"P":[102]},"R":{"I":0,"L":{"I":1,"L":{"I":2,"L":{"I":3,"P":[140]},"R":{"I":3,"P":[]},"P":[100]},"R":{"I":2,"P":[136]},"P":[148]},"R":{"I":1,"L":{"I":2,"P":[143]},"R":{"I":2,"P":[109]},"P":[120]},"P":[144]},"P":[117]},"P":[115]},"P":[104]},"P":[82]},"D":"float32","S":1}

0 commit comments

Comments
 (0)