4 Star 0 Fork 1

Gitee 极速下载 / NapkinML

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/eriklindernoren/NapkinML
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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.

MIT License Copyright (c) 2017 Erik Linder-Norén Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

NapkinML 是 NumPy 中机器学习模型的袖珍实现 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/mirrors/NapkinML.git
git@gitee.com:mirrors/NapkinML.git
mirrors
NapkinML
NapkinML
master

搜索帮助