Chapter 1 Python NumPy Blog 3
Performing Operations on NumPy Slicing / Joining / Arithmetic / Statistical
Learning Objectives:
* Developing the logical approach when to extract an element
* Extracting a subset or a grid from an existing Array
* How to add/ change / delete an element or a subset of the array
Text highlighted in blue colour to be pen down in the IP register along with the code.
Operations On NumPy
1. Slicing
2. Joining
3. Arithmetic
4. Statistical
SLICING
Extracting an element or a set of elements from an existing 1D array or a subset of elements from a 2D array is called Slicing.
Ways in which slicing can be performed
1. Operator ':'
2. Slice( )
Operator ':'
Syntax array_name[ start : stop : step] or array_name[ from: to: jump]
The slice returns the data from the array from the 'start' index and ends one element before the 'stop' index.
And if step value is mentioned then it step up that particular order of the data from the start index address.
In the above example nums is an array with data 10 to 90
Now in the code line 4 some_nums[2:7] means data stored at index address 2 to index address 7 will be sliced/extracted.
In the above code line In[5]:
10 20 30 40 50 60 70 80 90
0 1 2 3 4 5 6 7 8
start value = 0 = start index address of slicing = 0
stop value = 9 = stop index address of slicing = 9-1 = 8
so slicing will be done on index range from 0 to 8
that is exactly our array [10 20 30 40 50 60 70 80 90]
Q^ WAP in NumPy to show the first 5 elements form the array of first 10 natural numbers.
Q^ WAP in NumPy to show the first 9 elements from the array of first 10 odd natural numbers.
Q^ Now, WAP to accept the limit value 'n'(<50) from user to show the first n elements of multiples of '5'.
Q^ WAP to accept the limit value 'n'(<20) from user to show the first n elements of multiples of '11'.
Negative Index Slicing
Always remember the stop value will show data/element up to one previous index address
Here we gave the slicing range index as [-11 to -6] but showed the value/element up to index -7.
Because the stop index value is always 1 less than the stop value= -6 -1 = -7
P.S. Negative indexes allow us to easily take n-last elements of a NumPy array.
The below code extracts the last three elements of the first 10 natural numbers.
Q^ WAP in NumPy to extract and show the last 5 elements of an array which holds first 10 even natural numbers.
Q^ WAP in NumPy to extract and show the last 3 elements of an array which holds multiple of 12. (upto 20th multiples of 12)
P.S. Negative indexes also allow us taking all the elements but not the 'n' last elements .
Q^ WAP in NumPy to extract and show other than the last 3 elements of an array which holds first 10 natural numbers.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgl8WXG7oytON-b3mXmdX5eqGCKWJNDZqDZkfT3HeDUgNsiW4Y9-0azV7dV7-Roy-hKQAekDUEO4O8590x6yYI0T8wwIdNrhAaGac3_cskNkcyesEEZRdob9krx231f1Pl2aNlKTbkpsd7C/w640-h282/Screenshot+2020-05-14+at+10.58.41+PM.png)
Start Value = default (0)
Stop Value = -3 (-3 -1 = -4)
Start Value = -10 (as stop value is negative index)
Slice array value range = from -10 index address to - 4
Q^ WAP in NumPy to extract and show other than the last 6 elements of an array which holds first 10 odd natural numbers.
Q^ WAP in NumPy to extract and show other than the last 2 elements of an array which holds first 10 even natural numbers.
Q^ Now, WAP to accept the limit value 'n'(<20) from user to show other than the last n elements of multiples of '100' (limit upto 20 = 100x1 ..... 100x20)
Some advanced functionality of slicing operator
Syntax --> array_name[ start : stop : step ] or array_name[ from : to : jump]
step value is if mentioned then while slicing from the array the specified step value becomes the positional order in the array which will be skipped up from the start index address value.
** Where step is not an index address
In the above example a[ : : 2 ] means the start value and stop value are blank (which means the extraction of data will be from index address '0' and till the last index address 9)
But the data at each 2nd position (not the index address) in the array 'a' will not be displayed rather stepped up / jumped over.
In the above example a[1::2] means start value is 1 which is the index address 1 and the value stored at index address is 22
stop value is not mentioned (there is no number between the 2 semicolon) so till the last index address value will be read.
step value is 2 which means every 2nd position (alternate) data will be skipped from the start value which is 22 so the data skipped will be the data after 22 then the alternate and so on.
P.S. When some of the last elements need not be extracted and the first few elements to be extracted on a particular gap.
start value =index address = 1 so start data value = 2
stop value = index address = -3 so stop data value = 7 (at -4)
step value = 2 = every alternate data from the start data value
start from 2 leave next 4 leave next 6 stop data value
output = [ 2 4 6 ]
Q^ WAP in NumPy to extract the data stored at index address 3 and 6 only.
We can use a negative step value to obtain a reversed list:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK7ydekVEmdJdbgucz7S84ppS4tMRsSzxZMJP6E6bzBt1eI-wb8m3_zY94tY0ippR-cfVSwaNb63fl77TZeNOJGbZvOOld9tO-BtZf3p52r2LmGxgrt4YbIbQv0ZFLXc4FiRKIdilbyDFK/w640-h274/Screenshot+2020-05-15+at+12.11.04+AM.png)
Start at index 4 so start value = 5
End index = not given
step value = -1 means reverse from the start value
(towards the left ) So [ 5 4 3 2 1 ]
2. slice( )
The slice( ) function returns a slice object that can be used to slice an array.
Syntax slice( start, stop, step)
start (optional) - Starting integer where the slicing of the object starts. Default to None if not provided
stop - Integer until which the slicing takes place. The slicing stops at index stop -1 (last element)
step (optional) - Integer value which determines the increment between each index for slicing. Default to None if not provided
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj34TzcIitCpluGwuuytr78FuLvfITokCvtavdi1qREZJqcoMzyhXJW9izZb2m0LAv1GAFYKVdMUAakaHQn9Ax_ugfiDTYw9rI4vU_HrV1USMhv62pKAx5EDfkmGYjD1lOOx-yedWWWZKIo/w640-h238/Screenshot+2020-05-15+at+9.51.38+AM.png)
A | R | M | A | G | E | D | D | O | N |
-10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Start |
|
|
| Stop |
| x |
|
| x |
Step to 3 | Step to 1 | Step to 2 | Step to 3 | Step to 1 | Step to 2 | Step to 3 | Step to 1 | Step to 2 | Step to 3 |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxd7UvyNx-4oT1Gt8ryCNDcbpTYxnsmeCzsett0Y_PR6mdOpYFuVC9BLDJfQyc8uI_o2Cp0nOsuPtshq2OYFnJo7vgMsoKZU3Ws0Mn3K0YCaPJQ6g3VvdYlEKdnbqLyGGZOREOZqbjYBDp/w640-h290/Screenshot+2020-05-15+at+11.48.22+AM.png)
No comments:
Post a Comment