一、Python
import numpy as np
import matplotlib.pyplot as plt
# 定义预测函数
def predict(x, w, b):
# 计算特征向量和权重向量的点积
dot_product = np.dot(x, w)
# 计算预测值
y_pred = dot_product + b
# 如果预测值大于0,则返回1,否则返回-1
return 1 if y_pred > 0 else -1
# 定义训练函数
def train(X, y, w, b, learning_rate, epochs):
# 获取数据集大小(n)和特征维度(m)
n, m = X.shape
for i in range(epochs): # 迭代训练
for j in range(n): # 遍历数据集
y_pred = predict(X[j], w, b) # 预测结果
if y_pred != y[j]: # 判断是否需要更新权重和偏置
# 更新权重和偏置
w += learning_rate * y[j] * X[j]
b += learning_rate * y[j]
# 返回更新后的权重和偏置
return w, b
# 定义生成数据函数
def generate_data(num_examples):
# 初始化特征矩阵和标签向量
X = np.zeros((num_examples, 2))
y = np.zeros(num_examples)
for i in range(num_examples): # 生成数据
# 从标准正态分布中生成两个随机数作为特征
feature = np.random.normal(0.0, 1.0, size=2)
X[i] = feature
# 计算特征向量和权重向量的点积
dot_product = feature[0] * 2 + feature[1] * 3
# 如果点积大于0,则标签为1,否则为-1
label = 1 if dot_product > 0 else -1
y[i] = label # 添加标签
# 返回特征矩阵和标签向量
return X, y
# 生成数据
num_examples = 1000
X, y = generate_data(num_examples)
# 初始化权重和偏置
w = np.zeros(2)
b = 0
learning_rate = 0.1
epochs = 1000
# 训练模型
w, b = train(X, y, w, b, learning_rate, epochs)
# 输出训练后的权重和偏置
print("w:", w)
print("b:", b)
# 可视化数据
# 绘制散点图
plt.scatter(X[:,0], X[:,1], c=y)
plt.title("Generated data")
plt.xlabel("x1")
plt.ylabel("x2")
# 绘制决策边界
# 获取x1和x2的最小值和最大值
x1_min, x1_max = X[:,0].min() - 1, X[:,0].max() + 1
x2_min, x2_max = X[:,1].min() - 1, X[:,1].max() + 1
# 生成网格点
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, 0.1),
np.arange(x2_min, x2_max, 0.1))
# 预测网格点的标签
Z = np.array([predict(np.array([x1, x2]), w, b) for x1, x2 in np.c_[xx1.ravel(), xx2.ravel()]])
Z = Z.reshape(xx1.shape)
# 绘制等高线图
plt.contourf(xx1, xx2, Z, alpha=0.4)
plt.show()
二、C++
#include <iostream>
#include <vector>
#include "math.h"
#include <random>
using namespace std;
// 定义预测函数
int predict(vector<double>& x, vector<double>& w, double b) {
double dot_product = 0;
for (int i = 0; i < x.size(); i++) {
dot_product += x[i] * w[i];
}
double y_pred = dot_product + b;
return y_pred > 0 ? 1 : -1;
}
// 定义训练函数
void train(vector<vector<double>>& X, vector<int>& y, vector<double>& w, double& b, double learning_rate, int epochs) {
int n = X.size(); // 获取数据集大小
int m = X[0].size(); // 获取特征维度
for (int i = 0; i < epochs; i++) { // 迭代训练
for (int j = 0; j < n; j++) { // 遍历数据集
int y_pred = predict(X[j], w, b); // 预测结果
if (y_pred != y[j]) { // 判断是否需要更新权重和偏置
for (int k = 0; k < m; k++) {
w[k] += learning_rate * y[j] * X[j][k];
}
b += learning_rate * y[j];
}
}
}
}
// 定义生成数据函数
void generate_data(int num_examples, vector<vector<double>>& X, vector<int>& y) {
default_random_engine generator; // 定义随机数生成器
normal_distribution<double> distribution(0.0, 1.0); // 定义正态分布
for (int i = 0; i < num_examples; i++) { // 生成数据
vector<double> feature;
feature.push_back(distribution(generator));
feature.push_back(distribution(generator));
X.push_back(feature);
double dot_product = feature[0] * 2 + feature[1] * 3; // 计算点积
int label = dot_product > 0 ? 1 : -1; // 根据点积确定标签
y.push_back(label); // 添加标签
}
}
int main() {
// 生成数据
vector<vector<double>> X;
vector<int> y;
int num_examples = 1000;
generate_data(num_examples, X, y);
// 初始化权重和偏置
vector<double> w = {0, 0};
double b = 0;
double learning_rate = 0.1;
int epochs = 1000;
// 训练模型
train(X, y, w, b, learning_rate, epochs);
// 输出训练结果
cout << "w: " << w[0] << ", " << w[1] << ", b: " << b << std::endl;
// 随机生成测试数据
default_random_engine generator;
normal_distribution<double> distribution(0.0, 1.0);
vector<double> x_test = {distribution(generator), distribution(generator)};
// 预测结果
int y_pred = predict(x_test, w, b);
// 输出预测结果
cout << "Input values: " << x_test[0] << ", " << x_test[1] << std::endl;
cout << "Predicted label: " << y_pred << std::endl;
return 0;
}