朴素贝叶斯,SVM文本分类以及LDA生成主题特征

本文详细介绍了朴素贝叶斯、SVM和LDA在文本分类中的应用。首先,阐述了朴素贝叶斯的原理,并提供了数据集创建、词汇表构建、词向量转换及分类的步骤。接着,讨论了SVM的理论,通过词袋模型进行文本处理和训练。最后,探讨了LDA的主题生成,包括数据读取、词袋模型转换和LDA模型的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.朴素贝叶斯原理及文本分类

原理:https://blog.youkuaiyun.com/c369624808/article/details/78794741
代码部分:
1.先做一个数据集

from numpy import *

def loadDataSet():
    postingList=[['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
                 ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
                 ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
                 ['stop', 'posting', 'stupid', 'worthless', 'garbage'],
                 ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'],
                 ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]
    classVec = [0,1,0,1,0,1]    #1 is abusive, 0 not
    return postingList,classVecdef createVocabList(dataSet):

2.创建词汇表

def createVocabList(dataSet):
    vocabSet = set([])  #create empty set
    for document in dataSet:
        vocabSet = vocabSet | set(document) #union of the two sets
    return list(vocabSet)

3.词向量

def setofwords2vec(vocablist,inputset):
    returnvec=[0]*len(vocabilst)
    for word in inputset:
        if word in vocablist:
            returnvec[vocablist.index(word)]=1
    return returnvec

4.按照贝叶斯原理写个训练函数

def trainNB0(trainMatrix,trainCategory):
    numTrainDocs = len(trainMatrix)
    numWords = len(trainMatrix[0])
    pAbusive = sum(trainCategory)/float(numTrainDocs)
    p0Num = ones(numWords); p1Num = ones(numWords)      #change to ones() 
    p0Denom = 2.0; p1Denom = 2.0                        #change to 2.0
    for i in range(numTrainDocs):
        if trainCategory[i] == 1:        
            p1Num += trainMatrix[i]
            p1Denom += sum(trainMatrix[i])
        else:
            p0Num += trainMatrix[i]
            p0Denom += sum(trainMatrix[i])
    p1Vect = log(p1Num/p1Denom)          #change to log()
    p0Vect = log(p0Num/p0Denom)          #change to log()

    return p0Vect,p1Vect,pAbusive

5.分类

def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
    p1 = sum(vec2Classify * p1Vec) + log(pClass1)    #element-wise mult
    p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1)
    if p1 > p0:
        return 1
    else: 
        return 0

二.SVM原理及文本分类

原理:https://blog.youkuaiyun.com/weixin_39605679/article/details/81170300
代码部分:
1.调用数据集

import numpy as np
import sklearn
from sklearn.datasets import fetch_20newsgroups
twenty_train=fetch_20newsgroups(subset='train',shuffle=True)
twenty_train.traget_names

2.词袋模型

from sklearn.feature_extraction.text import CountVectorizer
count_vect=CountVectorizer()
x_train_counts=count_vect.fit_transform(twenty_train.data)#词袋模型

3.训练及给出答案

from sklearn.naive_bayes import MultinomialNB
clf=MultionmialNB.fit(x_train_counts,twenty_train.target)
twenty_test=fetch_20newsgroups(subset='test',shuffle=True)#生成测试集
x_test_counts=count_vect.transform(twenty_test.data)
predicted=clf.predict(x_test_counts)
np.mean(predicted==twenty_test.target)

三.LDA原理及代码实现

原理:https://blog.youkuaiyun.com/Kaiyuan_sjtu/article/details/83572927
代码:
1.读取和处理数据

from gensim.test.utils import common_texts
from gensim.corpora.dictionary import Dictionary

# Create a corpus from a list of texts
common_dictionary = Dictionary(common_texts)
common_corpus = [common_dictionary.doc2bow(text) for text in common_texts]

# Train the model on the corpus.
lda = LdaModel(common_corpus, num_topics=10)

2.将文本转化为词袋模型

from gensim.corpora import Dictionary
dct = Dictionary(["máma mele maso".split(), "ema má máma".split()])
dct.doc2bow(["this", "is", "máma"])
[(2, 1)]
dct.doc2bow(["this", "is", "máma"], return_missing=True)
([(2, 1)], {u'this': 1, u'is': 1})

3.运用lda模型

from gensim.models import LdaModel
lda = LdaModel(common_corpus, num_topics=10)
lda.print_topic(1, topn=2)
'0.500*"9" + 0.045*"10"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值