ml_algo 9.2.1
ml_algo: ^9.2.1 copied to clipboard
Machine learning algorithms written in native dart (without bindings to any popular ML libraries, just pure Dart implementation)
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)
- Softmax regression
The library's structure #
To provide main purposes of machine learning, the library exposes the following classes:
-
DataFrame. Factory, that creates instances of different adapters for data. For example, one can create a csv reader, that makes work with csv data easier: you just need to point, where your dataset resides and then get features and labels in convenient data science friendly format.
-
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. Wiki article about cross validation process.
-
LinearClassifier.logisticRegressor. A class, that performs simplest linear classification. If you want to use this classifier for your data, please, make sure, that your data is linearly separably. Multiclass classification is also supported (see ovr classification)
-
LinearClassifier.softmaxRegressor. A class, 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 separably.
-
LinearRegressor.gradient. An algorithm, that performs geometry-based linear regression using gradient vector of a cost function.
-
LinearRegressor.lasso An algorithm, that performs feature selection along with regression process. It uses coordinate descent optimization and subgradient vector instead of gradient descent optimization and gradient vector like in
LinearRegressor.gradient
to provide regression. If you want to decide, which features are less important - go ahead and use this regressor.
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 a csv file from the library's
datasets directory:
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 values of class labels 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 a data structure 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 perform 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: .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: ${maxAccuracy.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';
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)}');
}
For more examples please see examples folder
Contacts #
If you have questions, feel free to write me on