我想对每个条目信息进行分类。我的工作是处理波斯文文本。我已经用朴素贝叶斯实现了一个文本分类器。我没有使用Tf-idf,因为每一个特性对我来说都很重要。但我做了一些技巧来删除stop-words和pouncs,以获得更好的准确性。
我想用SVM实现一个文本分类器,但我搜索了很多。我找到的所有内容都与通过使用Tf-idf来使用管道函数有关。如下所示:
model = Pipeline([(‘vectorizer’, CountVectorizer()),
(‘tfidf’, TfidfTransformer()),
(‘clf’, OneVsRestClassifier(LinearSVC(class_weight=”balanced”)))])现在,如果没有Tf-idf,我如何使用SVM?
谢谢
发布于 2019-01-02 18:35:33
请看这里关于支持向量机的page,这里有一个使用支持向量机进行多类分类的部分。你首先必须将你的文本转换成一个特征向量(数字,如果你希望你使用支持向量机),如果你想使用词袋,你可以使用this,所以问题和this手册页面的sklearn
你可以使用预先编写的python代码从你的文本中创建弓,做一些类似的事情-提醒你,我收集了OP的相关信息-这是不清楚的,并且与SO strandart不兼容,所以你可能需要对代码进行一些修改,以适应你的excact使用。
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> vectorizer = CountVectorizer()
>>> vectorizer
CountVectorizer(analyzer=...'word', binary=False, decode_error=...'strict',
dtype=<... 'numpy.int64'>, encoding=...'utf-8', input=...'content',
lowercase=True, max_df=1.0, max_features=None, min_df=1,
ngram_range=(1, 1), preprocessor=None, stop_words=None,
strip_accents=None, token_pattern=...'(?u)\\b\\w\\w+\\b',
tokenizer=None, vocabulary=None)
>>> corpus = [
... 'This is the first document.',
... 'This is the second second document.',
... 'And the third one.',
... 'Is this the first document?',
... ]
>>> X = vectorizer.fit_transform(corpus)
>>> X
<4x9 sparse matrix of type '<... 'numpy.int64'>'
with 19 stored elements in Compressed Sparse ... format>然后,您可能需要将x转换为密集矩阵(取决于sklearn版本),然后您可以将x输入到SVM模型中,如下所示
>>>>from sklearn import svm
>>> X = [[0], [1], [2], [3]]
>>> Y = [0, 1, 2, 3]
>>> clf = svm.SVC(gamma='scale', decision_function_shape='ovo')
>>> clf.fit(X, Y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovo', degree=3, gamma='scale', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> dec = clf.decision_function([[1]])
>>> dec.shape[1] # 4 classes: 4*3/2 = 6
6
>>> clf.decision_function_shape = "ovr"
>>> dec = clf.decision_function([[1]])
>>> dec.shape[1] # 4 classeshttps://stackoverflow.com/questions/53996311
复制相似问题