有没有办法阻止python限制对象的字符串表示?

时间:2016-03-10 19:32:20

标签: python scikit-learn

基本上我想保存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不要这样做?

1 个答案:

答案 0 :(得分:0)

很确定那些人正在使用NumPy的stringify。

尝试:

__repr__
在打印之前的某个地方。

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

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

第142行,在_pprint函数中(第274行由BaseEstimator的 keyPadWindow = UIView(frame: CGRect(x: 128, y: 224, width: 512, height: 356)) var keyCount = 0 keyPadSV = UIStackView(frame: CGRect(x: 0, y: 0, width: 512, height: 356)) print("pickerWindow.bounds \(pickerWindow.bounds)") keyPadSV!.axis = .Vertical keyPadSV!.spacing = 0.0 keyPadSV!.alignment = .Center keyPadSV!.distribution = .EqualSpacing for var k2s = 0; k2s < 5; k2s++ { let keyPadSVB = UIStackView(frame:CGRect(x: 0, y: 0, width: 0, height: 0)) keyPadSVB.axis = .Horizontal keyPadSVB.spacing = 0.0 keyPadSVB.alignment = .Center keyPadSVB.distribution = .EqualSpacing for var k4s = 0; k4s < 4; k4s++ { let button = UIButton(type: UIButtonType.Custom) as UIButton button.frame = CGRectMake(0, 0, 0, 0) button.tag = keyCount let blah = "x" + String(keyCount) let blahIMAGE = UIImage(named: blah) button.setImage(blahIMAGE, forState: UIControlState.Normal) print("blah \(blah)") button.addTarget(self, action: "keyPadPress:", forControlEvents: UIControlEvents.TouchUpInside) keyPadSVB.addArrangedSubview(button) keyCount++ } keyPadSV.addArrangedSubview(keyPadSVB) } keyPadWindow.addSubview(keyPadSV) self.view.addSubview(keyPadWindow) } 使用)。子对象直接从基础继承,所以应该只能覆盖一个函数。