1、内容
(1)学习和理解感知器的基本原理。
(2)利用sklearn和NumPy两种方法设计实现感知器。
(3)将设计的感知器对一定数据进行分析,并将结果可视化,验证算法的正确性。
2、实验数据
samples=np.array([[3,-3],[4,-3],[1,1],[1,2]])
labels=np.array([-1,-1,1,1])
分析:
①有四个向量
②label:真实标签,如1为T,-1为F,写成数值同样是为了方便计算.
3、预备知识
Perceptron参数解释
用于创建感知机模型时传递的参数。
max_iter | int整数,默认=1000 | 最大迭代次数,哪怕损失函数依旧大于0 |
eta0 | 取值双精度浮点型double,默认=1 | 学习率,决定梯度下降时每次参数变化的幅度 |
#!/usr/bin/env python
# -*- coding=utf-8 -*-
# 实验3 感知器的设计与实现
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Perceptron
# 创建数据
samples = np.array([[3, -3], [4, -3], [1, 1], [1, 2]])
labels = np.array([-1, -1, 1, 1])
# 创建感知器模型并训练
perceptron = Perceptron()
perceptron.fit(samples, labels)
# 预测分类结果
predictions_sklearn= perceptron.predict(samples)
# 以下为用numpy手动实现感知器
# 创建感知器模型并训练
def perceptron_train(X, y, learning_rate=0.1, num_epochs=100):
num_samples, num_features = X.shape
weights = np.zeros(num_features)
bias = 0
for _ in range(num_epochs):
for i in range(num_samples):
prediction = np.dot(X[i], weights) + bias
if y[i] * prediction <= 0:
weights += learning_rate * y[i] * X[i]
bias += learning_rate * y[i]
return weights, bias
weights, bias = perceptron_train(samples, labels)
# 预测分类结果
def perceptron_predict(X, weights, bias):
return np.sign(np.dot(X, weights) + bias)
predictions_numpy = perceptron_predict(samples, weights, bias)
# 画出分类界限
def plot_decision_boundary(X, y, weights, bias):
plt.scatter(X[:, 0], X[:, 1], c=y)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01),
np.arange(y_min, y_max, 0.01))
Z = perceptron_predict(np.c_[xx.ravel(), yy.ravel()], weights, bias)
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Perceptron Classification_numpy')
plt.legend()
# 可视化预测分类和实际分类结果
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.scatter(samples[:, 0], samples[:, 1], c=labels, cmap='viridis', marker='o', label='Actual')
plt.scatter(samples[:, 0], samples[:, 1], c=predictions_numpy, cmap='viridis', marker='x', label='Predicted')
plot_decision_boundary(samples, labels, weights, bias)
plt.legend()
plt.subplot(1, 2, 2)
x_min, x_max = samples[:, 0].min() - 1, samples[:, 0].max() + 1
y_min, y_max = samples[:, 1].min() - 1, samples[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01), np.arange(y_min, y_max, 0.01))
Z = perceptron.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap='viridis', alpha=0.2)
plt.scatter(samples[:, 0], samples[:, 1], c=labels, cmap='viridis', marker='o', label='Actual')
plt.scatter(samples[:, 0], samples[:, 1], c=predictions_sklearn, cmap='viridis', marker='x', label='Predicted')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Perceptron Classification_sklearn')
plt.legend()
plt.show()