Skip to content

Commit a4f33b0

Browse files
add perceptron
1 parent e256177 commit a4f33b0

7 files changed

Lines changed: 179 additions & 797 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.pyc
2+
*.xml
23
.idea/
34
.DS_Store

.idea/workspace.xml

Lines changed: 0 additions & 790 deletions
This file was deleted.

kmeans/kmeans.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,17 @@
232232
" print(\"dataset=%s model=%s takeTime=%s\"%(dataName, modelName, round(takeTime, 5)))\n",
233233
" plt.scatter(X_tsne[:, 0],X_tsne[:, 1], c=labels)\n",
234234
" plt.show()\n",
235-
" print(\"-\" * 100)\n",
236-
" \n",
237-
" "
235+
" print(\"-\" * 100)"
238236
]
239237
},
240238
{
241239
"cell_type": "code",
242240
"execution_count": null,
243241
"metadata": {},
244242
"outputs": [],
245-
"source": []
243+
"source": [
244+
""
245+
]
246246
}
247247
],
248248
"metadata": {
@@ -254,7 +254,7 @@
254254
"language_info": {
255255
"codemirror_mode": {
256256
"name": "ipython",
257-
"version": 3
257+
"version": 3.0
258258
},
259259
"file_extension": ".py",
260260
"mimetype": "text/x-python",
@@ -265,5 +265,5 @@
265265
}
266266
},
267267
"nbformat": 4,
268-
"nbformat_minor": 2
269-
}
268+
"nbformat_minor": 0
269+
}

perceptron/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 实现感知机的基本算法和对偶算法
2+
3+
# 结果比较
4+
结果在perceptron.ipynb中展示
5+
6+
# 相关博客
7+
#### [1. 感知机原理(Perceptron)](https://www.cnblogs.com/huangyc/p/9706575.html)

perceptron/dual_perceptron.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import numpy as np
2+
from sklearn.datasets import load_iris
3+
from sklearn.model_selection import train_test_split
4+
import matplotlib.pyplot as plt
5+
from matplotlib.colors import ListedColormap
6+
7+
a, b, G_matrix = 0, 0, 0
8+
9+
# 计算Gram Matrix
10+
def calculate_g_matrix(data):
11+
global G_matrix
12+
G_matrix = np.zeros((data.shape[0], data.shape[0]))
13+
# 填充Gram Matrix
14+
for i in range(data.shape[0]):
15+
for j in range(data.shape[0]):
16+
G_matrix[i][j] = np.sum(data[i, 0:-1] * data[j, 0:-1])
17+
18+
# 迭代的判定条件
19+
def judge(data, y, index):
20+
global a, b
21+
tmp = 0
22+
for m in range(data.shape[0]):
23+
tmp += a[m] * data[m, -1] * G_matrix[index][m]
24+
25+
return (tmp + b) * y
26+
27+
28+
def dual_perceptron(data):
29+
"""
30+
对偶形态的感知机
31+
由于对偶形式中训练实例仅以内积的形式出现
32+
因此,若事先求出Gram Matrix,能大大减少计算量
33+
:param data:训练数据集;ndarray object
34+
:return:w,b
35+
"""
36+
global a, b, G_matrix
37+
38+
# 计算Gram_Matrix
39+
calculate_g_matrix(data)
40+
41+
# 读取数据集中含有的样本数
42+
num_samples = data.shape[0]
43+
# 读取数据集中特征向量的个数
44+
num_features = data.shape[1] - 1
45+
# 初始化a,b
46+
a, b = [0] * num_samples, 0
47+
# 初始化weight
48+
w = np.zeros((1, num_features))
49+
50+
i = 0
51+
while i < num_samples:
52+
if judge(data, data[i, -1], i) <= 0:
53+
a[i] += 1
54+
b += data[i, -1]
55+
i = 0
56+
else:
57+
i += 1
58+
59+
for j in range(num_samples):
60+
w += a[j] * data[j, 0:-1] * data[j, -1]
61+
62+
return w, b

perceptron/perceptron.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import numpy as np
2+
from sklearn.datasets import load_iris
3+
from sklearn.model_selection import train_test_split
4+
from utils.plot import plot_decision_regions
5+
6+
class Perceptron(object):
7+
"""
8+
原始形态感知机
9+
"""
10+
11+
def __init__(self, eta=0.01, n_iter=10):
12+
self.eta = eta #学习率
13+
self.n_iter = n_iter
14+
15+
def fit(self, X, y):
16+
"""
17+
拟合函数,使用训练集来拟合模型
18+
:param X:training sets
19+
:param y:training labels
20+
:return:self
21+
"""
22+
# X's each col represent a feature
23+
# initialization wb(weight plus bias)
24+
self.wb = np.zeros(1 + X.shape[1])
25+
# the main process of fitting
26+
self.errors_ = [] # store the errors for each iteration
27+
for _ in range(self.n_iter):
28+
errors = 0
29+
for xi, yi in zip(X, y):
30+
update = self.eta * (yi - self.predict(xi))
31+
self.wb[1:] += update * xi
32+
self.wb[0] += update
33+
errors += int(update != 0.0)
34+
self.errors_.append(errors)
35+
36+
return self
37+
38+
def net_input(self, xi):
39+
"""
40+
计算净输入
41+
:param xi:
42+
:return:净输入
43+
"""
44+
return np.dot(xi, self.wb[1:]) + self.wb[0]
45+
46+
def predict(self, xi):
47+
"""
48+
计算预测值
49+
:param xi:
50+
:return:-1 or 1
51+
"""
52+
return np.where(self.net_input(xi) <= 0.0, -1, 1)
53+
54+
55+
def main():
56+
iris = load_iris()
57+
X = iris.data[:100, [0, 2]]
58+
y = iris.target[:100]
59+
y = np.where(y == 1, 1, -1)
60+
X_train, X_test, y_train, y_test = \
61+
train_test_split(X, y, test_size=0.3)
62+
ppn = Perceptron(eta=0.1, n_iter=10)
63+
ppn.fit(X_train, y_train)
64+
plot_decision_regions(ppn,X,y)
65+
66+
67+
if __name__ == "__main__":
68+
main()

utils/plot.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import matplotlib.pyplot as plt
2+
from matplotlib.colors import ListedColormap
3+
import numpy as np
4+
5+
def plot_decision_regions(model, X, y, resolution=0.02):
6+
"""
7+
拟合效果可视化
8+
:param X:training sets
9+
:param y:training labels
10+
:param resolution:分辨率
11+
:return:None
12+
"""
13+
# initialization colors map
14+
colors = ['red', 'blue']
15+
markers = ['o', 'x']
16+
cmap = ListedColormap(colors[:len(np.unique(y))])
17+
18+
# plot the decision regions
19+
x1_max, x1_min = max(X[:, 0]) + 1, min(X[:, 0]) - 1
20+
x2_max, x2_min = max(X[:, 1]) + 1, min(X[:, 1]) - 1
21+
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
22+
np.arange(x2_min, x2_max, resolution))
23+
Z = model.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
24+
Z = Z.reshape(xx1.shape)
25+
plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
26+
plt.xlim(xx1.min(), xx1.max())
27+
plt.ylim(xx2.min(), xx2.max())
28+
29+
# plot class samples
30+
for idx, cl in enumerate(np.unique(y)):
31+
plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
32+
alpha=0.8, c=cmap(idx),
33+
marker=markers[idx], label=cl)
34+
plt.show()

0 commit comments

Comments
 (0)