繁体   English   中英

有没有一种方法可以阻止python限制对象的字符串表示形式?

[英]Is there a way to stop python from limiting the string representation of an object?

基本上,我想保存scikit学习管道的字符串表示形式,以便我确切地知道是什么创建了数据。 但是,当尝试获取repr()或str()表示形式时,python使用...来限制大小。

from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline

pipeline = Pipeline([
    ('ngrams', Pipeline([
        ('count_vectorizer1', CountVectorizer(max_features=200, token_pattern=r'(?u)\b\w+\b')),
    ])),
    ('classifier', RandomForestClassifier(n_estimators=2000, n_jobs=5))
])

print str(pipeline)
print repr(pipeline)

输出:

Pipeline(steps=[('ngrams', Pipeline(steps=[('count_vectorizer1', CountVectorizer(analyzer=u'word', binary=False, decode_error=u'strict',
        dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content',
        lowercase=True, max_df=1.0, max_features=200, min_df=1,
        ngram_range=(1, 1), preproc...n_jobs=5,
            oob_score=False, random_state=None, verbose=0,
            warm_start=False))])
Pipeline(steps=[('ngrams', Pipeline(steps=[('count_vectorizer1', CountVectorizer(analyzer=u'word', binary=False, decode_error=u'strict',
        dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content',
        lowercase=True, max_df=1.0, max_features=200, min_df=1,
        ngram_range=(1, 1), preproc...n_jobs=5,
            oob_score=False, random_state=None, verbose=0,
            warm_start=False))])

注意两种情况下的...。 有没有办法告诉python不要这样做?

可以肯定的是,它们使用的是NumPy的stringify。

尝试:

import numpy
numpy.set_printoptions(threshold=numpy.nan)

打印之前的某处。

编辑:不,我的错,我们是在谈论sklearn,而不是scikit本身。 sklearn有自己的基础对象,而BaseEstimator则在破坏字符串。

https://github.com/scikit-learn/scikit-learn/blob/51a765a/sklearn/base.py#L111

_pprint函数(第274行的BaseEstimator的__repr__使用)中的第__repr__行。 子对象直接从基础继承该子对象,因此应该只能够覆盖一个函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM