Javatpoint Logo
Javatpoint Logo

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

Parameters

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

1) object: array_like

Any object, which exposes an array interface whose __array__ method returns any nested sequence or an array.

2) dtype : optional data-type

This parameter is used to define the desired parameter for the array element. If we do not define the data type, then it will determine the type as the minimum type which will require to hold the object in the sequence. This parameter is used only for upcasting the array.

3) copy: bool(optional)

If we set copy equals to true, the object is copied else the copy will be made when an object is a nested sequence, or a copy is needed to satisfy any of the other requirements such as dtype, order, etc.

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

The order parameter specifies the memory layout of the array. When the object is not an array, the newly created array will be in C order (row head or row-major) unless 'F' is specified. When F is specified, it will be in Fortran order (column head or column-major). When the object is an array, it holds the following order.

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()

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:

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

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

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

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

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA