Logan Williams
Logan Williams

Reputation: 19

Predict function for multiple linear regression

I am trying to make a predict function for a homework problem where it takes the dot products of a matrix(x) and a vector(y) and inserts them into a numpy array

def predict(x, y):
    y_hat = np.empty
    for j in range(len(y)):
        y_hat[i] = np.dot(x, y)
    return y_hat

There is an error message on y_hat[i] = np.dot(x,y)

Upvotes: 1

Views: 52

Answers (1)

Masoud
Masoud

Reputation: 1280

There are two errors in the code:

  1. numpy.empty() is a method which get arguments for the shape. Here, you must define it as np.empty([len(y), len(x)]) (if x is matrix and y is a vector,np.dot(x, y) results a vector with length len(x)). It produces a placeholder for np.dot() resulted arrays.
  2. variable i is not defined.

so:

def predict(x, y):
    y_hat = np.empty([len(y), len(x)])
    for j in range(len(y)):
        y_hat[j] = np.dot(x, y)
    return y_hat

Upvotes: 2

Related Questions