Build Status Coverage Status pub package Gitter Chat

Machine learning algorithms with dart

Table of contents

What is the ml_algo for?

The main purpose of the library - to give developers, interested both in Dart language and data science, native Dart implementation of machine learning algorithms. This library targeted to dart vm, so, to get smoothest experience with the lib, please, do not use it in a browser.

Following algorithms are implemented:

  • Linear regression:

    • Gradient descent algorithm (batch, mini-batch, stochastic) with ridge regularization
    • Lasso regression (feature selection model)
  • Linear classifier:

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

The library's structure

To provide main purposes of machine learning, the library exposes the following classes:

Usage

Real life example

Let's classify records from well-known dataset - Pima Indians Diabets Database via Logistic regressor

Import all necessary packages:

import 'dart:async';

import 'package:ml_algo/ml_algo.dart';

Read csv-file pima_indians_diabetes_database.csv with test data. You can use csv from the library's datasets directory:

final data = MLData.fromCsvFile('datasets/pima_indians_diabetes_database.csv');
final features = await data.features;
final labels = await data.labels;

Data in this file is represented by 768 records and 8 features. Processed features are contained in a data structure of MLMatrix type and processed labels are contained in a data structure of MLVector type. To get more information about these types, please, visit ml_linal repo

Then, we should create an instance of CrossValidator class for fitting hyperparameters of our model

final validator = CrossValidator.KFold();

All are set, so, we can perform our classification. For better hyperparameters fitting, let's create a loop in order to try each value of a chosen hyperparameter in a defined range:

final step = 0.001;
final limit = 0.6;
double minError = double.infinity;
double bestLearningRate = 0.0;
for (double rate = step; rate < limit; rate += step) {
  // ...
}

Let's create a logistic regression classifier instance with stochastic gradient descent optimizer in the loop's body:

final logisticRegressor = LinearClassifier.logisticRegressor(
        iterationsLimit: 100,
        initialLearningRate: rate,
        learningRateType: LearningRateType.constant);

Evaluate our model via accuracy metric:

final error = validator.evaluate(logisticRegressor, featuresMatrix, labels, MetricType.accuracy);
if (error < minError) {
  minError = error;
  bestLearningRate = rate;
}

Let's print score:

print('best error on classification: ${(minError * 100).toFixed(2)}');
print('best learning rate: ${bestLearningRate.toFixed(3)}');

Best model parameters search takes much time so far, so be patient. After the search is over, we will see something like this:

best error on classification: 35.5%
best learning rate: 0.155

All the code above all together:

import 'dart:async';

import 'package:ml_algo/ml_algo.dart';

Future<double> logisticRegression() async {
  final data = CsvMLData.fromFile('datasets/pima_indians_diabetes_database.csv');
  final features = await data.features;
  final labels = await data.labels;

  final validator = CrossValidator.kFold(numberOfFolds: 7);

  final step = 0.001;
  final limit = 0.6;

  double minError = double.infinity;
  double bestLearningRate = 0.0;

  for (double rate = step; rate < limit; rate += step) {
    final logisticRegressor = LinearClassifier.logisticRegressor(
      iterationsLimit: 100,
      initialLearningRate: rate,
      learningRateType: LearningRateType.constant);
    final error = validator.evaluate(logisticRegressor, features, labels, MetricType.accuracy);
    if (error < minError) {
      minError = error;
      bestLearningRate = rate;
    }
  }

  print('best error on classification: ${(minError * 100).toFixed(2)}');
  print('best learning rate: ${bestLearningRate.toFixed(3)}');
}

For more examples please see examples folder

Contacts

If you have questions, feel free to write me on

Libraries

ml_algo