Explore array(), type() and astype() functions
NUMPY - array(), type() and astype() functions

Explore array(), type() and astype() functions

What is ndarray in Numpy?

"ndarray" stands for n-dimensional array. Numpy creates an object for an array in this type.


How to create an array in Numpy?

We can create an array using the built-in Numpy function called numpy.array() function.

Example

import numpy as np
array_1 = np.array([1,2,3,4,5,6], dtype='i', ndmin=0)
print(array_1)        

Output

[1 2 3 4 5 6]        

In the above example, we create an array with list of object, integer datatype and number of dimensions.

  • object - It can used to pass the elements of list or tuple in an array.
  • dtype - It is used to define the datatype of an array.
  • ndmin - It is used to define the higher dimensions of an array.


type() function

It is used to show the type of the object passed in it.

Example

print(type(array_1))        

Output

<class 'numpy.ndarray'>        


astype() function

It is used to create a copy of an particular array with specified datatype.

Example

array_2 = array_1.astype(dtype='f')
print(array_2)        

Output

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

Note: Some of the datatypes are integer('i'), float('f'), boolean('b') and string('S').


Some of the attributes of Numpy

  • dtype - It is used to check the datatype of an element present in an array.
  • ndim - It is used to check the number of dimensions of an array.
  • nbytes - It is used to check the number of bytes of an array.

Example

print("Array 1 - dtype, ndim, nbytes")
print(array_1.dtype)
print(array_1.ndim)
print(array_1.nbytes)


print("Array 2 - dtype, ndim, nbytes")
print(array_2.dtype)
print(array_2.ndim)
print(array_2.nbytes)        

Output

Array 1 - dtype, ndim, nbytes
int32
1
24

Array 2 - dtype, ndim, nbytes
float32
1
24        

Pro tip: Google Colaboratory is a cloud platform makes us easy to use and run anywhere.

Follow on LinkedIn - Trillionaire TK

Follow on Instagram - Trillionaire TK

Visit our website - Information Technology

Learn, Code, Practice!!!

To view or add a comment, sign in

More articles by Tharunkumar S

  • Comparison Operations in Numpy Arrays

    Comparison Operations in Numpy As we already know the comparison or relational operations like less than, less than and…

  • Arithmetic Operations in Numpy Arrays

    Arithmetic Operations in Numpy As we already know the arithmetic operations like addition, subtraction, division…

  • Shape of an Array and Reshape() function

    Shape of an Numpy array In Numpy array, shape is an attribute that is used to check the dimension of an array. shape…

  • Slicing in Numpy Arrays

    What is Slicing in Numpy Array? Slice the array in a particular range of elements. In other words, Make an sub array…

  • Indexing in Numpy Arrays

    What is Indexing in Numpy Array? Access the elements in a Numpy array based on index values. Accessing the elements in…

  • Introduction to Numpy Library in Python

    Introduction NUMPY stands for Numerical Python. Numpy is one of the libraries for the python programming language.

  • Journey of Machine Learning Begins

    Machine Learning Definition : The term Machine Learning was coined by Arthur Samuel in 1959, an American pioneer in the…

Explore content categories