Numpy array() Function



The Numpy array() function is used to create a array by converting input data. The input data can be a list, tuple, string, or other iterable. If the input is a scalar, the function returns a 0-dimensional array. For example, passing 90 as input will create a 0-dimensional array. This function returns multi-dimensional numpy array.

A 0 dimensional array in Python, used in libraries like Numpy, is an array structure that contains a single value and has no axes or dimensions, represented by a shape of ().It behaves like a scalar but retains all properties of an array.

Syntax

Following is the syntax of the Numpy array() function −

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

Parameters

Following are the parameters of the Numpy array() function −

  • object:This is the required input parameter. It can be any array-like object (like a list, tuple, or another NumPy array) that we want to convert into a NumPy array.
  • dtype: It specifies the desired data type of the array elements (e.g., int, float, complex, str). If not specified, NumPy will automatically consider the data type based on the input object.
  • copy (optional): If True(default value), a new copy of the object is always created, even if the object is already a NumPy array. If False, the function will try to avoid copying the data and just create a view of the input object if possible.
  • subok (optional): If True, subclasses of ndarray are passed through. If False(default value), the output will always be a base-class ndarray. The default value is True.
  • ndmin (optional): It specifies the minimum number of dimensions for the resulting array. If the input array does not have the required number of dimensions, additional dimensions are prepended to make up the difference. The default value is 0.
  • like (optional): It allows the creation of an array which is like the input object but uses an existing array-like object (like another NumPy array).
  • order (optional): It specifys the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless F is specified, in which case it will be in Fortran order (column major) −
  • 'C': C-style row-major order.
  • 'F': Fortran-style column-major order.
  • 'A': 'F' if the input is Fortran contiguous, 'C' otherwise.
  • 'K': This is the default value keep the order as close as possible to the input.

Return Type

This function return ndarray when an object such as list, tuple or set is passed as an argument.

Example

Following is a basic example to create a numpy array using Numpy array() function −

import numpy as np
my_Array = np.array([13,25,53,94,85])
print("Array:",my_Array)

Output

Following is the output of the above code −

Array: [13 25 53 94 85]

Example : Creating Array from Sequences

A NumPy array can be created from sequences such as lists, tuples, or sets by passing them as arguments to the numpy.array() function.

Here, we have passed my_list as an argument to the numpy.array(), which resulted an array −

#importing numpy module 
import numpy as np
#defined list
my_list=[20,30,40,50]
print(type(my_list))
#passed list an argument
my_Array = np.array(my_list)
#printing numpy array
print("Array:",my_Array)
print(type(my_Array))

Output

Following is the output of the above code −

<class 'list'>
Array: [20 30 40 50]
<class 'numpy.ndarray'>

Example : Creating an numpy array Using Scalar

The numpy.array() function returns 0-dimensional array when the scalar value is passed as an argument.

In the following example, we have passed 75, which is a scalar value in the numpy.array() function as an object −

#importing numpy module 
import numpy as np
#defined scalar
my_int = 75
print(type(my_int))
#passed scalr an argument
my_Array = np.array(my_int)
#printing numpy array
print("Array:",my_Array)
print(type(my_Array))

Output

Following is the output of the above code −

<class 'int'>
Array: 75
<class 'numpy.ndarray'>

Example: Creating N-Dimensional NumPy Array

The ndmin argument in the numpy.array() function is used to specify the minimum number of dimensions for the resulting NumPy array. For example, to create a 3-dimensional array, set ndmin to 3 −

#importing numpy module 
import numpy as np
#defined list
my_list1=[2,8,9,6]
my_list2=[12,38,79,56]
my_list3=[100, 200, 300, 400]
#passed list an argument
my_Array = np.array([my_list1, my_list2, my_list3],ndmin=3)
#printing numpy array
print("3D Array -")
print(my_Array)

Output

Following is the output of the above code −

3D Array -
[[[  2   8   9   6]
  [ 12  38  79  56]
  [100 200 300 400]]]
numpy_array_creation_routines.htm
Advertisements