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](https://content.bartleby.com/qna-images/question/dbc56138-9a19-4823-89b1-5c42047e882e/dabdfad1-3aa3-458c-9c9c-f75cc0fda043/hkxaskd_thumbnail.png)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 1 steps

Knowledge Booster