Chapter 1 Python NumPy Blog 4
Performing Operations on NumPy Slicing / Joining / Arithmetic / Statistical
Learning Objectives:
* Developing the logical approach when and why to join the ndarray
* Getting to know the various methods used to join ndarray
* Understanding the impact of using the arguments within these methods
Text highlighted in blue colour to be pen down in the IP register along with the code.
Certain points/terms which should be clear before understanding the joining operation –
Rows & Columns of ndarray
Axis of a 2-D ndarray
Overview
1. numpy.concatenate(arr values [, axis] ) – is used to join more than one array along its axis.
arr values = the arrays which can be direct array values or array variables.
axis = is the numeric value (0 for row wise / 1 for column wise) along which the new joined array appears.
The default axis value is 0 (if we do not specify this keyword in the argument of the .concatenate( ) then the new joined array will be joined row wise.
** Each Array to be joined should be of the same dimension
Prog 1 1-D arrays Joined (In 1-D array there is only one axis)
import numpy as na
arr1=na.array([1,2,3])
arr2=na.array([4,5,6])
print("First array is \n", arr1)
print("Second array is\n", arr2)
joinarr=na.concatenate((arr1,arr2))
print("The joined array is \n", joinarr)
args = axis =0/1
0 means joining the arrays row wise joining (Default joining style)
Prog 3 1-D array with axis=0
(See the output is same as Prog1)
Prog 4 2-D array with axis =0
Prog 5
import numpy as na
arr1=na.array([[1,2],[3,4],[5,6]])
arr2=na.array([[11,12],[13,14],[15,16]])
arr3=na.array([[21,22],[23,24],[25,26]])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.concatenate((arr1,arr2,arr3), axis=0)
print("The joined array is \n", joinarr)
axis = 1 means joining the array column wise ( arrays should have more than 1 row)
Prog 6 1-D array with axis =1
** 1-D Array do have only 1 row so joining with axis=1 is not possible
Prog 7 2-D array with axis =1
Prog 8
What will be the output for the given values for .concatenate( )?
Array Type | Axis Value | Output |
1-D | X | Arrays will be joined row wise [array 1 array2 …..] One after the other |
1-D | 0 | Arrays will be joined row wise [array 1 array2 …..] |
1-D | 1 | Error – out of dimensions (only 1 row) |
1-D | 2 | Error – out of dimensions (only 1 row) |
2-D | 0 | Arrays will be joined row wise [array 1 array2 …..] One after the other |
2-D | 1 | Arrays will be joined column wise (elements of the columns will become the row of new joined array in the same order) |
2-D | 2 | ???? |
2. numpy.stack(arr values [, axis]) à function is used to join a sequence of same dimension arrays along a new axis.
Prog 1 1-D array
import numpy as na
arr1=na.array([1,2,3,4,5])
arr2=na.array([11,12,13,14,15])
arr3=na.array([21,22,23,24,25])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.stack((arr1,arr2,arr3))
print("The stacked array is \n", joinarr)
Prog 2 2-D Array
** The axis parameter specifies the index of the new axis in the dimensions of the result.
Prog 3 1-D array with axis=0 (stack) (row remains row)
import numpy as na
arr1 = na.array([ 1, 2, 3] )
print ("1st array : \n", arr1)
arr2 = na.array([ 4, 5, 6] )
print ("2nd array : \n", arr2)
# Stacking the two arrays along axis 0
newarr1 = na.stack((arr1, arr2), axis = 0)
print ("The Stacked array along axis 0:\n", newarr1)
# Stacking the two arrays along axis 1
newarr2 = na.stack((arr1, arr2), axis = 1)
print ("The stacked array along axis 1:\n", newarr2)
Prog 4 1-D Arrays joining with axis=1 (stack) row becomes column
import numpy as na
arr1=na.array([1,2,3,4,5])
arr2=na.array([11,12,13,14,15])
arr3=na.array([21,22,23,24,25])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.stack((arr1,arr2,arr3), axis=1)
print("The stacked array is \n", joinarr)
Stacking for 1-D array with axis value as 2 or above will return an error (no such dimension)
2-D/ M-D array stacking
** The axis parameter specifies the index of the new axis in the dimensions of the result.
Prog 1 ( axis=0 / no axis specified so the arrays will appear one after the other ) row remains row
import numpy as na
arr1=na.array([['1','Dev'],[2,'Bunny']])
arr2=na.array([['3','Dhruv'],[4,'Mella']])
arr3=na.array([['5','Angad'],['6','Bahu']])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.stack((arr1,arr2,arr3), axis=0)
print("The joined array is \n", joinarr)
Prog 2 2D arrays stacked (axis=1)
import numpy as na
arr1=na.array([['1','Dev'],[2,'Bunny']])
arr2=na.array([['3','Dhruv'],[4,'Mella']])
arr3=na.array([['5','Angad'],['6','Bahu']])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.stack((arr1,arr2,arr3), axis=1)
print("The stacked array is \n", joinarr)
When 2-D arrays are stacked with axis value 1 then the new array is stacked column wise. ( respective columns of each array is stacked as one array )
Prog 3 2D arrays stacked (When axis=2)
import numpy as na
arr1=na.array([['1','Dev'],[2,'Bunny']])
arr2=na.array([['3','Dhruv'],[4,'Mella']])
arr3=na.array([['5','Angad'],['6','Bahu']])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.stack((arr1,arr2,arr3), axis=2)
print("The stacked array is \n", joinarr)
When axis = 2 then the arrays are stacked column wise ( elements of the columns of all the arrays are represented as in row)
What will be the output for the given values for .stack( )?
Array Type | Axis Value | Output |
1-D | X |
|
1-D | 0 |
|
1-D | 1 |
|
1-D | 2 |
|
2-D | 0 |
|
2-D | 1 |
|
2-D | 2 |
|
3. numpy .hstack( arr values ) – this function is used to stack arrays in horizontal sequence. This is equivalent to concatenation along the second axis, except for 1-D arrays where it concatenates along the first axis.
Prog 1 1-D hstack( )
import numpy as na
arr1=na.array([1,2,3,4,5])
arr2=na.array([11,12,13,14,15])
arr3=na.array([21,22,23,24,25])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.hstack((arr1,arr2,arr3))
print("The horizontally stacked array is \n", joinarr)
Prog 2 2-D hsatck( )
import numpy as na
arr1=na.array([['1','Dev'],[2,'Bunny']])
arr2=na.array([['3','Dhruv'],[4,'Mella']])
print("First array is \n", arr1)
print("Second array is\n", arr2)
joinarr=na.hstack((arr1,arr2))
print("The horizontally stacked array is \n", joinarr)
Prog 3 2-D hstack( )
import numpy as na
arr1=na.array([['1','Dev'],[2,'Bunny']])
arr2=na.array([['3','Dhruv'],[4,'Mella']])
arr3=na.array([['5','Angad'],['6','Bahu']])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.hstack((arr1,arr2,arr3))
print("The horizontally stacked array is \n", joinarr)
4. numpy. vstack( arr values) - function is used to stack arrays in a vertical sequence (row wise). vstack( ) is equivalent to concatenate( ) along the first axis after 1-D arrays of shape (N) have been reshaped to (1, N).
Prog 1 1-D vstack( )
import numpy as na
arr1=na.array([10,20,30,40,50])
arr2=na.array(['a','b','c','d','e'])
print("First array is \n", arr1)
print("Second array is\n", arr2)
joinarr=na.vstack((arr1,arr2))
print("The vertically stacked array is \n", joinarr)
Prog 3 2-D vstack( )
import numpy as na
arr1=na.array([['1','Dev'],[2,'Bunny']])
arr2=na.array([['3','Dhruv'],[4,'Mella']])
arr3=na.array([['5','Angad'],['6','Bahu']])
print("First array is \n", arr1)
print("Second array is\n", arr2)
print("Third array is \n", arr3)
joinarr=na.vstack((arr1,arr2,arr3))
print("The joined array is \n", joinarr)
Questions to be done in the notebook -
1. Which method(s) of numpy may be used to arrange arrays row wise?
2. Which method of numpy may be used to arrange arrays column wise (without using any arguments)?
3. How will the 1-D array be stacked when supplied with axis value as 2 ?
4. Fill in the table given below –
Method Name | Array Type | Axis Value | Output |
.stack( ) | 1-D | 2 |
|
.concatenate( ) | 2-D | 1 |
|
.stack( ) | 2-D | 1 |
|
.concatenate( ) | 1-D | 2 |
|
No comments:
Post a Comment