Friday, July 17, 2020

NumPy

NumPy

NumPy must be installed with PIP.

Then just import like any other module.

Whenever you see a __something__, that is called an attribute. Modules usually have many attributes.

Usually numpy is aliased as np.

import numpy as np

then create an array with np.array([]) by putting at least a scalar into the square braces.

0-D array is a scalar.
1-D array is a vector [ ]
2-D array is an array of vectors, or a matrix. [[ ]]
3-D array is an array of matrices, or rectangular box if you will envision that. [[[ ]]]

Using the len() built-in function will only give you the length of the highest dimension.

If you use more pairs of square braces than there are quantities to populate them, then the length of the unpopulated dimension(s) is one.

The .ndim property of np contains the number of dimensions of the total array.

An unlimited number of dimensions is allowed.

There are two ways to create an n-dimensional array.



1. Use the ndmin argument to the array() member function.

import numpy as np


family = np.array(["Dad", "Mom", "Daughter", "Son"], ndmin=3)

print(family)

# result is:

[[['Dad', 'Mom', 'Daughter', 'Son']]]



2. Use as many pairs of square braces as there are dimensions.

import numpy as np

family = np.array([[["Dad", "Mom", "Daughter", "Son"]]]

print(family)

# result is:

[[['Dad', 'Mom', 'Daughter', 'Son']]]


Note that the 1st and 2nd dimensions are of length 1.



No comments:

Post a Comment