根据你的步骤生成相应的代码
时间: 2023-10-13 08:04:25 浏览: 30
好的,以下是Python代码实现朴素贝叶斯决策的实验:
```python
import numpy as np
# 载入数据集
data = np.array([
['青绿', '蜷缩', '浊响', '清晰', '凹陷', '硬滑', '是'],
['乌黑', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑', '是'],
['乌黑', '蜷缩', '浊响', '清晰', '凹陷', '硬滑', '是'],
['青绿', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑', '是'],
['浅白', '蜷缩', '浊响', '清晰', '凹陷', '硬滑', '是'],
['青绿', '稍蜷', '浊响', '清晰', '稍凹', '软粘', '是'],
['乌黑', '稍蜷', '浊响', '稍糊', '稍凹', '软粘', '是'],
['乌黑', '稍蜷', '浊响', '清晰', '稍凹', '硬滑', '是'],
['乌黑', '稍蜷', '沉闷', '稍糊', '稍凹', '硬滑', '否'],
['青绿', '硬挺', '清脆', '清晰', '平坦', '软粘', '否'],
['浅白', '硬挺', '清脆', '模糊', '平坦', '硬滑', '否'],
['浅白', '蜷缩', '浊响', '模糊', '平坦', '软粘', '否'],
['青绿', '稍蜷', '浊响', '稍糊', '凹陷', '硬滑', '否'],
['浅白', '稍蜷', '沉闷', '稍糊', '凹陷', '硬滑', '否'],
['乌黑', '稍蜷', '浊响', '清晰', '稍凹', '软粘', '否'],
['浅白', '蜷缩', '浊响', '模糊', '平坦', '硬滑', '否'],
['青绿', '蜷缩', '沉闷', '稍糊', '稍凹', '硬滑', '否']
])
# 分离训练集和测试集
train_data = data[:10, :]
test_data = data[10:, :]
# 计算先验概率和条件概率
prior_prob = {'是': 0, '否': 0}
cond_prob = {}
for i in range(train_data.shape[1] - 1):
cond_prob[i] = {}
for sample in train_data:
label = sample[-1]
prior_prob[label] += 1
for i in range(len(sample) - 1):
if sample[i] not in cond_prob[i]:
cond_prob[i][sample[i]] = {'是': 0, '否': 0}
cond_prob[i][sample[i]][label] += 1
for label in prior_prob:
prior_prob[label] /= len(train_data)
for i in cond_prob:
for attr in cond_prob[i]:
for label in prior_prob:
cond_prob[i][attr][label] = cond_prob[i][attr][label] / prior_prob[label]
# 对测试集进行分类
correct_count = 0
for sample in test_data:
true_label = sample[-1]
pred_prob = {'是': 1, '否': 1}
for i in range(len(sample) - 1):
pred_prob['是'] *= cond_prob[i][sample[i]]['是']
pred_prob['否'] *= cond_prob[i][sample[i]]['否']
pred_label = max(pred_prob, key=pred_prob.get)
if true_label == pred_label:
correct_count += 1
# 输出分类准确率
accuracy = correct_count / len(test_data)
print('分类准确率为:', accuracy)
# 选择不同的属性组合进行实验
attr_indices = [[0, 1], [0, 1, 2, 3, 4, 5]]
for indices in attr_indices:
train_data_subset = train_data[:, indices]
test_data_subset = test_data[:, indices]
# 计算先验概率和条件概率
prior_prob = {'是': 0, '否': 0}
cond_prob = {}
for i in range(len(indices)):
cond_prob[i] = {}
for sample in train_data_subset:
label = sample[-1]
prior_prob[label] += 1
for i in range(len(indices)):
if sample[i] not in cond_prob[i]:
cond_prob[i][sample[i]] = {'是': 0, '否': 0}
cond_prob[i][sample[i]][label] += 1
for label in prior_prob:
prior_prob[label] /= len(train_data_subset)
for i in cond_prob:
for attr in cond_prob[i]:
for label in prior_prob:
cond_prob[i][attr][label] = cond_prob[i][attr][label] / prior_prob[label]
# 对测试集进行分类
correct_count = 0
for sample in test_data_subset:
true_label = sample[-1]
pred_prob = {'是': 1, '否': 1}
for i in range(len(indices)):
pred_prob['是'] *= cond_prob[i][sample[i]]['是']
pred_prob['否'] *= cond_prob[i][sample[i]]['否']
pred_label = max(pred_prob, key=pred_prob.get)
if true_label == pred_label:
correct_count += 1
# 输出分类准确率
accuracy = correct_count / len(test_data_subset)
print('属性组合为', indices, '时的分类准确率为:', accuracy)
```
代码中首先将数据集分为训练集和测试集,然后根据训练集计算先验概率和条件概率。接着,对测试集进行分类,并统计分类准确率。最后,选择不同的属性组合进行实验,重复上述过程,输出不同属性组合下的分类准确率。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)