sklearn:使用 CountVectorizer 对象获取新字符串的特征向量
sklearn: Using CountVectorizer object to get a feature vector of a new string
所以我通过执行以下几行来创建一个 CountVectorizer 对象。
count_vectorizer = CountVectorizer(binary='true')
data = count_vectorizer.fit_transform(data)
现在我有一个新字符串,我想将该字符串映射到我从 CountVectorizer 获得的 TDM 矩阵。
所以我对输入到 TDM 的字符串的期望是一个相应的文档术语向量。
我试过了,
count_vectorizer.transform([string])
报错,AttributeError: transform not found
添加堆栈跟踪的一部分,它是一个很长的堆栈跟踪,因此我只添加相关位,即跟踪的最后几行。
File "/Users/ankit/Desktop/geny/APIServer/RUNTIME/src/controller/sentiment/Sentiment.py", line 29, in computeSentiment
vec = self.models[model_name]["vectorizer"].transform([string])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/sparse/base.py", line 440, in __getattr__
raise AttributeError(attr + " not found")
请指教。
谢谢
Ankit S
您展示的示例不可重现 - 这里的字符串变量是什么?
但是,以下代码似乎可以完美运行:-
from sklearn.feature_extraction.text import CountVectorizer
data = ["aa bb cc", "cc dd ee"]
count_vectorizer = CountVectorizer(binary='true')
data = count_vectorizer.fit_transform(data)
# Check if your vocabulary is being built perfectly
print count_vectorizer.vocabulary_
# Trying a couple new string with added new word. new word should be ignored
newData = count_vectorizer.transform(["aa dd mm", "aa bb"])
print newData
# You can get the array by writing
print newData.toarray()
嗯,count_vectorizer.transform() 接受字符串列表 - 而不是单个字符串。如果变换拟合不起作用,它应该引发“ValueError:词汇未拟合或为空!”如果出现此类错误,请粘贴整个回溯堆栈(异常堆栈)。没有人能看到 AttributeError 的来源——你的代码或 sklearn 中的一些内部错误。
所以我通过执行以下几行来创建一个 CountVectorizer 对象。
count_vectorizer = CountVectorizer(binary='true')
data = count_vectorizer.fit_transform(data)
现在我有一个新字符串,我想将该字符串映射到我从 CountVectorizer 获得的 TDM 矩阵。 所以我对输入到 TDM 的字符串的期望是一个相应的文档术语向量。
我试过了,
count_vectorizer.transform([string])
报错,AttributeError: transform not found 添加堆栈跟踪的一部分,它是一个很长的堆栈跟踪,因此我只添加相关位,即跟踪的最后几行。
File "/Users/ankit/Desktop/geny/APIServer/RUNTIME/src/controller/sentiment/Sentiment.py", line 29, in computeSentiment
vec = self.models[model_name]["vectorizer"].transform([string])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/sparse/base.py", line 440, in __getattr__
raise AttributeError(attr + " not found")
请指教。
谢谢
Ankit S
您展示的示例不可重现 - 这里的字符串变量是什么? 但是,以下代码似乎可以完美运行:-
from sklearn.feature_extraction.text import CountVectorizer
data = ["aa bb cc", "cc dd ee"]
count_vectorizer = CountVectorizer(binary='true')
data = count_vectorizer.fit_transform(data)
# Check if your vocabulary is being built perfectly
print count_vectorizer.vocabulary_
# Trying a couple new string with added new word. new word should be ignored
newData = count_vectorizer.transform(["aa dd mm", "aa bb"])
print newData
# You can get the array by writing
print newData.toarray()
嗯,count_vectorizer.transform() 接受字符串列表 - 而不是单个字符串。如果变换拟合不起作用,它应该引发“ValueError:词汇未拟合或为空!”如果出现此类错误,请粘贴整个回溯堆栈(异常堆栈)。没有人能看到 AttributeError 的来源——你的代码或 sklearn 中的一些内部错误。