NumPy ndarray: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 14: Line 14:
import numpy as np
import numpy as np


a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a = np.array([[1, 2, 3], [4, 5, 6]])


a.shape
a.shape
(2, 3, 2)
(3, 2)


a.dtype
a.dtype

Revision as of 20:08, 20 May 2024

Internal

Overview

ndarray is an N-dimensional array object. It is a fast, flexible container for large datasets in Python. It is used to implement a Pandas Series. It allows performing mathematical operations on whole blocks of data using similar syntax to the equivalent operation between scalar elements. It also allows applying same mathematical operation, or function, to all array elements without the need to write loops. Examples are provided in Array Arithmetic section.

ndarrays are homogeneous, all elements of an ndarray instance have the same data type. The data type is exposed by the array's dtype attribute. The array's dimensions are exposed by the shape attribute.

ndarrays can be created by converting Python data structures, using generators, or initializing blocks of memory of specified shape with specified values. Once created, array sections can be selected with indexing and slicing syntax.

ndarray Geometry

Each array has a shape tuple that indicates the sizes of each dimensions, and a dtype object that describes the data type of the array.

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])

a.shape
(3, 2)

a.dtype
dtype('int64')

ndarray Creation

Convert Python Data Structures

The np.array() function takes Python data structures, such as lists, lists of list, tuples, etc. and generates the corresponding shape ndarray. For example, a bi-dimensional 3 x 3 ndarray can be created by providing a list of 3 lists, each of the enclosed lists containing 3 elements:

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

The Python data structures provided as arguments to np.array() are interpreting according to the array's geometry.

With Generators

By Specifying Shape and Value

Element Data Type

Array Indexing and Slicing

Array Arithmetic

Transposing Arrays

Swapping Axes

Universal Functions

Universal Functions

Array-Oriented Programming

Conditional Logic as Array Operations

Mathematical and Statistical Operations

Sorting

Linear Algebra

File Input/Output with Arrays