用python,自编函数实现朴素贝叶斯分类算法。并对sklearn数据库中鸢尾花数据进行分类。输出分类结果,说明分类效果。
时间: 2024-04-08 17:33:18 浏览: 179
以下是一个简单的朴素贝叶斯分类算法的自编函数,并对sklearn数据库中的鸢尾花数据进行分类:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
def naive_bayes_classification(X_train, y_train, X_test):
# 计算每个类别的先验概率
classes = np.unique(y_train)
prior_probs = {}
for c in classes:
prior_probs[c] = np.sum(y_train == c) / len(y_train)
# 计算每个特征的条件概率
feature_probs = {}
for feature in range(X_train.shape[1]):
feature_probs[feature] = {}
unique_values = np.unique(X_train[:, feature])
for value in unique_values:
feature_probs[feature][value] = {}
for c in classes:
numerator = np.sum((X_train[:, feature] == value) & (y_train == c))
denominator = np.sum(y_train == c)
feature_probs[feature][value][c] = numerator / denominator
# 进行分类
y_pred = []
for i in range(X_test.shape[0]):
class_probs = {}
for c in classes:
class_prob = prior_probs[c]
for feature in range(X_test.shape[1]):
value = X_test[i, feature]
if value in feature_probs[feature]:
class_prob *= feature_probs[feature][value][c]
else:
class_prob *= 0.0001 # 平滑处理,避免概率为0
class_probs[c] = class_prob
pred_class = max(class_probs, key=class_probs.get)
y_pred.append(pred_class)
return y_pred
# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 进行朴素贝叶斯分类
y_pred = naive_bayes_classification(X_train, y_train, X_test)
# 输出分类结果和分类效果
print("分类结果:", y_pred)
print("分类准确率:", accuracy_score(y_test, y_pred))
```
在这个示例中,我们首先使用`sklearn.datasets.load_iris`加载鸢尾花数据集。然后,我们使用`sklearn.model_selection.train_test_split`将数据集划分为训练集和测试集。接下来,我们调用自编函数`naive_bayes_classification`进行朴素贝叶斯分类。最后,使用`sklearn.metrics.accuracy_score`计算分类准确率,并输出分类结果和分类效果。
请注意,这只是一个简单的朴素贝叶斯分类算法实现,可能不是最优的方法。在实际应用中,可以考虑使用更复杂的模型或库来进行分类任务。
阅读全文