1

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?

1
  • 1
    Are you sure he calls it twice ? Isn't that just an example of the __repr__ of the constructed instance ? The line isn't prefixed by >>>. Commented Aug 13, 2016 at 12:40

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.