Numpy Ndarray Operation

Numpy Ndarray Operation

 

In last article we learnd about the basic of Numpy array and creation of ndarray object. In this article we are going to learn about the various operation on ndarray.

If you are new to numpy array then check previous article 

Follwing is the operation that can be performed on ndarray :

  • Creating empty array :

 import numpy as np

 x = np.array([ ]) # array creation using list
 y = np.array(( )) # array creation using tuples
 z = np.array({ }) # array creation using dictionary
 print (x)
 print (y)
 print (z)

Output :

 []
 []
 {}

 

  • Creating one dimensions array

 import numpy as np

 x = np.array([1,2,3]) # array creation using list
 y = np.array((4,5,6 )) # array creation using tuples
 z = np.array({'Name': 'solutionstouch', 'Founded': 2019}) #array creation using dictionary
 print (x)
 print (y)
 print (z)

 

Output: 

 [1 2 3]
 [4 5 6]
 {'Founded': 2019, 'Name': 'solutionstouch'}

 

  • Creating 2-dimensions array

 import numpy as np

 x = np.array([[1,2],[3,4]]) # array creation using list
 y = np.array(((5,6),(7,8))) # array creation using tuples
 z = np.array({'Name': 'solutionstouch', 'Founded': 2019}) #array creation using dictionary
 print (x)
 print (y)
 print (z)

Output :

 [[1 2]
  [3 4]]
 [[5 6]
  [7 8]]
 {'Founded': 2019, 'Name': 'solutionstouch'}

 

  • Creating ndarray with dtype parameter :

 import numpy as np
 x = np.array([11, 25, 68], dtype = float) # Float datatype
 y = np.array([11, 25, 68], dtype = complex) # Complex datatype
 z = np.array([11.0, 25.0, 68.0], dtype = int) # integer datatype

 print (x)
 print (y)
 print (z)

Output :

 [ 11. 25. 68.]
 [ 11.+0.j 25.+0.j 68.+0.j]
 [11 25 68]

 

  •  Creating ndarray with the use of copy parameter

Copy parameter Return an array copy of the given object.

 import numpy as np
 x = np.array([11, 22, 33]) #create an array.
 y = x #y is a reference variable
 z = np.copy(x) #copy of a x save in z

 x[0] = 10
 print(x[0] == y[0]) # output will be True
 print(x[0] == z[0]) # Output will be False because z having the initial value of x

 

Output:

 True
 False

 

  • Creating ndarray with the use of ndmin parameter

Ndmin means minimum dimensions of the resulting array.

 import numpy as np
 x = np.array([11,22,33,44], ndmin = 2) #ndmin is 2
 y = np.array([11,22,33,44], ndmin = 4) #ndmin is 4
 z = np.array([11,22,33,44], ndmin = 6) #ndmin is 6
 print(x)
 print(y)
 print(z)

 

Output: 

 [[11 22 33 44]]
 [[[[11 22 33 44]]]]
 [[[[[[11 22 33 44]]]]]]

 

Happy coding.....

 

  • Please give your suggestion if you find anything incorrect. contact us at team@bitsolve.in