MLKIT is a machine learning framework designed for tiny bare-metal controllers who supports micropython. The machine learning code is primarily developed from scratch. The APIs are developed in such a way so that compatibility with popular machine learning tools such as Scikit-Learn is maintained.
Note: Some portion of code is build on Numpy array. You need ULAB compatible firmware for Numpy support and to run the code. Some pre-compiled firmware of different port can be found in firmware directory.
Acknowledgement: Some part of the source code is taken from here and modified for running in MicroPython environment.
Highlights and supported features:
- Logistic Regression
- K-Nearest Neighbors (KNN) classifier
- Single Layer Perceptron
Data Processing
- csv_read() #Reading CSV file
- StandardScaler() # Scikit-learn's standard scaler
- train_test_split() # splitting into train and test set
Metrics
- accuracy score()
- classification_report()
- confusion_matrix()
Simply download the mlkit directory and copy it into supported board. You can use editor such as Thonny for editing, running and copying files/folders from PC to board and vice versa. Then run any test code provided (E.g. test_knn01.py). The test script should be outside mlkit directory as shown in repo.
StandardScaler()
from mlkit.preprocessing import StandardScaler
scaler = StandardScaler()
scaler = scaler.fit(X)
scaler = scaler.transform(X)
train_test_split()
Splitting the dataset into train and test set with specific test size and random state.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=5)
Note: true_labels and predicted_labels are Numpy array
accuracy_score()
from mlkit.metrics import accuracy_score
accuracy = accuracy_score(true_labels, predicted_labels)
classification_report()
from mlkit.metrics import classification_report
classification_report(true_labels, predicted_labels,num_labels=<number of labels>)
confusion_matrix()
Note: True and Predicted labels must be rounded
from mlkit.metrics import confusion_matrix
confusion_matrix(true_labels, predicted_labels,num_labels=<number of labels>)
csv_read()
Reading the content of a comma separated format file from base directory and storing the content into Numpy array. Known issue: Currently support for numbers only, not characters.If the file contain string or character or header string/column, it will throw an error.
from ulab import numpy as np
data = csv_read('foo.csv')
np_round()
Rounding 1-D Numpy array upto specified decimal digits
from ulab import numpy as np
from mlkit.utils import np_round
x = np.array([1.122, 2.664, 9.883, 4.663])
y = np_round(x) #rounding upto zero decimal places, default=0
y1 = np_round(x,2) #rounding upto two decimal places
Note: rounding function is not implemented in ulab, thats why you need to import it separately from implemented custom functions.
LogisticRegression()
Initialize LogisticRegression classifier object.
from ulab import numpy as np
from mlkit.utils import np_round
from mlkit.activation import Sigmoid
from mlkit.linear_model import LogisticRegression
#Generate data
x = np.linspace(-5,5,20).reshape((-1, 1))
y = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
clf = LogisticRegression(gradient_descent=True)
clf.fit(x,y)
x_test=np.array([-2.0, -3.7, -4.1, -2.8, 0.9, 2.4, 4.2, 5.1]).reshape((-1,1))
ypred = np_round(clf.predict(x_test))
print(ypred)
KNeighborsClassifier()
K-Nearest neighbors classifier. In this version, only Euclidean distance method is used to calculate the distance among different features.
from ulab import numpy as np
from mlkit.neighbors import KNeighborsClassifier
# Sample data
X_train = np.array([[1,2,1], [2,3,2], [3,4,2], [4,5,5]])
y_train = np.array([0, 0, 1, 1])
# Initialize and fit the classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
# Test data
X_test = np.array([[1,2,.4], [4,5,4]])
# Predict
predictions = knn.predict(X_test)
print("Predictions:", predictions)
Perceptron()
A single layer Perceptron with specified activation function.
from mlkit.linear_model import Perceptron
clf = Perceptron(n_iterations=1000,learning_rate=0.001,loss=CrossEntropy,activation_function=Sigmoid)
clf.fit(X_train, y_train)
predictions = clf.predict(X_test)
Note: Currently supported loss functions are SquareLoss and CrossEntropy