import numpy as np
import random
import matplotlib.pyplot as plt # 需要安装但用户允许的可视化库
# 生成线性可分数据
def generate_data(n_samples=100):
np.random.seed(42)
X1 = np.random.randn(n_samples//2, 2) + np.array([2, 2]) # 类别1
X2 = np.random.randn(n_samples//2, 2) + np.array([-2, -2]) # 类别-1
X = np.vstack((X1, X2))
y = np.array([1]*(n_samples//2) + [-1]*(n_samples//2))
indices = np.random.permutation(len(X)) # 打乱索引保证数据一致性[^1]
return X[indices], y[indices]
# 划分训练集和测试集
def train_test_split(X, y, test_ratio=0.2):
split_idx = int(len(X)*(1-test_ratio))
return X[:split_idx], X[split_idx:], y[:split_idx], y[split_idx:]
# 感知机实现
class Perceptron:
def __init__(self, n_features):
self.weights = np.random.uniform(-1, 1, n_features) # numpy向量化初始化
self.bias = 0.0
def predict(self, X):
return np.where(np.dot(X, self.weights) + self.bias >= 0, 1, -1)
def train(self, X_train, y_train, epochs=100, lr=0.1):
for _ in range(epochs):
for i in range(len(X_train)):
x, y_true = X_train[i], y_train[i]
y_pred = self.predict(x.reshape(1, -1))[0]
if y_pred != y_true:
update = lr * (y_true - y_pred)
self.weights += update * x # numpy向量化更新
self.bias += update
# 计算准确率
def calculate_accuracy(model, X, y):
predictions = model.predict(X)
return np.mean(predictions == y) * 100 # 直接使用numpy计算准确率[^3]
# 绘制决策边界
def plot_decision_boundary(model, X, y):
plt.scatter(X[:,0], X[:,1], c=y, cmap='bwr', edgecolors='k')
x_min, x_max = X[:,0].min()-1, X[:,0].max()+1
w1, w2 = model.weights
b = model.bias
x_values = np.linspace(x_min, x_max, 100)
y_values = (-w1 * x_values - b) / w2 # 决策边界方程: w1x1 + w2x2 + b = 0
plt.plot(x_values, y_values, 'g--', lw=2)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Decision Boundary')
plt.show()
# 主流程
if __name__ == "__main__":
X, y = generate_data(200)
X_train, X_test, y_train, y_test = train_test_split(X, y)
model = Perceptron(n_features=2)
model.train(X_train, y_train, epochs=50)
train_acc = calculate_accuracy(model, X_train, y_train)
test_acc = calculate_accuracy(model, X_test, y_test)
print(f"训练准确率: {train_acc:.1f}%")
print(f"测试准确率: {test_acc:.1f}%")
print(f"学习到的权重: {model.weights}")
print(f"偏置项: {model.bias}")
plot_decision_boundary(model, X_train, y_train)不适用matplotlib库请修改
最新发布