C++/Python机器学习—感知机(二分类)

 一、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;
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南叔先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值