贝叶斯⑤——搜狗新闻分类实战(jieba + TF-IDF + 贝叶斯)

贝叶斯算法(3)新闻分类任务实战:jieba中文分词、tfidf特征词提取、wordcloud词云展示、LDA主题建模、朴素贝叶斯算法分析预测 1 加载数据 import pandas as pd import jieba # 数据源:http://www.sogou.com/labs/resource/ca.php val_path = '/Users/haiwangluo/Downloads/人工智能全套/自然语言处理(Python版)/第六章:贝叶斯算法-新闻分类任务/贝叶斯Python文本分析/Python文本分析/data/val.txt' df_news = pd.read_table(val_path,names=['categor 阅读详情

贝叶斯机器学习系列:
贝叶斯①——贝叶斯原理篇(联合概率&条件概率&贝叶斯定理&拉普拉斯平滑)
贝叶斯②——贝叶斯3种分类模型及Sklearn使用(高斯&多项式&伯努利)
贝叶斯③——Python实现贝叶斯文本分类(伯努利&多项式模型对比)
贝叶斯④——Sklean新闻分类(TF-IDF)
贝叶斯⑥——银行借贷模型(贝叶斯与决策树对比)

分类过程:
1、导入文件并用jieba分词
2、随机抽取训练和测试样本
3、建立tf-idf词频权重矩阵
4、朴素贝叶斯分类(采用多项式模型)

数据集下载
选取了搜狗新闻下财经,体育、教育等九大类别共90篇文章,本博文将使用朴素贝叶斯进行分类实战(数据集文末有下载方式,方便你实操一遍)

一、读取文件并用jieba分词

import os
import jieba

# 读取所有文件并组成矩阵,特征和类别单独存放
fold_path = r'C:\Users\cindy407\Desktop\Naive-Bayes-Text-Classifier\Database\SogouC\Sample'
folder_list = os.listdir(fold_path)  # 读取文件夹列表
print(folder_list)
artcilt_list = []
class_list = []
for fold in folder_list:  # 读取子文件夹列表 
    new_fold_path = os.path.join(fold_path,fold) # 将路径拼接
    files = os.listdir(new_fold_path) # 再读取子文件夹
    for file in files: # 读取文件
        with open(os.path.join(new_fold_path,file),'r',encoding=
jieba实现基于tf-idf算法的关键词提取(附完整代码) @基于itf-idf算法的关键词提取 提出问题 假设我们现在有一篇文章,需要提取这篇文章的关键词,要怎样才能通过计算机实现呢? TF-IDF算法介绍 一篇文章的关键词基本都是能体现文章的内容,而且几乎是在文章中频繁出现的词,统计文章中各个词出现的次数,出现最多的则是这篇文章的关键词了,那具体是怎么统计呢,这里有一个专业术语叫词频(term frequency),简称TF。计算公式如下: TF(词频) = 某次在文章中出现的次数 / 文章中的总词数 举例子:我正在学习人工智能,并且我一定会成功的。 **通过j 阅读详情

相关推荐

16Python文本数据分析:新闻分类任务 (贝叶斯算法应用实例)

唐宇迪《python数据分析与机器学习实战》学习笔记 16Python文本数据分析:新闻分类任务 (贝叶斯算法应用实例) ** 一、流程分析 ** 数据如下图:content为主体内容, 1.1 停用词:在语料中大量出现又没啥大用,会干扰统计分析,因此去掉。(例如:一个、一切、¥、#等),网上搜索停用词表,匹配过滤。 1.2 Tf-idf:关键词提取 : 这里修改一下:词频=(某个词在文章出现...

qq_45425321的博客 2685

搜狗新闻语料库中文分类:TF-IDF与RoBERTa实战

文本分类是自然语言处理中最常见的基础任务,也是检验分词、特征工程与模型能力的试金石。从词频统计到TF-IDF加权,再到BERT等预训练模型的语义表征,技术演进的本质是让机器更准确地理解文本语境。在中文场景下,大规模语料是训练和评估分类器的关键资源,搜狗新闻语料库因其类别清晰、规模充足,常被用于对比传统机器学习与深度学习的分类效果。借助该语料,开发者既能实践jieba分词、TF-IDF向量化、线性分类器及宏平均F1评估,也能完成RoBERTa微调、显存优化等工程级操作。围绕这一真实数据,可梳理出从数据清洗到模

weixin_34416649的博客 531

搜狗新闻语料库中文文本分类:从TF-IDF到RoBERTa完整实战

中文文本分类是自然语言处理的基础任务,其核心在于将文本转化为机器可理解的向量表示,并通过分类模型实现自动归类。传统方法依赖TF-IDF特征提取与线性分类器,而预训练语言模型如RoBERTa通过深层语义建模显著提升了分类精度。在工程实践中,数据清洗、编码统一、类别不均衡处理等环节直接影响模型效果。搜狗新闻语料库作为中文文本分类的经典数据集,覆盖体育、财经、科技等多个领域,适合从传统机器学习到预训练模型的完整对比实验。本文以该语料库为依托,系统梳理从TF-IDF到RoBERTa的落地路线,涵盖数据预处理、模型微

weixin_34203832的博客 379

搜狗新闻语料库中文文本分类全流程:从TF-IDF基线到BERT微调

文本分类是自然语言处理中最经典的任务之一,核心目标是将非结构化文本自动映射到预定义类别,广泛应用于舆情分析、新闻推荐和知识库构建。中文文本因缺乏天然分隔符,分类流程通常涉及语料清洗、分词、特征表示与模型训练等环节。常见思路先用TF-IDF将文本转化为高维稀疏向量,配合逻辑回归等线性模型快速建立可靠基线;再引入BERT等预训练模型增强语义理解,进一步提升准确率。数据预处理与特征工程的质量直接影响模型上限,而选择合适的语料库则决定实验结果的参考价值。本文以搜狗新闻语料库为实践载体,系统梳理中文分类的完整流程,覆

weixin_33913332的博客 343

中文文本分类实战:从搜狗新闻语料到TF-IDF、CNN与预训练模型

从概念到应用,中文文本分类是NLP领域的核心任务之一,其本质是让模型理解文本语义并归入正确类别。传统方法中,TF-IDF结合线性分类器能快速构建基线,通过向量化与特征筛选反映词频信息。随着深度学习发展,Embedding加CNN模型可自动学习局部语义特征,在工程实践中提升分类准确率。近年来,预训练模型如RoBERTa通过大规模语料学习通用语言表示,微调后能进一步突破效果瓶颈。该技术链路覆盖搜索引擎、舆情分析、新闻推荐等场景。本文以搜狗新闻语料库为基础,完整介绍从数据清洗、特征工程到深度模型与预训练迁移的实战

weixin_34238633的博客 406

搜狗新闻文本分类数据集SougoCS

SougoCS数据集,内含11类搜狐新闻文本,近10万条。 搜狗提供的数据为未分类的XML格式。 此资源已经将XML解析并分类完毕,方便使用。

ODrive0.5.5源码分析(1) ADC的处理

odrive 0.5.5 ADC

danger的博客 4318

【GAN实战】基于pytorch-CycleGAN与pix2pix的自定义数据集训练全攻略

本文详细解析了基于PyTorch的CycleGAN与pix2pix模型在自定义数据集上的完整训练流程。核心在于根据数据是否成对来正确选择模型:pix2pix适用于严格配对的“孪生”数据,而CycleGAN则擅长处理非配对的“散装”数据集。文章从数据准备、配置训练到测试应用,提供了全链路实战指南与关键参数调优心得,帮助开发者高效实现图像到图像的风格转换。

x2y3z4a5b的博客 710

避坑指南:英飞凌TC3XX的MCMCAN初始化常见错误及解决方法

本文针对英飞凌TC3XX系列MCU的MCMCAN模块初始化难题,深入剖析了寄存器配置时序、总线监控模式误用、中断映射错误及Message RAM地址冲突四大常见陷阱。通过实战代码示例和诊断流程,提供了清晰的避坑指南和解决方案,帮助嵌入式开发者高效解决CAN FD通信故障,确保汽车电子与工业控制项目的稳定运行。

weixin_29193259的博客 255

vesta软件_分子动力学计算实战:利用 OVITO 软件找弗伦克尔缺陷和位错环分析

今天继续和大家分享这篇纯分子动力学(MD)的 Nat. Comm. 文章(DOI:10.1038/s41467-018-07102-3)。上期推文(从一篇 Nat. Comm. 学习经典分子动力学基本知识和相关软件基本操作 (一)),我们借用该文章的 fig.1 简单介绍了如何用文中所提到的 LAMMPS 软件来实现材料中级联碰撞过程。但是,想完整的展示类似该文章的 fig.1,单纯利用...

weixin_39524842的博客 5991

STM32驱动电磁阀电路设计:从光耦选型到MOSFET驱动的实战解析

本文深入解析了STM32驱动24V电磁阀的完整电路设计,重点探讨了光耦选型与MOSFET驱动两大核心环节。针对电平转换与电气隔离需求,详细对比了EL357N与TLP2310等光耦在CTR、速度及耐压等关键参数上的差异与应用场景。同时,文章提供了MOSFET关键参数解读、驱动电路优化方案(如推挽驱动)以及完整的PCB布局、电源处理与调试避坑指南,旨在帮助工程师构建稳定可靠的工业控制电路。

weixin_29184371的博客 246

UCIE Specification详解(七)

Logical Physical Layer(逻辑物理层)

Crazzy_M的博客 1708

k8s 不同node之间pod ip不通

底层k8s使用calico网络,ipip网络模式,kubevirt启动了2台虚拟机;在这两台虚拟机上再搭建k8s,calico网络,ipip网络模式。说明:我是使用helm安装的calico,改这个模式要改crd资源ippools。其他方式安装的calico修改方法可能不一样。在虚拟机上的k8s上启动了2个pod,分布在不同的node上,这两个pod不能ping通对方的pod ip。修改虚拟机之上的k8s集群的calico网络的ipipMode为Never即可,之前使用的是Always。

weixin_40548182的博客 4062

Amazon SES 移出沙盒完整指南 & 高通过率模板

AWS SES沙盒模式转正式模式申请指南 申请正式模式需通过AWS审核,常见被拒原因包括邮件用途不清晰、缺少退订机制或发送量不合理。人工申诉需登录AWS Support Center提交服务限额提升请求,填写详细的公司信息、邮件用途及合规措施。 高通过率申诉模板需包含: 公司信息:名称、网址及与AWS的合作情况。 邮件规范:明确类型(交易/营销)、退订链接及退信处理机制。 发送计划:合理预估发送量,确保合规。 关键点:真实信息、用途清晰、退订机制完善、发送量合理。首次被拒可修改后重新提交。

xianyinsuifeng的博客 335

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 贝叶斯④——Sklean新闻分类(CountVectorizer&TfidfTransformer&TfidfVectorizer)
下一篇: 贝叶斯⑥——银行借贷模型(贝叶斯与决策树对比)
数据小斑马
数据小斑马 领域专家: 物联网技术领域 领域专家: 物联网技术领域
博客等级 码龄8年 1606粉丝 87原创
评论 9
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值