【python 对多项式朴素贝叶斯做十折交叉验证】

对多项式朴素贝叶斯MNB做十折交叉验证,求平均准确率。

加载数据集data:

import pandas as pd
import numpy as np
data = read_sparse_arrf("fbis.wc.arff")

取数据集与类标签:

x=data.iloc[:,0:-1]
y=data.iloc[:,-1]
df = np.array(x)
label = np.array(y)

使用MNB模型训练,做十折交叉:

import time
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from sklearn.naive_bayes import MultinomialNB   #使用多项式朴素贝叶斯模型

start_time = time.time()
kf = KFold(n_splits=10, shuffle=True)  #n_splits=多少,就是做几折交叉
scores = []  # 存储模型评估结果

for train_index, test_index in kf.split(df):
    x_train, x_test = df[train_index], df[test_index]
    y_train, y_test = label[train_index], label[test_index]
    model = MultinomialNB()  
    model.fit(x_train,y_train)  # 训练模型
    score = model.score(x_test, y_test)  # 测试模型
    scores.append(score)    
mean_score = np.mean(scores)  # 计算平均准确率
print( mean_score,np.max(scores),np.min(scores))   #输出每次的准确率及平均准确率
end_time = time.time()  # 程序结束时间
run_time = end_time - start_time  # 程序的运行时间,单位为秒
print("Time:", run_time)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值