朴素贝叶斯算法c语言代码
时间: 2024-03-28 15:32:46 浏览: 75
朴素贝叶斯算法C语言实现
朴素贝叶斯算法是一种常用的分类算法,它基于贝叶斯定理和特征条件独立假设。下面是一个简单的朴素贝叶斯算法的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_CLASSES 2
#define NUM_FEATURES 4
// 计算高斯分布的概率密度函数
double gaussian_pdf(double x, double mean, double stddev) {
double exponent = exp(-pow(x - mean, 2) / (2 * pow(stddev, 2)));
return (1 / (sqrt(2 * M_PI) * stddev)) * exponent;
}
// 训练朴素贝叶斯分类器
void train_naive_bayes(double data[][NUM_FEATURES], int labels[], int num_samples, double means[][NUM_FEATURES], double stddevs[][NUM_FEATURES], double class_probs[]) {
int i, j, class;
int class_counts[NUM_CLASSES] = {0};
// 计算每个类别的样本数量
for (i = 0; i < num_samples; i++) {
class_counts[labels[i]]++;
}
// 计算每个特征在每个类别中的均值和标准差
for (i = 0; i < NUM_CLASSES; i++) {
for (j = 0; j < NUM_FEATURES; j++) {
double sum = 0.0;
double sum_sq = 0.0;
int count = 0;
for (class = 0; class < num_samples; class++) {
if (labels[class] == i) {
sum += data[class][j];
sum_sq += pow(data[class][j], 2);
count++;
}
}
means[i][j] = sum / count;
stddevs[i][j] = sqrt((sum_sq / count) - pow(means[i][j], 2));
}
}
// 计算每个类别的概率
for (i = 0; i < NUM_CLASSES; i++) {
class_probs[i] = (double)class_counts[i] / num_samples;
}
}
// 使用训练好的模型进行分类
int predict_naive_bayes(double sample[], double means[][NUM_FEATURES], double stddevs[][NUM_FEATURES], double class_probs[]) {
int i, j;
double likelihoods[NUM_CLASSES]; double max_likelihood = 0.0;
int predicted_class = -1;
// 计算每个类别的似然概率
for (i = 0; i < NUM_CLASSES; i++) {
likelihoods[i] = 1.0;
for (j = 0; j < NUM_FEATURES; j++) {
likelihoods[i] *= gaussian_pdf(sample[j], means[i][j], stddevs[i][j]);
}
likelihoods[i] *= class_probs[i];
if (likelihoods[i] > max_likelihood) {
max_likelihood = likelihoods[i];
predicted_class = i;
}
}
return predicted_class;
}
int main() {
// 样本数据
double data[][NUM_FEATURES] = {
{5.1, 3.5, 1.4, 0.2},
{4.9, 3.0, 1.4, 0.2},
{4.7, 3.2, 1.3, 0.2},
// ...
};
// 样本标签
int labels[] = {0, 0, 0, 1, 1, 1, /* ... */};
// 均值和标准差
double means[NUM_CLASSES][NUM_FEATURES];
double stddevs[NUM_CLASSES][NUM_FEATURES];
// 类别概率
double class_probs[NUM_CLASSES];
// 训练模型
train_naive_bayes(data, labels, sizeof(labels) / sizeof(labels[0]), means, stddevs, class_probs);
// 预测样本
double sample[] = {5.8, 2.6, 4.0, 1.2};
int predicted_class = predict_naive_bayes(sample, means, stddevs, class_probs);
printf("Predicted class: %d\n", predicted_class);
return 0;
}
```
这段代码实现了一个简单的朴素贝叶斯分类器,包括训练和预测两个函数。训练函数`train_naive_bayes`用于计算每个类别的均值、标准差和概率,预测函数`predict_naive_bayes`用于根据输入样本进行分类预测。
阅读全文