Last answered:

01 Aug 2022

Posted on:

25 Jul 2022

0

Difference in display of s_array.shape - Would a scalar return with a result of 1 or nothing?

Hi 365 Team,

I'm currently viewing the Scalars, Vectors and Matrices as Python Arrays lesson of Mathematics course.

I found a discrepancy in the display when I copied the lesson commands and ran it on my own Jupyter notebook. In the video of the lesson, "s_array.shape" returned only parentheses, while mine returned "(1,)" (Out [17] in first screen capture). However, I had the captions turned on, which indicated the same result as what I had (printed that "(1,)" was an expected return value, see second screen capture)

I also got curious and ran a code in [19] that could test if it is a scalar or not, which it said "s_array" is not a scalar.
Does that mean if we run "np.array" it would turn the scalar into a (1,) vector? Or how does it remain a 0 dimension scalar in the program?

Thanks in advance for clearing the confusion!

Math_ScalarsQ
Math_ScalarsQ2

1 answers ( 0 marked as helpful)
Instructor
Posted on:

01 Aug 2022

0

Dear Eve,

Thank you for your question and the observations you've made!

Let me address the issue with the subtitles first. The captions indeed do not correspond to the voice at this part of the lecture - we will have that corrected, thank you for letting us know.

For the rest of your questions, let me refer to the code snippet below:
image.png
I have defined 2 variables - variable1 and variable2. They are both np-arrays. However, in the first one the number 5 is not surrounded by square brackets, while in variable2 it is. Notice that, as a result, the shape of variable1 is (), as in the lecture, while the shape of variable2 is (1, ), as in your notebook. Therefore, the square brackets play a crucial role - without them, we get a 0-dimensional scalar object, but putting them returns a vector object.

The other 2 results are the outputs of the np.isscalar() and the np.ndim() methods. The first one is the method you have used and, indeed, it returns False for both variables. The second method counts the number of dimensions. It returns 0 for variable1 and 1 for variable2. Please, study the documentation of the method where the difference between the two methods is described very thoroughly. More specifically, note the following distinction:
image.png
The isscalar() method returns False even for 0-dimensional arrays.

Hope this helps!

Kind regards,
365 Hristina

Submit an answer