Signup/Sign In
LAST UPDATED: DECEMBER 7, 2020

NumPy array() function

In this tutorial, we will cover the concept of array() function in the NumPy library.

The array() function in the NumPy library is mainly used to create an array. Just like the Numpy arange() function.

  • In the NumPy library the homogeneous multidimensional array is the main object. Homogeneous multidimensional array is basically a table of elements and these elements are all of the same type and indexed by a tuple of positive integers. The dimensions of an array are called as axis in NumPy.

  • The array class in the Numpy library is mainly known as ndarray or an alias array.

  • The numpy.array() is not same as the standard Python library class array.array because in python array.array is used to handle only one-dimensional arrays and thus it provides less functionality.

Syntax of numpy.array():

The syntax required to use this method is as follows:

numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0) 

Parameters:

Let us now take a look at the parameters of this function:

  • object
    This parameter indicates an array or it can be any object exposing the array interface mainly it can be an object whose __array__ method returns an array, or any nested sequence.

  • dtype
    This parameter defines the desired data-type for the array. In case if this parameter is not given, then the type will be determined as the minimum type required to hold the objects in the sequence.

  • copy
    If value of this parameter is set to True, the object will be copied else the copy will be made only when an object is a nested sequence, or if a copy is just needed to satisfy any of the other requirements such as dtype, order, etc.

  • order
    This prameter is used to specify the memory layout of the array. In case if the object is not an array then the newly created array will be in C order that is row major unless 'F' is specified, in that case it will be in Fortran order that is column order. If the object is mainly an array then the following holds:
Order No Copy copy=true
'K' unchanged If copy is true in this case then F & C order preserved otherwise most similar order
'A' unchanged In this case if copy is true and input is F not C then the order is F otherwise the order is C
'C' C order In this case the order is C
'F' F order In this case the order is F.
  • subok
    This is an optional parameter, when the value of this parameter is True, then sub-classes will pass-through otherwise by default the returned array will force to be a base-class array.

  • ndmin
    It is an optional parameter that is used to specify the minimum number of dimensions that the resulting array should have.

Returned Values:

The array() function in the numpy library is used to return an array object that satisfies the specified requirements.

Example 1: Basic example of array() function

Below we have a basic example where we create an array having only one dimension with the help of array() function:

import numpy as np  

a = np.array([1,4,9])  
print("The Array is:")
print(a)


The Array is:
[1 4 9]

Example 2: Multi-dimension array

Now we will create an array having more than one dimension and the code for the same is as follows:

import numpy as np  

a = np.array([[1, 7], [6, 4]])  
print("The Array with more than one dimension:")
print(a)


The Array with more than one dimension:
[[1 7]
[6 4]]

Example 3: Using the dtype Parameter

Below we have an example where we will use the dtype parameter:

import numpy as np  

a = np.array([1, 7,9], dtype='complex')  
print("The Array with more than one dimension:")
print(a)


The Array with more than one dimension:
[1.+0.j 7.+0.j 9.+0.j]

Note: The output of the above code snippet indicates the values of the array elements in the form of complex numbers.

Summary

This tutorial was all about array() function in the Numpy Library. We have covered how it is used with its syntax, its parameters and values returned by this function. In the coming tutorial pages we will cover more such functions.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.