Typerror in machine learning tutorial, numpy
This is the notebook that gives me the problem: 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]))
This is the error I am getting:
TypeError: unsupported format string passed to numpy.ndarray.__format__
I don't know where to start because I am not familiar with numpy.
+3
source to share
1 answer
It seems like it just skips the explicit string conversion:
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])))
or
print('{:^9} {} {!s:^25}'.format(iteration, data[0], data[1]))
!s
is another way to convert it to string.
In general the mini formatting language is explained in the "Format specification mini language"
+1
source to share