2017-05-12 149 views
3

這個標題是指給了我一個問題筆記本:https://github.com/justmarkham/scikit-learn-videos/blob/master/07_cross_validation.ipynbTyperror在機器學習教程,numpy的

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

看來它只是缺少明確的字符串轉換:

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"