scikit-learn/scikit-learn — pull request #699 by njwilson (2012-03-15 07:00:54+00:00)
Title: Add verbose parameter to SVMs (fixes #250)

Add a verbose parameter to SVMs to get verbose output from libsvm
printed to stdout while fitting a model.

Note that this setting takes advantage of a per-process runtime setting
in libsvm that, if enabled, may not work properly in a multithreaded
context (the output may come out interleaved).

It works like this:

>>> from sklearn import svm
>>> X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]
>>> Y = [1, 1, 1, 2, 2, 2]
>>> clf = svm.SVC().fit(X, Y) # verbose=False
>>> clf = svm.SVC(verbose=True).fit(X, Y) # verbose=True
.*.*
optimization finished, #iter = 17
obj = -1.492063, rho = 0.000025
nSV = 6, nBSV = 0
Total nSV = 6

Similarly for sparse SVMs:

>>> clf = svm.sparse.SVC().fit(X, Y)
>>> clf = svm.sparse.SVC(verbose=True).fit(X, Y)
.*.*
optimization finished, #iter = 17
obj = -1.492063, rho = 0.000025
nSV = 6, nBSV = 0
Total nSV = 6

I tried to write a test that captures and verifies stdout, but I was
unable to capture stdout with ``sys.stdout = StringIO`` (I tried stderr
too). I'm assuming this is because the output is coming from C code
(svm/src/libsvm/libsvm_helper.c) rather than Python code.

A possible future improvement may be to allow the user to provide a
Python callback function that is passed the string to be printed. This
would provide more control over what gets printed where...