Python | Numpy ndarray .__ array __ ()

NumPy | Python Methods and Functions

Using the ndarray.__array__() method, we can create a new array of our own by setting the parameter as dtype, and we can get a copy of the array that does not change the data element of the original array if we change any element in the new one.

Syntax: ndarray .__ array __ ()

Return:

  • Returns either a new reference to self if dtype is not given
  • New array of provided data type if dtype is different from the current dtype of the array.

Example # 1:

In this example, we we can see that we are changing the dtype of the new array simply using ndarray .__ array __ () .

# import important module in python

import numpy as np

 
# make an array with NumPy

gfg = np.array ([ 1 , 2 , 3 , 4 , 5 ])

  
# applying the ndarray .__ array __ () method

geeks = gfg .__ array __ ( float )

 

print (geeks)

< b> Output:

 [1. 2. 3. 4. 5.] 

Example # 2:

# import an important module into python

import numpy as np

 
# make an array with NumPy

gfg = np.array ([[ 1.1 , 2 , 3.3 , 4 , 5 ],

  [ 6 , 5.2 , 4 , 3 , 2.2 ]])

 
# applying the ndarray method .__ array __ ()

geeks = gfg .__ array __ ( int )

 

print (geeks)

Exit:

 [[1 2 3 4 5] [6 5 4 3 2]] 




Get Solution for free from DataCamp guru