# NapkinML
## About
Pocket-sized implementations of machine learning models, most of which will fit in a tweet.
## Table of Contents
- [NapkinML](#napkinml)
* [About](#about)
* [Table of Contents](#table-of-contents)
* [Installation](#installation)
* [Implementations](#implementations)
+ [K-Means](#k-means)
+ [K-Nearest Neighbors](#k-nearest-neighbors)
+ [Linear Regression](#linear-regression)
+ [Linear Discriminant Analysis](#linear-discriminant-analysis)
+ [Logistic Regression](#logistic-regression)
+ [Multilayer Perceptron](#multilayer-perceptron)
+ [Principal Component Analysis](#principal-component-analysis)
## Installation
$ git clone https://github.com/eriklindernoren/NapkinML
$ cd NapkinML
$ sudo python setup.py install
## Implementations
### K-Means
```python
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
```
<p align="center">
<img src="http://eriklindernoren.se/images/napkin_kmeans.png" width="640">
</p>
<p align="center">
Figure: K-Means clustering of the Iris dataset.
</p>
### K-Nearest Neighbors
```python
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
```
<p align="center">
<img src="http://eriklindernoren.se/images/napkin_knn.png" width="640">
</p>
<p align="center">
Figure: Classification of the Iris dataset with K-Nearest Neighbors.
</p>
### Linear Regression
```python
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
```
<p align="center">
<img src="http://eriklindernoren.se/images/napkin_linreg.png" width="640">
</p>
<p align="center">
Figure: Linear Regression.
</p>
### Linear Discriminant Analysis
```python
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
```python
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
```
<p align="center">
<img src="http://eriklindernoren.se/images/napkin_logreg.png" width="640">
</p>
<p align="center">
Figure: Classification with Logistic Regression.
</p>
### Multilayer Perceptron
```python
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
```
<p align="center">
<img src="http://eriklindernoren.se/images/napkin_mlp1.png" width="640">
</p>
<p align="center">
Figure: Classification of the Iris dataset with a Multilayer Perceptron <br> with one hidden layer.
</p>
### Principal Component Analysis
```python
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
```
<p align="center">
<img src="http://eriklindernoren.se/images/napkin_pca.png" width="640">
</p>
<p align="center">
Figure: Dimensionality reduction with Principal Component Analysis.
</p>
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
机器学习(ML)虽然不能精确模拟自然界中的系统,但却能够学习系统模型并预测系统行为。在过去几年,经典的 ML 模型解决了科学领域诸多难题,在癌症检测、地震余震预测、极端天气预测以及系外行星探测等方面发挥了巨大作用。 近年来,随着量子计算技术的发展,在量子机器学习模型上的新发现将对世界级重大问题产生深远影响,从而带来医学、材料、传感和通信领域的突破。但是,迄今为止我们遇到的瓶颈是,缺乏研究工具来发现有用的、可以处理量子数据并能在计算机上使用的量子机器学习模型。 基于此,谷歌最神秘的部门 Google X 与滑铁卢大学和大众汽车公司等合作,联合发布 TensorFlow Quantum(TFQ),这是一个用于快速建立量子 ML 模型原型的开源库。TFQ 为量子计算和机器学习研究社区的结合提供了必要工具,从而控制 / 建模自然或人工量子系统,比如内含大约 50~100 量子比特的噪声中级量子处理器(NISQ)。 在底层,TFQ 集成了 NISQ 算法的开源框架 Cirq(Cirq 是 Google 专为 NISQ 算法打造的框架, 允许开发者为特定的量子处理器编写量子算法)
资源推荐
资源详情
资源评论
收起资源包目录
NapkinML 是 NumPy 中机器学习模型的袖珍实现.rar (21个子文件)
NapkinML 是 NumPy 中机器学习模型的袖珍实现
新建文本文档.txt 19B
NapkinML-master
setup.py 1014B
LICENSE 1KB
napkin_ml
__init__.py 22B
data
digits.csv 550KB
temp_linkoping.txt 6KB
iris.csv 4KB
examples
__init__.py 0B
lda.py 749B
pca.py 1KB
kmeans.py 423B
linear_regression.py 1KB
mlp.py 976B
knn.py 680B
logistic_regression.py 831B
models.py 2KB
utils
__init__.py 20B
misc.py 4KB
requirements.txt 42B
.gitignore 76B
README.md 4KB
共 21 条
- 1
资源评论
野生的狒狒
- 粉丝: 1634
- 资源: 1667
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功