Reputation: 409
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
?
Upvotes: 3
Views: 2110
Reputation: 36805
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])
Upvotes: 2
Reputation: 7222
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.
Upvotes: 6