• Stars
    star
    547
  • Rank 81,254 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tiny lib with pocket-sized implementations of machine learning models in NumPy, most of which will fit in a tweet.

NapkinML

About

Pocket-sized implementations of machine learning models, most of which will fit in a tweet.

Table of Contents

Installation

$ git clone https://github.com/eriklindernoren/NapkinML
$ cd NapkinML
$ sudo python setup.py install

Implementations

K-Means

class KMeans:
    def fit(self, X, k, n_iter=200):
        centers = random.sample(list(X), k)
        for i in range(n_iter):
            clusters = np.argmin(cdist(X, centers), axis=1)
            centers = np.array([X[clusters == c].mean(0) for c in clusters])
        return clusters
$ python napkin_ml/examples/kmeans.py

Figure: K-Means clustering of the Iris dataset.

K-Nearest Neighbors

class KNN:
    def predict(self, k, Xt, X, y):
        idx = np.argsort(cdist(Xt, X))[:, :k]
        y_pred = [np.bincount(y[i]).argmax() for i in idx]
        return y_pred
$ python napkin_ml/examples/knn.py

Figure: Classification of the Iris dataset with K-Nearest Neighbors.

Linear Regression

class LinearRegression:
    def fit(self, X, y):
        self.w = np.linalg.lstsq(X, y, rcond=None)[0]
    def predict(self, X):
        return X.dot(self.w)
$ python napkin_ml/examples/linear_regression.py

Figure: Linear Regression.

Linear Discriminant Analysis

class LDA:
    def fit(self, X, y):
        cov_sum = sum([np.cov(X[y == val], rowvar=False) for val in [0, 1]])
        mean_diff = X[y == 0].mean(0) - X[y == 1].mean(0)
        self.w = np.linalg.inv(cov_sum).dot(mean_diff)
    def predict(self, X):
        return 1 * (X.dot(self.w) < 0)

Logistic Regression

class LogisticRegression:
    def fit(self, X, y, n_iter=4000, lr=0.01):
        self.w = np.random.rand(X.shape[1])
        for _ in range(n_iter):
            self.w -= lr * (self.predict(X) - y).dot(X)
    def predict(self, X):
        return sigmoid(X.dot(self.w))
$ python napkin_ml/examples/logistic_regression.py

Figure: Classification with Logistic Regression.

Multilayer Perceptron

class MLP:
    def fit(self, X, y, n_epochs=4000, lr=0.01, n_units=10):
        self.w = np.random.rand(X.shape[1], n_units)
        self.v = np.random.rand(n_units, y.shape[1])
        for _ in range(n_epochs):
            h_out = sigmoid(X.dot(self.w))
            out = softmax(h_out.dot(self.v))
            self.v -= lr * h_out.T.dot(out - y)
            self.w -= lr * X.T.dot((out - y).dot(self.v.T) * (h_out * (1 - h_out)))
    def predict(self, X):
        return softmax(sigmoid(X.dot(self.w)).dot(self.v))
$ python napkin_ml/examples/mlp.py

Figure: Classification of the Iris dataset with a Multilayer Perceptron
with one hidden layer.

Principal Component Analysis

class PCA:
    def transform(self, X, dim):
        _, S, V = np.linalg.svd(X - X.mean(0), full_matrices=True)
        idx = S.argsort()[::-1][:dim]
        return X.dot(V[idx].T)
$ python napkin_ml/examples/pca.py

Figure: Dimensionality reduction with Principal Component Analysis.

More Repositories

1

ML-From-Scratch

Machine Learning From Scratch. Bare bones NumPy implementations of machine learning models and algorithms with a focus on accessibility. Aims to cover everything from linear regression to deep learning.
Python
22,643
star
2

PyTorch-GAN

PyTorch implementations of Generative Adversarial Networks.
Python
15,079
star
3

Keras-GAN

Keras implementations of Generative Adversarial Networks.
Python
9,062
star
4

PyTorch-YOLOv3

Minimal PyTorch implementation of YOLOv3
Python
7,170
star
5

Fast-Neural-Style-Transfer

Fast Neural Style Transfer in Pytorch
Python
333
star
6

Action-Recognition

Exploration of different solutions to action recognition in video, using neural networks implemented in PyTorch.
Python
166
star
7

PyTorch-Deep-Dream

Minimal PyTorch implementation of Deep Dream
Python
142
star
8

eriklindernoren.se

Personal website
CSS
19
star
9

HomeAssistant

Alfred - Domestic butler - IoT Experiment
JavaScript
15
star
10

SemSegGUI

C++
11
star
11

GoogleTTS

Lightweight Python wrapper of the Google Text-To-Speech API
Shell
6
star
12

TBMI26-Neural-Networks

Repository for the lab series in TBMI26 Neural Networks and Learning Systems. Matlab implementations of the Perceptron, MLP, Adaboost, kNN and a Q-learning example
MATLAB
5
star
13

PFTA-Final-Assignment

Final assignment of the course 'Python for Text Analysis'
Jupyter Notebook
4
star
14

TDDC17-Artificial-Intelligence

Lab assignments, exam questions and answers. https://www.ida.liu.se/~TDDC17/info/labs.en.shtml
JavaScript
3
star
15

Sonus-JFugueUI

Application that enables composing of music using the Java library JFugue.
Java
2
star