ml_algo 3.5.3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new47

Build Status

Machine learning algorithms with dart

Following algorithms are implemented:

  • Linear regression:

    • gradient descent models (batch, mini-batch, stochastic) with ridge regularization
    • lasso model (feature selection model)
  • Linear classifier:

    • Logistic regression (with "one-vs-all" multinomial classification)

Usage

A simple usage example (Linear regression with stochastic gradient descent):

Import all necessary packages:

import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:ml_algo/ml_algo.dart';
import 'package:csv/csv.dart' as csv;

Read csv-file advertising.csv with test data:

final csvCodec = csv.CsvCodec(eol: '\n');
final input = File('example/datasets/advertising.csv').openRead();
final fields = (await input.transform(utf8.decoder)
  .transform(csvCodec.decoder).toList())
  .sublist(1);

Data in this file is represented by 200 lines, every line contains 4 elements. First 3 elements of every line are features and the last one is label. Let's extract features from the data. Declare utility method extractFeatures, that extracts 3 elements from every line:

List<double> extractFeatures(List<dynamic> item) => item.sublist(0, 3)
      .map((dynamic feature) => (feature as num).toDouble())
      .toList();

...and finally get all features:

final features = fields
  .map(extractFeatures)
  .toList(growable: false);

...and labels (last element of a every line)

final labels = Float32x4VectorFactory.from(fields.map((List<dynamic> item) => (item.last as num).toDouble()));

Create an instance of CrossValidator class for evaluating quality of our predictor

final validator = CrossValidator<Float32x4>.KFold();

Create a linear regressor instance with stochastic gradient descent optimizer:

final sgdRegressor = GradientRegressor(type: GradientType.stochastic, iterationLimit: 100000,
                         learningRate: 1e-5, learningRateType: LearningRateType.constant);

Evaluate our model via MAPE-metric:

final scoreMAPE = validator.evaluate(sgdRegressor, Float32x4Matrix.from(features), labels, metric: MetricType.mape);

Let's print score:

print("score (MAPE): ${scoreMAPE}");

We will see something like this:

score (MAPE): 31.221150755882263

For more examples please see examples folder

Changelog

3.5.3

  • dartfmt tool applied to all necessary files

3.5.2

  • Travis configuration file name corrected

3.5.1

  • Travis integration added

3.5.0

  • Vectorized cost functions applied

3.4.0

  • ml_linalg 2.0.0 supported

3.3.0

  • Matrix-based gradient calculation added for log likelihood cost function

3.2.0

  • Matrix-based gradient calculation added for squared cost function

3.1.2

  • Description corrected

3.1.1

  • dartfm tool applied

3.1.0

  • Get rid of MLVector's deprecated methods

3.0.0

  • Library public release

2.0.0

  • ml_linalg supported

1.2.1

  • subVector -> subvector

1.2.0

  • Matrices support added

1.1.1

  • Examples fixed, dependencies fixed

1.1.0

  • Support of updated linalg package

1.0.1

  • Readme updated, dependencies fixed

1.0.0

  • Migration to dart 2.0

0.38.1

0.38.0

  • Lasso solution refactored

0.37.0

  • Support of linalg package (former simd_vector)

0.36.0

  • Intercept term considered (fitIntercept and interceptScale parameters)

0.35.1

  • Logistic regression tests improved

0.35.0

  • One versus all refactored, tests for logistic regression added

0.34.0

  • One versus all classifier

0.33.0

  • Gradient descent regressor type enum added

0.32.1

  • Gradient optimizer unit tests

0.32.0

  • Get rid of derivative computation

0.31.0

  • Get rid of di package usage

0.30.1

  • File structure flattened

0.30.0

  • Redundant gradient optimizers removed

0.29.0

  • part ... part of directives removed

0.28.0

  • Coordinate descent optimizer added
  • Lasso regressor added

0.27.0

  • Gradient calculation changed

0.26.1

  • Code was optimized (removed unnecessary)
  • Refactoring

0.26.0

  • More distinct modularity was added to the library
  • Unit tests were fixed

0.25.0

  • Tests for gradient optimizers were added
  • Gradient calculator was created as a separate entity
  • Initial weights generator was created as a separate entity
  • Learning rate generator was created as a separate entity

0.24.0

  • All implementations were hidden

0.23.0

  • findMaxima and findMinima methods were added to Optimizer interface

0.22.0

  • File structure reorganized, predictor classes refactored
  • README.md updated

0.21.0

  • Logistic regression model added (with example)

0.20.2

  • README.md updated

0.20.1

  • simd_vector dependency url fixed

0.20.0

  • Repository dependency corrected (dart_vector -> simd_vector)

0.19.0

  • Support for Float32x4Vector class was added (from dart_vector library)
  • Type List for label (target) list replaced with Float32List (in Predictor.train() and Optimizer.optimize())

0.18.0

  • class Vector and enum Norm were extracted to separate library (https://github.com/gyrdym/dart_vector.git)

0.17.0

  • Common interface for loss function was added
  • Derivative calculation was fixed (common canonical method was used)
  • Squared loss function was added as a separate class

0.16.0

  • README.md was actualized

0.15.0

  • Tests for gradient optimizers were added
  • Interfaces (almost for all entities) for DI and IOC mechanism were added
  • Randomizer class was added
  • Removed separate classes for k-fold cross validation and lpo cross validation, now it resides in CrossValidation class

0.14.0

  • L1 and L2 regularization added

0.13.0

  • Script for running all unit tests added

0.12.0

  • Vector interface removed
  • Regular vector implementation removed
  • TypedVector -> Vector
  • Implicit vectors constructing replaced with explicit new-instantiation

0.11.0

  • Entity names correction

0.10.0

  • K-fold cross validation added (KFoldCrossValidation)
  • Leave P out cross validation added (LpoCrossValidation)
  • DataTrainTestSplitter was removed

0.9.0

  • copy, fill methods were added to Vector

0.8.0

  • Reflection was removed for all cases (Vector instantiation, Optimizer instantiation)

0.7.0

  • Abstract Vector-class was added as a base for typed and regular vector classes

0.6.0

  • Manhattan norm support was added

0.5.2

  • README file was extended and clarified

0.5.1

  • Random interval obtaining for the mini-batch gradient descent was fixed

0.5.0

  • BGDOptimizer, MBGDOptimizer and GradientOptimizer were added

0.4.0

  • OptimizerInterface was added
  • Stochastic gradient descent optimizer was extracted from the linear regressor class
  • Line separators changed for all files (CRLF -> LF)

0.3.1

  • tests for sum, abs, fromRange methods of the TypedVector were added
  • tests for DataTrainTestSplitter was added

0.3.0

  • MAPE cost function was added

0.2.0

  • SGD Regressor refactored (rmse on training removed, estimator added) + example extended

0.1.0

  • Implementation of -, *, / operators and all vectors methods added to the TypedVector

0.0.1

  • Initial version

example/main.dart

import 'dart:async';

import 'gradient_descent_regression.dart';
import 'lasso_regression.dart';
import 'logistic_regression.dart';

Future main() async {
  print('Learning in process, wait a bit...');
  print('\n');

  final sgdQuality = await gradientDescentRegression();
  print('========================================================================================================');
  print('|| Stochastic gradient descent regression, K-fold cross validation with MAPE metric (error in percents):');
  print('|| ${sgdQuality.toStringAsFixed(2)}%');
  print('========================================================================================================');

  print('\n');

  final lassoQuality = await lassoRegression();
  print('========================================================================================================');
  print('|| Lasso regression, K-fold cross validation with MAPE metric (error in percent):');
  print('|| ${lassoQuality.toStringAsFixed(2)}%');
  print('=========================================================================================================');

  print('\n');
  print('Learning of logistic regressor in progress, wait a bit... (best possible parameters are being fitted)');
  print('\n');

  final logisticRegressionError = await logisticRegression();
  print('=========================================================================================================');
  print('|| Logistic regression, error on cross validation: ');
  print('|| ${(logisticRegressionError * 100).toStringAsFixed(2)}%,');
  print('=========================================================================================================');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ml_algo: ^3.5.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ml_algo/ml_algo.dart';
  
Version Uploaded Documentation Archive
3.5.3 Dec 11, 2018 Go to the documentation of ml_algo 3.5.3 Download ml_algo 3.5.3 archive
3.5.0 Dec 11, 2018 Go to the documentation of ml_algo 3.5.0 Download ml_algo 3.5.0 archive
3.4.0 Dec 7, 2018 Go to the documentation of ml_algo 3.4.0 Download ml_algo 3.4.0 archive
3.3.0 Dec 5, 2018 Go to the documentation of ml_algo 3.3.0 Download ml_algo 3.3.0 archive
3.2.0 Dec 4, 2018 Go to the documentation of ml_algo 3.2.0 Download ml_algo 3.2.0 archive
3.1.2 Dec 4, 2018 Go to the documentation of ml_algo 3.1.2 Download ml_algo 3.1.2 archive
3.1.1 Dec 4, 2018 Go to the documentation of ml_algo 3.1.1 Download ml_algo 3.1.1 archive
3.1.0 Dec 4, 2018 Go to the documentation of ml_algo 3.1.0 Download ml_algo 3.1.0 archive
3.0.0 Dec 4, 2018 Go to the documentation of ml_algo 3.0.0 Download ml_algo 3.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
90
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
47
Learn more about scoring.

We analyzed this package on Dec 12, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:ml_algo/ml_algo.dart.

Health issues and suggestions

Document public APIs (-10 points)

29 out of 29 API elements (library, class, field or method) have no adequate dartdoc content. Good documentation improves code readability and discoverability through search.

Format lib/src/classifier/linear_classifier.dart.

Run dartfmt to format lib/src/classifier/linear_classifier.dart.

Format lib/src/classifier/logistic_regression.dart.

Run dartfmt to format lib/src/classifier/logistic_regression.dart.

Format lib/src/cost_function/cost_function.dart.

Run dartfmt to format lib/src/cost_function/cost_function.dart.

Fix additional 25 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/cost_function/cost_function_factory.dart (Run dartfmt to format lib/src/cost_function/cost_function_factory.dart.)
  • lib/src/cost_function/log_likelihood.dart (Run dartfmt to format lib/src/cost_function/log_likelihood.dart.)
  • lib/src/cost_function/squared.dart (Run dartfmt to format lib/src/cost_function/squared.dart.)
  • lib/src/data_preprocessing/intercept_preprocessor.dart (Run dartfmt to format lib/src/data_preprocessing/intercept_preprocessor.dart.)
  • lib/src/data_splitter/k_fold.dart (Run dartfmt to format lib/src/data_splitter/k_fold.dart.)
  • lib/src/math/math_analysis/gradient_calculator.dart (Run dartfmt to format lib/src/math/math_analysis/gradient_calculator.dart.)
  • lib/src/math/math_analysis/gradient_calculator_impl.dart (Run dartfmt to format lib/src/math/math_analysis/gradient_calculator_impl.dart.)
  • lib/src/math/randomizer/randomizer.dart (Run dartfmt to format lib/src/math/randomizer/randomizer.dart.)
  • lib/src/math/randomizer/randomizer_impl.dart (Run dartfmt to format lib/src/math/randomizer/randomizer_impl.dart.)
  • lib/src/metric/classification/accuracy.dart (Run dartfmt to format lib/src/metric/classification/accuracy.dart.)
  • lib/src/metric/classification/metric_factory.dart (Run dartfmt to format lib/src/metric/classification/metric_factory.dart.)
  • lib/src/metric/regression/mape.dart (Run dartfmt to format lib/src/metric/regression/mape.dart.)
  • lib/src/metric/regression/rmse.dart (Run dartfmt to format lib/src/metric/regression/rmse.dart.)
  • lib/src/model_selection/cross_validator.dart (Run dartfmt to format lib/src/model_selection/cross_validator.dart.)
  • lib/src/model_selection/evaluable.dart (Run dartfmt to format lib/src/model_selection/evaluable.dart.)
  • lib/src/optimizer/coordinate.dart (Run dartfmt to format lib/src/optimizer/coordinate.dart.)
  • lib/src/optimizer/gradient.dart (Run dartfmt to format lib/src/optimizer/gradient.dart.)
  • lib/src/optimizer/initial_weights_generator/initial_weights_generator_factory.dart (Run dartfmt to format lib/src/optimizer/initial_weights_generator/initial_weights_generator_factory.dart.)
  • lib/src/optimizer/initial_weights_generator/zero_weights_generator.dart (Run dartfmt to format lib/src/optimizer/initial_weights_generator/zero_weights_generator.dart.)
  • lib/src/optimizer/learning_rate_generator/generator_factory.dart (Run dartfmt to format lib/src/optimizer/learning_rate_generator/generator_factory.dart.)
  • lib/src/optimizer/optimizer.dart (Run dartfmt to format lib/src/optimizer/optimizer.dart.)
  • lib/src/regressor/gradient.dart (Run dartfmt to format lib/src/regressor/gradient.dart.)
  • lib/src/regressor/lasso.dart (Run dartfmt to format lib/src/regressor/lasso.dart.)
  • lib/src/regressor/linear_regressor.dart (Run dartfmt to format lib/src/regressor/linear_regressor.dart.)
  • lib/src/score_to_prob_link_function/link_function_impl.dart (Run dartfmt to format lib/src/score_to_prob_link_function/link_function_impl.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
csv ^4.0.0 4.0.1
ml_linalg ^2.0.0 2.2.2
Dev dependencies
benchmark_harness >=1.0.0 <2.0.0
build_runner ^0.10.0
build_test ^0.10.2
mockito ^3.0.0
test ^1.2.0