Question: Python broadcasting. Rewrite the following code without for-loops using vectorization and python broadcasting. (a) Given a data matrix X and vector beta compute a vector

Python broadcasting. Rewrite the following code without for-loops using vectorization and python broadcasting. (a) Given a data matrix X and vector beta compute a vector yhat: n = X. shape [0] yhat = np.zeros (n) for i in range(n): yhat[i] = beta [0]*X[i,0] + beta [1]*X[i, 1] + beta [2]*X[i, 1] *X[i, 2] (b) Given vectors x, alpha, and beta computes a vector yhat: n = m len(x) len(alpha) yhat = np.zeros (n) for i in range(n): for j in range (m): yhat[i] += alpha[j]*np.exp(-beta[j]*x[i]) (c) Given arrays x and y, find the squared distances dist: n, d = x. shape m, d = y.shape dist = np.zeros((n,m)) for i in range(n): for j in range (m): for k in range (d): dist[i, j] += (x[i,k]-y[j,k]) **2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
