I'm trying to code the design function used in linear regression using numpy and I get this error:

Traceback (most recent call last): File "C:\Users\visha\AppData\Local\Continuum\anaconda3\lib\site-
packages\nose\case.py", line 197, in runTest self.test(*self.arg) File "C:\Users\visha\machinelearning\test.py", line 23, in test_compute_Phi assert type(Phi) == np.matrixlib.defmatrix.matrix AssertionError

input > x: input vector of shape n x 1, p: the number of polynomials/features

TEST CASE : x = np.mat('1.;2.;3') , P=2

can you please help me correct this code ?


def compute_Phi(x,p):

    x = np.asarray(x)
    Phi = np.zeros(shape=(len(x),p+1))
    for i in range(0,p+1):
        Phi[:,i] = np.power(x,i).reshape(x.shape[0],)

    return Phi
  • $\begingroup$ It is working for me. Don't know why it's causing you an error. Try explicitly converting Phi to a matrix in your return statement as np.matrix(Phi) $\endgroup$
    – bkshi
    Sep 19, 2019 at 7:14
  • $\begingroup$ Hi @bkshi, I did and it throws a ValueError: could not broadcast input array from shape (1,3) into shape (3,1) $\endgroup$
    – VishwaV
    Sep 19, 2019 at 22:13


Your Answer

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