小编mec*_*ech的帖子

将列添加到Tfidf矩阵

我想使用单词以及一些附加功能(例如,有链接)在文本上构建分类模型

tweets = ['this tweet has a link htt://link','this one does not','this one does http://link.net']
Run Code Online (Sandbox Code Playgroud)

我使用sklearn来获取我的文本数据的稀疏矩阵

tfidf_vectorizer = TfidfVectorizer(max_df=0.90, max_features=200000, min_df=0.1, stop_words='english', use_idf=True, ntlk.tokenize,ngram_range=(1,2))

tfidf_matrix = tfidf_vectorizer.fit_transform(tweets)

我想为其添加列以支持我的文本数据的其他功能.我试过了:

import scipy as sc
Run Code Online (Sandbox Code Playgroud)

all_data = sc.hstack((tfidf_matrix, [1,0,1]))

这给了我这样的数据:

array([ <3x8 sparse matrix of type '<type 'numpy.float64'>' with 10 stored elements in Compressed Sparse Row format>, 1, 1, 0], dtype=object)

当我将此数据框提供给模型时:

`from sklearn.naive_bayes import MultinomialNB
 clf = MultinomialNB().fit(all_data, y)` 
Run Code Online (Sandbox Code Playgroud)

我收到了一个追溯错误:

`Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File …
Run Code Online (Sandbox Code Playgroud)

python nltk scipy scikit-learn

3
推荐指数
1
解决办法
2069
查看次数

标签 统计

nltk ×1

python ×1

scikit-learn ×1

scipy ×1