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
  • Linear classifier:

    • Logistic regression (with "one-vs-all" multiclass classification)
    • Softmax regression
  • Non-parametric regression:

    • KNN regression

The library's structure

  • CrossValidator. Factory, that creates instances of a cross validator. In a few words, this entity allows researchers to fit different hyperparameters of machine learning algorithms, assessing prediction quality on different parts of a dataset.

  • LinearClassifier.logisticRegressor. An algorithm, that performs simplest linear classification. If you want to use this classifier for your data, please, make sure, that your data is linearly separable. Multiclass classification is also supported (see ovr classification)

  • LinearClassifier.softmaxRegressor. An algorithm, that performs simplest linear multiclass classification. As well as for logistic regression, if you want to use this classifier for your data, please, make sure, that your data is linearly separable.

  • LinearRegressor.gradient. A well-known algorithm, that performs linear regression using gradient vector of a cost function.

  • LinearRegressor.lasso An algorithm, that performs feature selection along with regression process. The heart of the algorithm - coordinate descent optimization. If you want to decide, which features are less important - go ahead and use this regressor.

  • NoNParametricRegressor.nearestNeighbor An algorithm, that makes prediction for each new observation based on first k closest observations from training data. It has quite high computational complexity, but in the same time it may easily catch non-linear pattern of the data.

Examples

Logistic regression

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

Import all necessary packages. First, it's needed to ensure, if you have ml_preprocessing package in your dependencies:

dependencies:
  ml_preprocessing: ^3.2.0

We need this repo to parse raw data in order to use it farther. For more details, please, visit ml_preprocessing repository page.

import 'dart:async';

import 'package:ml_algo/ml_algo.dart';
import 'package:ml_preprocessing/ml_preprocessing.dart';

Download dataset from Pima Indians Diabets Database and read it (of course, you should provide a proper path to your downloaded file):

final data = DataFrame.fromCsv('datasets/pima_indians_diabetes_database.csv', 
  labelName: 'class variable (0 or 1)');
final features = await data.features;
final labels = await data.labels;

Data in this file is represented by 768 records and 8 features. 9th column is a label column, it contains either 0 or 1 on each row. This column is our target - we should predict a class label for each observation. Therefore, we should point, where to get label values. Let's use labelName parameter for that (labels column name, 'class variable (0 or 1)' in our case).

Processed features and labels are contained in data structures of Matrix type. To get more information about Matrix type, please, visit ml_linal repo

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

final validator = CrossValidator.KFold(numberOfFolds: 5);

All are set, so, we can do our classification.

Let's create a logistic regression classifier instance with full-batch gradient descent optimizer:

final model = LinearClassifier.logisticRegressor(
    initialLearningRate: .8,
    iterationsLimit: 500,
    gradientType: GradientType.batch,
    fitIntercept: true,
    interceptScale: 0.1,
    learningRateType: LearningRateType.constant);

Evaluate our model via accuracy metric:

final accuracy = validator.evaluate(model, featuresMatrix, labels, MetricType.accuracy);

Let's print score:

print('accuracy on classification: ${accuracy.toStringAsFixed(2)}');

We will see something like this:

acuracy on classification: 0.77

All the code above all together:

import 'dart:async';

import 'package:ml_algo/ml_algo.dart';
import 'package:ml_preprocessing/ml_preprocessing.dart';

Future main() async {
  final data = DataFrame.fromCsv('datasets/pima_indians_diabetes_database.csv', 
     labelName: 'class variable (0 or 1)');
  
  final features = await data.features;
  final labels = await data.labels;

  final validator = CrossValidator.kFold(numberOfFolds: 5);
  
  final model = LinearClassifier.logisticRegressor(
    initialLearningRate: .8,
    iterationsLimit: 500,
    gradientType: GradientType.batch,
    fitIntercept: true,
    interceptScale: .1,
    learningRateType: LearningRateType.constant);
  
  final accuracy = validator.evaluate(model, features, labels, MetricType.accuracy);

  print('accuracy on classification: ${accuracy.toStringFixed(2)}');
}

Softmax regression

Let's classify another famous dataset - Iris dataset. Data in this csv is separated into 3 classes - therefore we need to use different approach to data classification - Softmax regression.

As usual, start with data preparation. Before we start, we should update our pubspec's dependencies with xrange` library:

dependencies:
    ...
    xrange: ^0.0.5
    ...

Download the file and read it:

final data = DataFrame.fromCsv('datasets/iris.csv',
    labelName: 'Species',
    columns: [ZRange.closed(1, 5)],
    categories: {
      'Species': CategoricalDataEncoderType.oneHot,
    },
);

final features = await data.features;
final labels = await data.labels;

The csv database has 6 columns, but we need to get rid of the first column, because it contains just ID of every observation - it's absolutely useless data. So, as you may notice, we provided a columns range to exclude ID-column:

columns: [ZRange.closed(1, 5)]

Also, since the label column 'Species' has categorical data, we encoded it to numerical format:

categories: {
  'Species': CategoricalDataEncoderType.oneHot,
},

Next step - create a cross validator instance:

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

And finally, create an instance of the classifier:

final softmaxRegressor = LinearClassifier.softmaxRegressor(
      initialLearningRate: 0.03,
      iterationsLimit: null,
      minWeightsUpdate: 1e-6,
      randomSeed: 46,
      learningRateType: LearningRateType.constant);

Evaluate quality of prediction:

final accuracy = validator.evaluate(softmaxRegressor, features, labels, MetricType.accuracy);

print('Iris dataset, softmax regression: accuracy is '
  '${accuracy.toStringAsFixed(2)}'); // It yields 0.93

Gather all the code above all together:

import 'dart:async';

import 'package:ml_algo/ml_algo.dart';
import 'package:ml_preprocessing/ml_preprocessing.dart';
import 'package:xrange/zrange.dart';

Future main() async {
  final data = DataFrame.fromCsv('datasets/iris.csv',
    labelName: 'Species',
    columns: [ZRange.closed(1, 5)],
    categories: {
      'Species': CategoricalDataEncoderType.oneHot,
    },
  );

  final features = await data.features;
  final labels = await data.labels;

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

  final softmaxRegressor = LinearClassifier.softmaxRegressor(
      initialLearningRate: 0.03,
      iterationsLimit: null,
      minWeightsUpdate: 1e-6,
      randomSeed: 46,
      learningRateType: LearningRateType.constant);

  final accuracy = validator.evaluate(
      softmaxRegressor, features, labels, MetricType.accuracy);

  print('Iris dataset, softmax regression: accuracy is '
      '${accuracy.toStringAsFixed(2)}');
}

For more examples, please, visit ml_algo_examples repository

Contacts

If you have questions, feel free to write me on

Libraries

ml_algo