1

The following code represents an attempt at a minimal, reproducible example which compiles and runs as expected.

import numpy as np
from sklearn import  model_selection as skms

N = 20

ftr = np.linspace(-10,10,num=N) # ftr values
tgt = 2*ftr**2 -3 + np.random.uniform(-2,2,N) # tgt =func(ftr)

(train_ftr, test_ftr, train_tgt, test_tgt) = skms.train_test_split(ftr, tgt, 
test_size = N//2)

model_one = np.poly1d(np.polyfit(train_ftr, train_tgt, 1))
preds_one = model_one(test_ftr)

Where the following are of type numpy ndarrays.

train_ftr
test_ftr
train_tgt
test_tgt

My question relates to the output of the last line preds_one wrt the second last line model_one. In the last line, how can you pass a parameter test_ftr to the function model_one defined in the second last line when model_one is a composite function? That is, what is test_ftr actually being passed to?

1
  • 1
    model_one is not a composite function, it is a class instance of the numpy.poly1d class. A class instance can be callable, depending on whether you have its __call__ method defined (in this case: yes). So calling a class instance such as preds_one = model_one(test_ftr) is a simplified expression of preds_one = model_one.__call__(test_ftr). Commented Sep 7, 2021 at 5:04

1 Answer 1

1

np.poly1d returns a callable object. That is, it can be called like a function (i.e. it is a function). Objects in Python are callable if they have a __call__() function.

By calling model_one(test_ftr), you are actually calling model_one.__call__(test_ftr), which is a method that this object has.

Every function in Python is an object with a __call__ 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.