hi i am unable to understand the concept of shape
What does this line mean Some data creation routines do not take a shape tuple:
Hello @SantoshKumarDoodala , welcome to the community!
When we want to know the shape of a numpy array (or vector), we use the array.shape
function. The shape of an array is nothing more than its dimensions. The example you circled is an array with shape = (4, ), that is, a 1D array. When there is nothing after the comma, it’s the same as having a 1. That’s why the shape (dimension) of this array is 1D.
I hope it was helpful!
there is also a statement
# NumPy routines which allocate memory and fill arrays with value but do not accept shape as input argument.
going by this statement numpy routine being np.arange(4,);
- it alloacates memory and fill array with values,
there is this line but do not accept shape as input argument, could you please explain what does this mean, it would really helpful.
Thank you
Any help pls for this thread
Hello @SantoshKumarDoodala,
Shape talks about two things, how many dimensions (or axes) your array has, and what are the sizes of each dimension.
If you have an array of 2 rows and 3 columns, the shape is (2, 3)
, or when the shape of your array is (6, 2)
, it has 6 rows and 2 columns. Because each of the shapes has 2 numbers, they are both 2-dimension arrays. For a 2-dimension array, you can imagine it as a table, and the shape talks about the size of such table.
Back to the lab,
Here we talk about how an array can be initialized in numpy. For some numpy functions, we can specify a shape so that numpy can initialize an array that has the exact same shape, for example, np.zeros( (3, 2) )
will initialize a 2D array with 3 rows and 2 columns of all zeros, because np.zeros
allows us to specify a shape and we specified (3, 2)
.
I suggest you to try to change the shape/non-shape inputs to those numpy functions and see if your understanding is consistent with the outputs. If you have question, google “numpy {function name}” then you should be able to find the relevant numpy official documentation that talks about how the numpy function in question works.
Raymond
Hello @rmwkwok thank you for the reply.
I think I am understanding a bit but is it that a = np.arange(4.);
is the code where we can’t specify shape like ‘a = np.arange(4,5);’–> because this is incorrect.
Also I wanted to try the code with my values but I am unable to do so, I think they are hard coded or something like that
for ex: when I Change the input to 3. for np.arange
there is no effect
I suggest you to follow these steps for each numpy function that you want to try out.
-
Google “np.{function name}”. For example, for np.arange, google “np.arange”. Below is the official documentation googled, and it usually shows up in the first few places. Click into it.
-
Read what it does:
-
Read the definition for parameters, and determine whether you can put in a shape or not. For
np.arange
, it requires you to put in NOT A SHAPE, but at least an interval which is denoted by a start and an end. I suggest you to read through the page.
-
If you scroll down, it usually (not always) has some examples to show what it returns given an input.
-
With this understanding, you can go back to the jupyter notebook. The code cell is not hard-coded., in your screenshot
Below the line # NumPy routines which allocate memory and ...
, change the 9, 5
inside np.arange(...)
into 1, 10
.
After you finished making your change, press “Ctrl + Enter” to run the currently active cell, and you should see some changes in the output. Note that, before you press “Ctrl + Enter”, don’t use your mouse to click anywhere to make the code cell inactive. “Ctrl + Enter” only works for the currently active code cell.
Once this works, try out other configurations. Also, I suggest you to try out the examples in the numpy documentation.
Raymond