gpt4 book ai didi

python - 如何使用 sklearn 库使用朴素贝叶斯进行文本分类?

转载 作者:行者123 更新时间:2023-11-30 09:26:18 25 4
gpt4 key购买 nike

我正在尝试使用朴素贝叶斯文本分类器进行文本分类。我的数据采用以下格式,根据问题和摘录,我必须决定问题的主题。训练数据有超过20K条记录。我知道 SVM 在这里会是一个更好的选择,但我想选择 Naive Bayes using sklearn library .

{[{"topic":"electronics","question":"What is the effective differencial effective of this circuit","excerpt":"I'm trying to work out, in general terms, the effective capacitance of this circuit (see diagram: http://i.stack.imgur.com/BS85b.png).  \n\nWhat is the effective capacitance of this circuit and will the ...\r\n        "},
{"topic":"electronics","question":"Outlet Installation--more wires than my new outlet can use [on hold]","excerpt":"I am replacing a wall outlet with a Cooper Wiring USB outlet (TR7745). The new outlet has 3 wires coming out of it--a black, a white, and a green. Each one needs to be attached with a wire nut to ...\r\n "}]}

这是我迄今为止尝试过的,

import numpy as np
import json
from sklearn.naive_bayes import *

topic = []
question = []
excerpt = []

with open('training.json') as f:
for line in f:
data = json.loads(line)
topic.append(data["topic"])
question.append(data["question"])
excerpt.append(data["excerpt"])

unique_topics = list(set(topic))
new_topic = [x.encode('UTF8') for x in topic]
numeric_topics = [name.replace('gis', '1').replace('security', '2').replace('photo', '3').replace('mathematica', '4').replace('unix', '5').replace('wordpress', '6').replace('scifi', '7').replace('electronics', '8').replace('android', '9').replace('apple', '10') for name in new_topic]
numeric_topics = [float(i) for i in numeric_topics]

x1 = np.array(question)
x2 = np.array(excerpt)
X = zip(*[x1,x2])
Y = np.array(numeric_topics)
print X[0]
clf = BernoulliNB()
clf.fit(X, Y)
print "Prediction:", clf.predict( ['hello'] )

但正如预期的那样,我收到 ValueError:无法将字符串转换为 float 。我的问题是如何创建一个简单的分类器来将问题和摘录分类为相关主题?

最佳答案

sklearn 中的所有分类器都要求输入表示为某个固定维度的向量。对于文本有 CountVectorizer , HashingVectorizerTfidfVectorizer它可以将字符串转换为 float 向量。

vect = TfidfVectorizer()
X = vect.fit_transform(X)

显然,您需要以相同的方式对测试集进行矢量化

clf.predict( vect.transform(['hello']) )

查看tutorial on using sklearn with textual data .

关于python - 如何使用 sklearn 库使用朴素贝叶斯进行文本分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com