机器学习教程中的Typerror,numpy

时间:2017-05-12 00:46:04

标签: python numpy scikit-learn jupyter

这是给我一个问题的笔记本:https://github.com/justmarkham/scikit-learn-videos/blob/master/07_cross_validation.ipynb

from sklearn.cross_validation import KFold
kf = KFold(25, n_folds=5, shuffle=False)

# print the contents of each training and testing set
print('{} {:^61} {}'.format('Iteration', 'Training set observations',             
'Testing set observations'))
for iteration, data in enumerate(kf, start=1):
    print('{:^9} {} {:^25}'.format(iteration, data[0], data[1]))

这是我得到的错误:

TypeError: unsupported format string passed to numpy.ndarray.__format__

我真的不知道从哪里开始,因为我对numpy并不熟悉。

1 个答案:

答案 0 :(得分:1)

它似乎只是缺少明确的字符串转换:

from sklearn.cross_validation import KFold
kf = KFold(25, n_folds=5, shuffle=False)

# print the contents of each training and testing set
print('{} {:^61} {}'.format('Iteration', 
                            'Training set observations', 
                            'Testing set observations'))
for iteration, data in enumerate(kf, start=1):
    print('{:^9} {} {:^25}'.format(iteration, data[0], str(data[1])))

或:

    print('{:^9} {} {!s:^25}'.format(iteration, data[0], data[1]))

!s只是将其转换为字符串的另一种方式。

一般来说,格式化迷你语言在"Format Specification Mini-Language"

中有解释