具体代码
时间: 2023-07-02 18:23:45 浏览: 39
好的,下面是一个简单的 C 语言朴素贝叶斯分类器代码,用于判断西瓜的好坏:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_SAMPLES 100 // 最大样本数量
#define MAX_FEATURES 10 // 最大特征数量
// 样本结构体
typedef struct {
int features[MAX_FEATURES]; // 样本特征值
int label; // 样本标签(好瓜为1,坏瓜为0)
} Sample;
Sample samples[MAX_SAMPLES]; // 样本数组
int sample_count = 0; // 样本数量
int feature_count = 0; // 特征数量
int good_count = 0; // 好瓜数量
int bad_count = 0; // 坏瓜数量
double good_prob[MAX_FEATURES][2]; // 好瓜条件概率数组
double bad_prob[MAX_FEATURES][2]; // 坏瓜条件概率数组
// 添加样本
void addSample(int features[], int label) {
Sample sample;
memcpy(sample.features, features, sizeof(int) * feature_count);
sample.label = label;
samples[sample_count++] = sample;
if (label == 1) {
good_count++;
} else {
bad_count++;
}
}
// 计算条件概率
double calcProb(int feature, int value, int label) {
int count = 0;
int label_count = 0;
for (int i = 0; i < sample_count; i++) {
if (samples[i].features[feature] == value) {
count++;
if (samples[i].label == label) {
label_count++;
}
}
}
if (label == 1) {
return (double)(label_count + 1) / (good_count + 2);
} else {
return (double)(label_count + 1) / (bad_count + 2);
}
}
// 计算所有条件概率
void calcAllProb() {
for (int i = 0; i < feature_count; i++) {
for (int j = 0; j < 2; j++) {
good_prob[i][j] = calcProb(i, j, 1);
bad_prob[i][j] = calcProb(i, j, 0);
}
}
}
// 预测样本标签
int predict(int features[]) {
double good_p = (double)good_count / sample_count;
double bad_p = (double)bad_count / sample_count;
for (int i = 0; i < feature_count; i++) {
if (features[i] == 1) {
good_p *= good_prob[i][1];
bad_p *= bad_prob[i][1];
} else {
good_p *= good_prob[i][0];
bad_p *= bad_prob[i][0];
}
}
if (good_p > bad_p) {
return 1;
} else {
return 0;
}
}
int main() {
// 添加样本数据
int features1[] = {1, 1, 0, 0, 0};
addSample(features1, 0);
int features2[] = {1, 1, 0, 1, 0};
addSample(features2, 0);
int features3[] = {0, 1, 0, 0, 0};
addSample(features3, 1);
int features4[] = {0, 0, 1, 0, 1};
addSample(features4, 1);
int features5[] = {1, 1, 0, 1, 1};
addSample(features5, 0);
// 计算所有条件概率
feature_count = 5;
calcAllProb();
// 预测新样本
int features6[] = {0, 1, 0, 1, 1};
int label = predict(features6);
printf("新样本的标签为:%d\n", label);
return 0;
}
```
以上代码中,我们使用了一个简单的样本数据集,包含了5个样本,每个样本有5个特征值。通过添加样本数据并计算条件概率,我们构建了一个朴素贝叶斯分类器,并预测了一个新的样本的标签。在实际应用中,我们需要收集更多的数据,并进行更加复杂的特征处理和模型优化,以提高分类器的准确率和泛化能力。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)