如何保存分类器';将最具信息量的特性转换为变量?(Python NLTK)
假设我有一段代码:如何保存分类器';将最具信息量的特性转换为变量?(Python NLTK),python,classification,nltk,training-data,Python,Classification,Nltk,Training Data,假设我有一段代码: classifier.show_most_informative_features(10) Python将显示10个特性及其pos、neg标记。 有没有办法将功能保存到变量中?方法show\u most\u informational\u features()打印出请求的功能,并返回None。要返回功能名称和“信息量”列表,请使用以下命令: features = classifier.most_informative_features(10) 您将得到一个配对列表,可以如
classifier.show_most_informative_features(10)
Python将显示10个特性及其pos、neg标记。
有没有办法将功能保存到变量中?方法
show\u most\u informational\u features()
打印出请求的功能,并返回None
。要返回功能名称和“信息量”列表,请使用以下命令:
features = classifier.most_informative_features(10)
您将得到一个配对列表,可以如下所示:
for f, w in features:
print(f, w)
方法
show\u most\u information\u features()
打印出请求的特征,并返回None
。要返回功能名称和“信息量”列表,请使用以下命令:
features = classifier.most_informative_features(10)
您将得到一个配对列表,可以如下所示:
for f, w in features:
print(f, w)
解决方案适用于python3,保存到输出字符串变量
from io import StringIO
import contextlib
import sys
@contextlib.contextmanager
def stdout_redirect(where):
sys.stdout = where
try:
yield where
finally:
sys.stdout = sys.__stdout__
with stdout_redirect(StringIO()) as new_stdout:
classifier.show_most_informative_features(15)
new_stdout.seek(0)
# output assigned to variable
output_string = new_stdout.read()
解决方案适用于python3,保存到输出字符串变量
from io import StringIO
import contextlib
import sys
@contextlib.contextmanager
def stdout_redirect(where):
sys.stdout = where
try:
yield where
finally:
sys.stdout = sys.__stdout__
with stdout_redirect(StringIO()) as new_stdout:
classifier.show_most_informative_features(15)
new_stdout.seek(0)
# output assigned to variable
output_string = new_stdout.read()
分类器
对象是否有返回这些特征的方法,而不仅仅是显示它们,或者有保存它们的实例变量?那就是我要找的地方。实际上,您的问题是如何获取该方法显示的内容,而不是仅显示它,让变量引用None
对您没有任何好处。分类器
对象是否有方法返回这些功能,而不是仅显示它们,或者有一个实例变量保存它们?那就是我要找的地方。实际上,您的问题是如何获取该方法显示的内容,而不是仅仅显示它,让变量referenceNone
对您没有任何好处。