#compsci #python #software 

NumPy is a [[Python modules|Python]] [[List of Python modules|module]] designed for use in scientific and engineering computation

Main advantage is that it is much much faster than generic Python due to the fact it has optimized, pre-compiled C code.

The following is valid as of Nov 2025, numpy version 2.4.0

## Basics
Every object contains the reference to the **docstring** - documentation, use help(%object%) for that OR %object%?


### Arrays
NumPy's main object is the homogenous multidimensional array - **ndarray**, alias - **array**

In NumPy, dimensions are called **axes**
![[Pasted image 20250130180221.png]]

Properties:
![[Pasted image 20250130175854.png]]
![[Pasted image 20250130175905.png]]

#### Array creation
![[Pasted image 20250130182415.png]]

Explicit specification of dtype:
![[Pasted image 20250130182433.png]]

Arrays with initial placeholder content:
![[Pasted image 20250201164926.png]]
![[Pasted image 20250201173007.png]]

To create an array filled with an arbitrary number/object:
![[Pasted image 20260309221518.png]]
![[Pasted image 20260309231322.png]]

Sequences of numbers:
![[Pasted image 20250201165044.png]]
![[Pasted image 20250201165125.png]]

![[Pasted image 20250201182206.png]]
![[Pasted image 20250201182219.png]]
([[Identity matrix]] and [[Диагональная матрица]])

#### Printing arrays
![[Pasted image 20250201165255.png]]![[Pasted image 20250201165449.png]]

![[Pasted image 20250201165504.png]]

#### Basic operations
Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result
![[Pasted image 20250201165544.png]]

The concept that allows NumPy to perform elementwise operations optimally is called **broadcasting**:
![[Pasted image 20250201174456.png]]

The product operator * operates elementwise in NumPy arrays. The [[Matrix|matrix product]] can be berformed using the @ operator.

![[Pasted image 20250201165716.png]]

![[Pasted image 20250201165758.png]]

![[Pasted image 20260309231823.png]]

Other: nd.array.min()/max()/mean()/sum()/prod()/std()

np.median(), np.dot (for example: x=np.array(...). print(x.dot(z)), where z is some other array)
np.inner(x,z) - inner product
np.trace(x) - [[Trace of a matrix]]


std() - standard deviation

np.unique(ndarray) - prints the unique values in your array:

![[Pasted image 20250201174735.png]]
![[Pasted image 20250201174759.png]]

Random number generation:
```python
from numpy.random import default_rng
default_rng(%seed_number%).random(%number or tuple%)
```

![[Pasted image 20250201182451.png]]
#### Universal functions
**ufunc** - like sin, cos, exp. Within NumPy, these functions operate elementwise on an array, producing an array as output

#### Indexing, slicing and iterating
One-dimensional arrays can be indexed, sliced, and iterated over:
![[Pasted image 20250201170024.png]]

Multi-dimensional arrays can have one index per axis. These indices are given in a tuple separated by commas:
![[Pasted image 20250201170054.png]]

![[Pasted image 20250201170117.png]]
![[Pasted image 20250201170208.png]]

![[Pasted image 20250201170413.png]]
![[Pasted image 20250201172129.png]]

![[Pasted image 20250201172418.png]]

#### Shape manipulation
![[Pasted image 20250201170804.png]]
![[Pasted image 20250201175050.png]]
![[Pasted image 20250201175059.png]]
![[Pasted image 20250201174837.png]]

![[Pasted image 20250201174855.png]]
ndarray.reshape function returns its argument with a modified shape, whereas the ndarray.resize method modifies the array itself

![[Pasted image 20250201170907.png]]

Stacking:
![[Pasted image 20250201171236.png]]

Splitting:
![[Pasted image 20250201171329.png]]
![[Pasted image 20250201171447.png]]

![[Pasted image 20250201173626.png]]
![[Pasted image 20250201173634.png]]
#### Copies and views
![[Pasted image 20250201171737.png]]
![[Pasted image 20250201171751.png]]
![[Pasted image 20250201171808.png]]


#### Sorting
![[Pasted image 20250201173217.png]]
![[Pasted image 20250201173224.png]]
![[Pasted image 20250201173240.png]]

![[Pasted image 20260309232739.png]]
![[Pasted image 20260309232759.png]]

#### Saving and loading
Ndarray objects can be saved and loaded into/from txt files with **loadtxt** and **savetxt**, **load** and **save** functions handle NumPy binary files with a .npy file extension, and a **savez** function handles NumPy files with a .npz file extension
![[Pasted image 20250201181643.png]]
![[Pasted image 20250201181657.png]]


You can also use **numpy.genfromtxt**:
![[Pasted image 20260309233333.png]]
### Interactions with other libraries
#### [[Pandas]]
Importing a CSV file:
```python
pd.read_csv('file', header=0).values
```

Exporting a CSV file:
```python
a=np.array(...)
df=pd.DataFrame(a)
df.to_csv('pd.csv')
```

#### [[Matplotlib]]
MPL is often used with numpy's ndarray objects

### Structured arrays
![[Pasted image 20250201183231.png]]

### Linear algebra
The **numpy.linalg** module contains methods used in linear algebra, such as:
- numpy.linalg.vecdot - [[Скалярное произведение векторов]]
- numpy.linalg.cross - [[Векторное произведение векторов]]
- numpy.linalg.eig and numpy.linalg.eigvals - [[Eigenvalues and eigenvectors of a matrix]]
- numpy.linalg.det - [[Determinant of a matrix]]
- numpy.linalg.solve - similar to LinearSolve[] in [[Wolfram Mathematica|Wolfram Language]]
### Extra
**numpy.fft.fft** - [[Fast Fourier Transform]]
