3

Code below:

def computerCost(x,y,theta):
    m = len(y)
    J = np.sum((np.dot(x,theta) - y)**2) /(2*m)
    return J

m = 100
x = np.linspace(-5,10,m)
y = np.linspace(1,100,m)
x, y = x.reshape(m,1), y.reshape(m,1)
theta_0 = np.linspace(-10,10,100)
theta_1 = np.linspace(-1,4,100)
X,Y = np.meshgrid(theta_0,theta_1)

###### Here I want to initialize a numpy array with generator.
J_vals = np.array(computerCost(x,y,np.array([a,b])) for a,b in zip(np.ravel(X), np.ravel(Y)) )

print('out:',J_vals)

Running this code in Python 3.5 gives:

out:<generator object <genexpr> at 0x0000028ACF28B258>

The console prints that J_vals is a generator. Is there some way to change the generator into a np.ndarrray?

4
  • Can you try to explain what you are trying to solve? Maybe you can vectorize the operation, removing the need for zip() and fromiter(). Commented Jan 11, 2018 at 8:49
  • Inside your function you are doing np.dot(x, theta) where x is an array of shape (100, 1) and theta is of shape (2,). This will not work. Can you try to explain the math you are trying to do here? Commented Jan 11, 2018 at 8:57
  • @NilsWerner You are right. I just want to use Least squares to draw a line model to fitting my data. And the computerCost function is to computer cost in one theta(matrix:[theta_0,theta_1]) Commented Jan 11, 2018 at 9:06
  • See my answer on how to do it without the for loop and the zip. Commented Jan 11, 2018 at 9:14

2 Answers 2

6

You're looking for np.fromiter.

Here's a simpler example to demonstrate how it works:

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> np.fromiter((i + j for (i, j) in zip(a, b)), np.float)
array([ 5.,  7.,  9.])

Note you have to supply the data type as the second argument, and that the generator expression must be parenthesized since it's not the sole argument.

When I tried this with your sample code, I got an error saying shapes are not aligned... I'm guessing it's an issue with the dot product.

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

1 Comment

Thank you very much. You are right, I should change np.arrray([a,b]) in J_vals = np.array(computerCost(x,y,np.array([a,b])) for a,b in zip(np.ravel(X), np.ravel(Y))) to np.array([[a,b]]) since the shape problem in dot product operation.
2

You can use NumPy broadcasting to vectorize your operation, avoiding the need for Python loops altogether:

def computerCost(x, y, theta):
    return np.sum((x * theta - y) ** 2, axis=(0, 1)) / (2 * len(y))

m = 100
x = np.linspace(-5,10,m)[:, None, None]
y = np.linspace(1,100,m)[:, None, None]

theta_0 = np.linspace(-10,10,100)
theta_1 = np.linspace(-1,4,100)
X, Y = np.meshgrid(theta_0,theta_1)

XY = np.stack((X.ravel(), Y.ravel()))[None, :, :]

computerCost(x, y, XY)
# array([ 7442.62878788,  7340.86628993,  7240.13955518, ...,  1322.02086831,
#         1320.72740104,  1320.46969697])

2 Comments

I want to know why you add [:,None,Noe] in x = np.linspace(-5,10,m)[:, None, None], And why this operation can work well. @NilsWerner
You need to arrange your axes right for NumPy broadcasting to work.

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.