user56834
user56834

Reputation: 394

why do these python code examples all call the constructor twice?

I am new to Python, and have been reading some API examples of python scientific libraries.

in one library, scikit-learn, the code examples always call the constructor once without arguments, and then later again with all the arguments.

Here is an example (see the last line, and the third before last line (clf = linear_....), taken from the documentation of sklearn.linear_model.SGDRegressor:

>>> import numpy as np
>>> from sklearn import linear_model
>>> n_samples, n_features = 10, 5
>>> np.random.seed(0)
>>> y = np.random.randn(n_samples)
>>> X = np.random.randn(n_samples, n_features)
>>> clf = linear_model.SGDRegressor()
>>> clf.fit(X, y)
... 
SGDRegressor(alpha=0.0001, average=False, epsilon=0.1, eta0=0.01,
             fit_intercept=True, l1_ratio=0.15, learning_rate='invscaling',
             loss='squared_loss', n_iter=5, penalty='l2', power_t=0.25,
             random_state=None, shuffle=True, verbose=0, warm_start=False)

I think I might misunderstand something about Python, because I don't see why the constructor is called twice? Is there a reason for this, or is it just a weird way to show all the possible parameters of the constructor?

Upvotes: 1

Views: 86

Answers (1)

pistache
pistache

Reputation: 6223

What you are reading is a doctest, a test embedded in a documentation string.

Doctests use lines prefixed by >>> to indicate runnable examples, while their output is shown unprefixed.

The test runner will match the output of the example with the output written in the documentation test.

We can see that by running the example in an IPython interpreter :

In [18]: import numpy as np

In [19]: np.random.seed(0)

In [20]: y = np.random.randn(n_samples)

In [21]: X = np.random.randn(n_samples, n_features)

In [22]: clf = linear_model.SGDRegressor()

In [23]: clf.fit(X, y)
Out[23]: 
SGDRegressor(alpha=0.0001, average=False, epsilon=0.1, eta0=0.01,
       fit_intercept=True, l1_ratio=0.15, learning_rate='invscaling',
       loss='squared_loss', n_iter=5, penalty='l2', power_t=0.25,
       random_state=None, shuffle=True, verbose=0, warm_start=False)

TLDR; The 2nd constructor you see is not in the code, and is just a representation of the output of the fit method.

Upvotes: 3

Related Questions