Array Creation in NumPy.
We will create arrays in different ways in the Python library NumPy and understand the creation process.
1. np.array()
It will help us to create a multi-dimensional or single-dimensional array, this is the simple way to create an array using NumPy, we can consider it primary.
Structure.
Output.
2. np.zeros()
np.zeros() takes the array dimensions as parameters and returns the value 0 for each.
Structure.
Output.
3. np.arange()
np.arange() takes three parameters. The first parameter specifies the starting point, the middle parameter specifies the ending point, and the last parameter specifies the space or gap between the numbers.
Structure.
- Here we are printing a table of five where we set the starting point to 5, if we set it to 0 then obviously it will start from zero, and like [0, 5, 10, 15, ...] will be printed. Which doesn't look very appropriate in a math table.
- Then the middle value is 51 because this parameter checks that the value is less than, not equal. ie, if we write 50 instead of 51, the array will stop at 45 because 50 is not less than 50, but if we write values between 51 - 55, it will be perfect because of the difference of 5 between each value.
Output.
- If we want to add 55 to the array, we need to set the middle parameter between 56 - 60. Then it will be like this.
Structure.
Output.
4. np.linspace()
np.linspace() also takes three parameters, the first for start, the second for stop, and the third is the length. But its calculation is not the same as np.arange(), it divides the second parameter with the last parameter and keeps adding the last value of the array with the new value until it gets the value of what we have given to stop.
Structure.
- Here we are printing a table of 2 where we have set the starting point 2, ending point 20, and length (num) 10. Now it will calculate like this.
- First, it will divide 20 by 10 20/10 and print 2, then again divide 20 by 10 20/10 but this time it will add the last value which is 2 thus 20/10+2=4 and will do the same again and this time adds 4 thus 20/10+4=6 and it will continue to perform until the value is 20.
Output.
- Sometimes it shows value in float so to fix that you can set dtype=int then it will be perfect but sometimes we need float also.
5. np.empty()
np.empty() takes array dimensions as parameters and prints random values in it. This will be more useful when you want an array immediately.
Structure.
- An integer document type would be a more comfortable choice for this.
Output.
6. np.empty_like()
np.empty_like() takes an array variable as a parameter and creates another array with the same structure with random values.
Structure.
Output.
- If arr01 looks like this.
- So this will also look the same but with random values. and it will print like this in the output
7. np.identity
np.identity() takes only 1 parameter for the length and creates an array like the identity matrix.
Structure.
Output.
1 Comments
Testing cloud computing course:
ReplyDeleteAWS Certification
https://www.youtube.com/playlist?list=PL6XT0grm_TfgtwtwUit305qS-HhDvb4du
GCP TG
https://www.youtube.com/playlist?list=PLBGx66SQNZ8YWRUw6yicKtD4AIpUl_YiJ