Python-Numpy-Cheatsheet
Numpy CheatSheet
See the repo on my github: https://github.com/xiyuanyang-code/numpy_tutorial.
Introduction
This section is copied from
README.md
part in my github repo.
Learning Numpy within 30 minutes!
😊Quickly get started with basic NumPy operations in 30 minutes!
See https://xiyuanyang-code.github.io/posts/Python-numpy-cheatsheet/ for more details.
Introduction
NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more. [1]
This repo provides with the basic numpy tutorial with .ipynb
file, aiming to teach greenhands to quickly get started with basic NumPy operations using Jupyter Notebook interaction in 30 minutes.
Installation
This command is for importing the numpy module in your python scripts:
1 |
|
You can also use the requirements.txt
file in this repo.
1 |
|
After the installation, run the first code cell in numpy_tutorial.ipynb
to check if the output is correct. (1.26.4
)
Usage
The tutorial includes several parts:
- Creating arrays and matrix
- Checking parameters of Arrays
- Accessing and modifying elements
- Slices
- Modifications
- Repetition, Copying and Reshaping
- Repeat
- Copy
- Reshape
- Linear Algebra and other mathematical operations
- Basic Mathematical operations
- Linear Algebra
- Statics
- Boolean Mask
- Advanced Usage
- Loading data from files
Go to the juypter notebook for more details!
In the future…
We will finish the /demo
directory in the future, which will provides you with practical applications of numpy.
References
Basic Usage
This section is copied from
numpt_tutorial.ipynb
section in my github repo.
Numpy tutorial
Preliminaries
This command is for importing the numpy module in your python scripts:
1 |
|
You can use functions in numpy via np.XX
(e.g. np.array
)
You can replace it with:
- import numpy as (anything word you like)
- from numpy import numpy (danger! This may cause conflicts in different functions. I don’t recommend for this way of importing numpy)
In [1]:
1 |
|
Creating arrays and matrix
You can create arrays in numpy
of different dimensions, which is one of the most frequently used commands in numpy.
Creating arrays manually
You can create arrays manually by writing all elements into the arrays.
In [ ]:
1 |
|
Creating several templates
It’s time-consuming to write all elements in your python scripts if the size or the dimensions of arrays becomes very large. (More bigger the dimension is, there are more square backets!)
Thus, you can use the templates in
numpy
to initilize an array automatically.
In [ ]:
1 |
|
In [ ]:
1 |
|
Moreover, you can generate arrays randomly using the random
module in numpy.
np.random.rand
function is used to generate an array of a given size with a a uniform distribution over [0, 1).randint
function is used to return random integers from a boundary of [a,b).
In [ ]:
1 |
|
Checking parameters of Arrays
You can checking several basic information of an array using print()
function.
- print the array: It will show all elements of the array.
dtype
: The data type of the array.ndim
: The dimensions of the array.shape
: The size of the array.itemsize
: The size of the single element in the array.nbytes
: The total size of the array.
In [ ]:
1 |
|
Wow! You have sucessfully created various arrays! In the following chapter, you will learn how to operate arrays and make some calculations.
Accessing and modifying elements
Slices
We can get the slices of the array and access specific elements by using []
operator. The scripts below shows the fundamental usage of it.
In [ ]:
1 |
|
From the example shown above, we know that []
can get an array’s slices and decrease array’s dimensions. It’s just like peeling an onion!
Regarding the different dimensions of an array, you can determine different dimensions by counting square brackets.
Moreover, you can use :
to make the slices more powerful.
In [ ]:
1 |
|
The colon :
operator is used for slicing, which enables you to select a range of elements.
For example, array[start:end] retrieves elements from the index start to end-1. If you use : alone, like array[:], it returns the entire array. You can also specify a step, such as array[start:end:step], which selects elements at regular intervals.
Additionally, when working with multi-dimensional arrays, you can use a comma to separate indices for each dimension, like array[row_index, column_index]. The colon can be applied to each dimension, allowing for complex selections, such as array[:, 1:3], which selects all rows but only columns 1 and 2.
For me, I prefer to memorize :
as a greedy symbol, which means get all the index from the current position to the end (or the beginning).
For example, get_element[0,2:,3:]
means for the second dimension, we just want from the 2 (included) till the end. The third dimension is the same. If :
is before the number like get_element[0,:3]
, this means for second dimension, we want (from the begining) to the index 3 (excluded).
Modifications
You can also modify values (or even slices) of the array by giving the right index.
Make sure the size of the array doesn’t mismatch!
In [ ]:
1 |
|
In [ ]:
1 |
|
Repetition, Copying and Reshaping
You can repeat, reshape and copy arrays!
Repeat
In [ ]:
1 |
|
Copy
In [ ]:
1 |
|
Attention! If you use copy()
function, then the copied array have no correlations with the original array from now on. However, if you simply use the assignment operator, then it just creates a references of the original array. Modifying one’s value will affect the other’s!
Reshape
You can reshape the size of an array by using the reshape
commands in numpy. For example, you can compress a 25x25 image into a 625-dimensional column vector. But make sure the reshaped size have same numbers of elements compared with previous ones.
Stacking is a command which is likely to repeat
, but it can operate on different arrays with same row-length (using hstack) or colomn-length (using vstack).
In [ ]:
1 |
|
Linear Algebra and other mathematical operations
Here is Linear Algebra! You can implement various computations regarding matrix and arrays in numpy.
Basic Mathematical operations
In [ ]:
1 |
|
Linear Algebra
You can go through the linalg website for more caiculations. The python scripts demonstrat several basic operations of it.
In [ ]:
1 |
|
Statics
In [ ]:
1 |
|
Boolean Mask
In [ ]:
1 |
|
Advanced Usage
Loading data from files
You can use genfromtxt()
function to automatically write the arrays from txt file into numpy. Moreover, you can use savetxt()
function to save your arrays into other files!
In [ ]:
1 |
|
In [ ]:
1 |
|
Conclusion
This memo provides a concise overview of NumPy’s capabilities, including array creation, manipulation, mathematical operations, and more. For further details, refer to the official NumPy documentation.