Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typerror in machine learning tutorial, numpy

This is the notebook that's giving me a 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 get:

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

I don't really know where to start because I'm not that familiar with numpy.

like image 263
user3605834 Avatar asked Mar 31 '26 00:03

user3605834


1 Answers

It seems it's just missing 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]))

The !s is just another way of converting it to a string.

In general the formatting mini-language is explained in "Format Specification Mini-Language"

like image 171
MSeifert Avatar answered Apr 02 '26 12:04

MSeifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!