3
x_d = np.linspace(-4, 8, 30)

print('x_d shape: ',x_d.shape)
print('x shape: ',x.shape)


density = sum((abs(xi - x_d) < 0.5) for xi in x)---------> difficulty in understanding statement

output:

x_d shape:  (30,)
x shape:  (20,)

I am having difficulty in understanding above statement

for each value of x we are substracting x_d from it, and we will get single value. But we are density as (30,)

How we got density dimension as (30,)

1 Answer 1

3

The expression

xi - x_d

will use NumPy broadcasting to conform the shapes of the two objects. In this case it means treating the scalar value xi as if it was an array of all the same value and of equal dimensions as x_d.

The abs function and the less-than comparison will work element-wise with NumPy arrays, so that the expression

(abs(xi - x_d) < 0.5)

should result in a length-30 array (same size as x_d) where each entry of that array is either True or False depending on the condition applied to each element of x_d.

This gets repeated for multiple values of xi, leading to multiple different length-30 arrays.

The result of calling sum on these arrays is that they are added together elementwise (and also by the luck of broadcasting, since the sum function has a default initial value of 0, the first array is added to 0 elementwise, leaving it unchanged).

So in the final result, it will be a length-30 array, where item 0 of the array counts how many xi values satisfied the absolute value condition based on the 0th element of x_d. Item 1 of the output array will count the number of xi values that satisfied the absolute value condition on the 1st element of x_d, and so on.

Here is an example with some test data:

In [31]: x_d = np.linspace(-4, 8, 30)

In [32]: x = np.arange(20)

In [33]: x
Out[33]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])

In [34]: density = sum((abs(xi - x_d) < 0.5) for xi in x)

In [35]: density
Out[35]: 
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1])
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for response. I didn't understand statement. 'This gets repeated for multiple values of xi, leading to multiple different length-30 arrays'. For loop is repeated for each element of x which in this case so we get list of 20 arrays of shape(30,) for each element of x and we are summing all 20 arrays of shape(30). Is my understanding right. By the way is there is a way to print each array of length 30 for each of value of x? . Thanks
Imagine taking only the first value of x (the first iteration of the loop inside of sum). Let's pretend this value is 0. Then you will compute 0 - x_d. In NumPy, this results in an array that has the values [0 - x_d[0], 0 - x_d[1], ...]. So we get a length-30 array for that iteration. Let's put it in the back of our mind and move on to the next iteration, which will come from the entry x[1] (the second entry in x). We repeat the process and get [x[1] - x_d[0], x[1] - x_d[1], ...]. So inside sum, each pass of the loop is creating a whole length-30 array for that iteration.
If you want to print what the intermediate arrays are you can use for xi in x: print((abs(xi - x_d) < 0.5)). You could even manually save 3 of them into a list, like this: test = [(abs(x[0] - x_d) < 0.5), (abs(x[1] - x_d) < 0.5), (abs(x[2] - x_d) < 0.5)], and then try sum(test) to see what happens.
thanks for help. Now it helps to understand statement.

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.