Array Basics

import numpy as np

Create Arrays

The basic array type in NumPy is ndarray. It is also known by the alias array. So when we talk about array, we are referring to the ndarray unless specified.

An ndarray object stores a matrix, every elements of which have the same data type. The matrix is indexed by a tuple of non-negative integers.

Dimensions of matrices are called axes (axis).

np.array(
    object, 
    dtype = None, 
    copy = True, 
    order = None, 
    subok = False, 
    ndmin = 0
)
"""
#pointer, dtype, shape, stride
#object: 
    array; 
    object exposing the array interface;
    object whose __array__ returns an array
    nested list
#dtype: data type
#copy: if this is a copy
"""

a = np.array([[1,2],[3,4]])
b = np.array([1+1.j,2+1.j],dtype = np.complex)  # a complex array

# use function of coordinates to create an array from a given shape
np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
array([[ True, False, False],
       [False,  True, False],
       [False, False,  True]])

Properties

  • data types for np:

    • bool

    • int8 int16 int32 int64

    • uint8 uint16 uint32 uint64

    • float float16 float32 float64

    • complex64 complex128

Special Arrays

Notice that following methods all return array objects.

Indexing, Slicing & Iterating

Arithmetic Operations

See also Python - Emulating numeric types.

Reference

[1] https://numpy.org/devdocs/user/quickstart.html

Last updated

Was this helpful?