numpy.array() in Python

The homogeneous multidimensional array is the main object of NumPy. It is basically a table of elements which are all of the same type and indexed by a tuple of positive integers. The dimensions are called axis in NumPy.

The NumPy's array class is known as ndarray or alias array. The numpy.array is not the same as the standard Python library class array.array. The array.array handles only one-dimensional arrays and provides less functionality.

Syntax

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

Parameters

There are the following parameters in numpy.array() function.

1) object: array_like

2) dtype : optional data-type

3) copy: bool(optional)

4) order : {'K', 'A', 'C', 'F'}, optional

order no copy copy=True
'K' Unchanged F and C order preserved.
'A' Unchanged When the input is F and not C then F order otherwise C order
'C' C order C order
'F' F order F order

When copy=False or the copy is made for the other reason, the result will be the same as copy= True with some exceptions for A. The default order is 'K'.

5) subok : bool(optional)

When subok=True, then sub-classes will pass-through; otherwise, the returned array will force to be a base-class array (default).

6) ndmin : int(optional)

This parameter specifies the minimum number of dimensions which the resulting array should have. Users can be prepended to the shape as needed to meet this requirement.

Returns

The numpy.array() method returns an ndarray. The ndarray is an array object which satisfies the specified requirements.

Example 1: numpy.array()

snippet
import numpy as np
arr=np.array([1,2,3])
arr

Output:

Output
array([1, 2, 3])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the 'arr' variable and assigned the value returned by np.array() function.
  • In the array() function, we have passed only the elements, not axis.
  • Lastly, we have tried to print the value of arr.

In the output, an array has been shown.

Example 2:

snippet
import numpy as np
arr=np.array([1,2.,3.])
arr

Output:

Output
array([1., 2., 3.])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the 'arr' variable and assigned the value returned by np.array() function.
  • In the array() function, we have passed elements of different type such as integer, float, etc.
  • Lastly, we have tried to print the value of arr.

In the output, an array has been displayed containing elements in such type which require minimum memory to hold the object in the sequence.

Example 3: More than one dimensions

snippet
import numpy as np
arr=np.array([[1,2.,3.],[4.,5.,7]])
arr

Output:

Output
array([[1., 2., 3.], [4., 5., 7.]])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the 'arr' variable and assigned the value returned by np.array() function.
  • In the array() function, we have passed the number of elements in different square brackets.
  • Lastly, we have tried to print the value of arr.

In the output, a multi-dimensional array has been shown.

Example 4: Minimum dimensions: 2

snippet
import numpy as np
arr=np.array([1,2.,3.],ndmin=2)
arr

Output:

Output
array([[1., 2., 3.]])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the 'arr' variable and assigned the value returned by np.array() function.
  • In the array() function, we have passed the number of elements in a square bracket and the dimension to create a ndarray.
  • Lastly, we have tried to print the value of arr.

In the output, a two-dimensional array has been shown.

Example 5: Type provided

snippet
import numpy as np
arr=np.array([12,45.,3.],dtype=complex)
arr

Output:

Output
array([12.+0.j, 45.+0.j, 3.+0.j])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the 'arr' variable and assigned the value returned by the np.array() function.
  • In the array() function, we have passed the elements in the square bracket and set the dtype to complex.
  • Lastly, we have tried to print the value of arr.

In the output, the values of the 'arr' elements have been shown in the form of complex numbers.

Example 6: Creating an array from sub-classes

snippet
import numpy as np
arr=np.array(np.mat('1 2;3 4'))
arr
arr=np.array(np.mat('1 2;3 4'),subok=True)
arr

Output:

Output
array([[1, 2], [3, 4]]) matrix([[1, 2], [3, 4]])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the 'arr' variable and assigned the value returned by the np.array() function.
  • In the array() function, we have passed the elements in the form of the matrix using np.mat() function and set the subok=True.
  • Lastly, we have tried to print the value of arr.

In the output, a multi-dimensional array has been shown.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +