NumPy-Übersicht

to create NumPy ndarray objects

NumPy is used to handle arrays. The array objects in NumPy are called ndarray.

We can use array() function creates a NumPy ndarray object.

Example

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

Run Example

type(): This built-in Python function tells us the type of the object passed to it. Like the above code, it indicates arr is numpy.ndarray type.

to create ndarray, we can pass a list, tuple, or any similar array object to array() Method, then it will be converted to ndarray:

Example

Create a NumPy array using a tuple:

import numpy as np 
arr = np.array((1, 2, 3, 4, 5))
print(arr)

Run Example

Array dimensions

The dimensions in the array are a level of array depth (nested arrays).

Nested arrays:refers to an array as an element array.

0-D array

0-D array, or scalar (Scalars), are the elements of the array. Each value in the array is a 0-D array.

Example

Create a 0-D array using the value 61:

import numpy as np
arr = np.array(61)
print(arr)

Run Example

1-D array

Its elements are arrays of 0-D arrays, called one-dimensional or 1-D arrays.

This is the most common and basic array.

Example

Create a 1-D array containing the values 1, 2, 3, 4, 5, 6:

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

Run Example

2-D array

Its elements are arrays of 1-D arrays, called 2-D arrays.

They are usually used to represent matrices or second-order tensors.

NumPy has a complete sub-module dedicated to matrix operations numpy.mat.

Example

Create a 2-D array containing two arrays with values 1, 2, 3 and 4, 5, 6:

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

Run Example

3-D array

Its elements are arrays of 2-D arrays, called 3-D arrays.

Example

Create a 3-D array using two 2-D arrays, both containing the values 1, 2, 3 and 4, 5, 6:

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

Run Example

Check the number of dimensions?

NumPy arrays provide ndim Property, which returns an integer that tells us how many dimensions the array has.

Example

Check the number of dimensions of the array:

import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim) 
print(b.ndim) 
print(c.ndim) 
print(d.ndim)

Run Example

Higher-dimensional arrays

Arrays can have an arbitrary number of dimensions.

You can use ndmin Parameter defines the number of dimensions.

Example

Create an array with 5 dimensions and verify that it has 5 dimensions:

import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions:', arr.ndim)

Run Example

In this array, the innermost dimension (the 5th dim) has 4 elements, the 4th dim has 1 element as a vector, the 3rd dim has 1 element as a matrix with the vector, the 2nd dim has 1 element as a 3D array, and the 1st dim has 1 element, which is a 4D array.