是否有添加到现有语料库的功能?我已经生成了我的矩阵,我希望定期添加到表中而不需要重新整理整个sha-bang
例如;
articleList = ['here is some text blah blah','another text object', 'more foo for your bar right now']
tfidf_vectorizer = TfidfVectorizer(
max_df=.8,
max_features=2000,
min_df=.05,
preprocessor=prep_text,
use_idf=True,
tokenizer=tokenize_text
)
tfidf_matrix = tfidf_vectorizer.fit_transform(articleList)
#### ADDING A NEW ARTICLE TO EXISTING SET?
bigger_tfidf_matrix = tfidf_vectorizer.fit_transform(['the last article I wanted to add'])
Run Code Online (Sandbox Code Playgroud) 术语频率(TF)和反向文档频率(IDF)如何受到停用词删除和词干的影响?
谢谢!
我尝试使用火花1.1.0提供的新TFIDF算法.我正在用Java编写我的MLLib工作,但我无法弄清楚如何使TFIDF实现工作.由于某种原因,IDFModel仅接受JavaRDD作为方法转换的输入而不是简单的Vector.如何使用给定的类为我的LabledPoints建模TFIDF向量?
注意:文档行的格式为[标签; 文本]
到目前为止我的代码:
// 1.) Load the documents
JavaRDD<String> data = sc.textFile("/home/johnny/data.data.new");
// 2.) Hash all documents
HashingTF tf = new HashingTF();
JavaRDD<Tuple2<Double, Vector>> tupleData = data.map(new Function<String, Tuple2<Double, Vector>>() {
@Override
public Tuple2<Double, Vector> call(String v1) throws Exception {
String[] data = v1.split(";");
List<String> myList = Arrays.asList(data[1].split(" "));
return new Tuple2<Double, Vector>(Double.parseDouble(data[0]), tf.transform(myList));
}
});
tupleData.cache();
// 3.) Create a flat RDD with all vectors
JavaRDD<Vector> hashedData = tupleData.map(new Function<Tuple2<Double,Vector>, Vector>() …Run Code Online (Sandbox Code Playgroud) 我正在使用服装标记器传递给TfidfVectorizer.该标记化器依赖于外部类TermExtractor,它位于另一个文件中.
我基本上想要基于某些术语构建TfidVectorizer,而不是所有单个单词/令牌.
这是代码:
from sklearn.feature_extraction.text import TfidfVectorizer
from TermExtractor import TermExtractor
extractor = TermExtractor()
def tokenize_terms(text):
terms = extractor.extract(text)
tokens = []
for t in terms:
tokens.append('_'.join(t))
return tokens
def main():
vectorizer = TfidfVectorizer(lowercase=True, min_df=2, norm='l2', smooth_idf=True, stop_words=stop_words, tokenizer=tokenize_terms)
vectorizer.fit(corpus)
pickle.dump(vectorizer, open("models/terms_vectorizer", "wb"))
Run Code Online (Sandbox Code Playgroud)
运行正常,但每当我想重新使用这个TfidfVectorizer并用pickle加载它时,我收到一个错误:
vectorizer = pickle.load(open("models/terms_vectorizer", "rb"))
Traceback (most recent call last):
File "./train-nps-comments-classifier.py", line 427, in <module>
main()
File "./train-nps-comments-classifier.py", line 325, in main
vectorizer = pickle.load(open("models/terms_vectorizer", "rb"))
File "/usr/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", …Run Code Online (Sandbox Code Playgroud) 如何计算tf-idf查询?我理解如何使用以下定义计算一组文档的tf-idf:
tf =文档中的出现/文档中的总词数
idf = log(#documents/#documents,其中包含术语
但我不明白这与查询有何关联.
例如,我读了一个资源,说明了查询的值" life learning"
生活| tf = .5 | idf = 1.405507153 | tf_idf = 0.702753576
学习| tf = .5 | idf = 1.405507153 | tf_idf = 0.702753576
tf我理解的值,每个术语只出现在两个可能的术语中,因此1/2,但我不知道idf它来自何处.
我认为#documents = 1和occurrence = 1,log(1)= 0,所以idf将是0,但似乎并非如此.它是基于您使用的任何文件?你如何计算查询的tf-idf?
我必须对一些情绪进行分类,我的数据框是这样的
Phrase Sentiment
is it good movie positive
wooow is it very goode positive
bad movie negative
Run Code Online (Sandbox Code Playgroud)
我做了一些预处理作为标记化停止词干...等我得到
Phrase Sentiment
[ good , movie ] positive
[wooow ,is , it ,very, good ] positive
[bad , movie ] negative
Run Code Online (Sandbox Code Playgroud)
我需要最终得到一个数据帧,该行是文本,其值是tf_idf,列是像这样的单词
good movie wooow very bad Sentiment
tf idf tfidf_ tfidf tf_idf tf_idf positive
Run Code Online (Sandbox Code Playgroud)
(其余两条线也一样)
我有一个产品数据集的TF-IDF矩阵:
tfidf = TfidfVectorizer().fit_transform(words)
Run Code Online (Sandbox Code Playgroud)
这里的话是说明的列表。这将生成69258x22024矩阵。
现在,我想找到一个新产品与矩阵中的余弦相似度,因为我需要找到与其最相似的10个乘积。我使用与上面相同的方法对其进行矢量化。
但是,我不能将矩阵相乘,因为它们的大小不同(新的矩阵将是6个单词,所以是1x6的矩阵),因此我需要制作一个TFIDFVectorizer,其列数为原始列数。
我该怎么做?
在我试图实施的论文中,它说,
在这项工作中,推文使用三种类型的文本表示建模.第一个是由tf-idf(术语频率 - 逆文档频率)加权的词袋模型(第2.1.1节).第二个代表一个句子,通过平均所有单词的嵌入(在句子中),第三个代表一个句子,通过平均所有单词的加权单词嵌入,单词的权重由tf-idf给出(第2.1.2节) ).
我不确定所提到的第三种表示形式,因为使用单词权重的加权单词嵌入由tf-idf给出.我甚至不确定它们是否可以一起使用.
在本书“ TensorFlow机器学习指南”的第七章中,作者在数据预处理中使用fit_transformscikit-learn的tfidf功能来获取文本的特征进行训练。作者将所有文本数据提供给函数,然后再将其分为训练和测试。这是真的吗?还是我们必须先分离数据,然后再进行fit_transform训练和transform测试?
我正在使用 TFIDF 稀疏矩阵进行文档分类,并且希望仅保留每个文档的前 n 个(比如 50 个)术语(按 TFIDF 分数排名)。请参阅下面的编辑。
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
tfidfvectorizer = TfidfVectorizer(analyzer='word', stop_words='english',
token_pattern='[A-Za-z][\w\-]*', max_df=0.25)
n = 50
df = pd.read_pickle('my_df.pickle')
df_t = tfidfvectorizer.fit_transform(df['text'])
df_t
Out[15]:
<21175x201380 sparse matrix of type '<class 'numpy.float64'>'
with 6055621 stored elements in Compressed Sparse Row format>
Run Code Online (Sandbox Code Playgroud)
我已经尝试按照这篇文章中的示例进行操作,虽然我的目的不是显示特征,而是在训练前为每个文档选择前 n 个。但是我收到内存错误,因为我的数据太大而无法转换为密集矩阵。
df_t_sorted = np.argsort(df_t.toarray()).flatten()[::1][n]
Traceback (most recent call last):
File "<ipython-input-16-e0a74c393ca5>", line 1, in <module>
df_t_sorted = np.argsort(df_t.toarray()).flatten()[::1][n]
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\sparse\compressed.py", …Run Code Online (Sandbox Code Playgroud) python sparse-matrix tf-idf scikit-learn text-classification
tf-idf ×10
python ×5
scikit-learn ×5
nlp ×2
apache-spark ×1
data-mining ×1
dataframe ×1
java ×1
pandas ×1
pickle ×1
search ×1
stemming ×1
stop-words ×1
tensorflow ×1
text-mining ×1
word2vec ×1