Using DASK in Python to solve this problem. Given a 2D dask array, for each row, pick the number closest to 0.5 using dask array functions. A poorly performing sequential implementation has been provided for reference. 1 # A very poor performing sequential implementation def sequential_closest_to_0_5(aA): result = da.zeros(aA.shape[0]) 2 3 4 5 for row in range(aA.shape[0]): closest = None 6 7 8 9 for col in range(aA.shape[1]): if closest is None or abs(aA[row, col] - 0.5) < abs(closest 0.5): closest = aA [row, col] result [row] = closest 10 return result Complete the function below. You should use the abs, argmin, choose, and transpose functions. Don't call compute() aA is assumed to be a dask array already Test print (all(isclose(a, b) for a, b in zip(closest_to_0_5(da.from_array([[1,0.2], [0.6,0.1]])).compute(), [0.2, 0.6]))) def closest_to_0_5 (aA): ## WRITE YOUR CODE HERE Result True

icon
Related questions
Question

Using Dask in Python

 
Using DASK in Python to solve this problem.
Given a 2D dask array, for each row, pick the number closest to 0.5 using dask array
functions. A poorly performing sequential implementation has been provided for reference.
1 # A very poor performing sequential implementation
def sequential_closest_to_0_5(aA):
result = da.zeros(aA.shape[0])
2
3
4
5
for row in range(aA.shape[0]):
closest = None
6
7
8
9
for col in range(aA.shape[1]):
if closest is None or abs(aA[row, col] - 0.5) < abs(closest 0.5):
closest = aA [row, col]
result [row] = closest
10
return result
Complete the function below. You should use the abs, argmin, choose, and transpose
functions.
Don't call compute()
aA is assumed to be a dask array already
Test
print (all(isclose(a, b) for a, b in
zip(closest_to_0_5(da.from_array([[1,0.2], [0.6,0.1]])).compute(),
[0.2, 0.6])))
def closest_to_0_5 (aA):
## WRITE YOUR CODE HERE
Result
True
Transcribed Image Text:Using DASK in Python to solve this problem. Given a 2D dask array, for each row, pick the number closest to 0.5 using dask array functions. A poorly performing sequential implementation has been provided for reference. 1 # A very poor performing sequential implementation def sequential_closest_to_0_5(aA): result = da.zeros(aA.shape[0]) 2 3 4 5 for row in range(aA.shape[0]): closest = None 6 7 8 9 for col in range(aA.shape[1]): if closest is None or abs(aA[row, col] - 0.5) < abs(closest 0.5): closest = aA [row, col] result [row] = closest 10 return result Complete the function below. You should use the abs, argmin, choose, and transpose functions. Don't call compute() aA is assumed to be a dask array already Test print (all(isclose(a, b) for a, b in zip(closest_to_0_5(da.from_array([[1,0.2], [0.6,0.1]])).compute(), [0.2, 0.6]))) def closest_to_0_5 (aA): ## WRITE YOUR CODE HERE Result True
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 1 steps

Blurred answer