Skip to content
Snippets Groups Projects
Commit a032a3e3 authored by Jöran Frey's avatar Jöran Frey
Browse files

made some progress in the lecture part 1 of numpy

parent a187acb6
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# NumPy
%% Cell type:markdown id: tags:
## Agenda Part 1: Arrays and Matrices
* Introduction to NumPy
* NumPy Arrays
* Slice
* Split
* Join
* Copy
* Serach
* Sort
* Filter
* Multidimensional Arrays
* Shape / Reshape
* Iterations
%% Cell type:markdown id: tags:
## Numpy
* **What is Numpy?**
A Python library for numerical computing that provides efficient operations on large arrays and matrices. The range of functions is similar to matlab
* **Why Use NumPy Instead of Native Python** <br>
NumPy provides better performance, memory efficiency, and ease of use, making it the preferred choice for numerical computing tasks over native Python. <br>
* **Why is Numpy faster?** <br>
NumPy is written in C, a low-level programming language, which allows it to execute operations much faster than Python, which is an interpreted language.
%% Cell type:markdown id: tags:
## Let's look at some numpy Code!
%% Cell type:markdown id: tags:
## Numpy Arrays
* A NumPy array is a powerful data structure provided by the NumPy library, designed for numerical and scientific computing. It is similar to Python lists but much more efficient
<br>
<br>
* **Key characteristics of Numpy arrays**
* **Fixed Size:**
Once created, the size of a NumPy array cannot change. This makes it more efficient compared to Python lists.
* **Homogeneous Data:**
All elements in a NumPy array must be of the same data type (e.g., all integers or all floats). This allows NumPy to optimize memory usage and computation.
* **N-Dimensional:**
NumPy arrays support multi-dimensional structures, making them suitable for handling vectors (1D), matrices (2D), or even tensors (3D and beyond).
%% Cell type:markdown id: tags:
## Creating a NumPy Array
* first we have to import the numpy library like the other libraries we used before we can do this with the import command ```import numpy as np``` with np beeing the short form of numpy.
* a basic numpy array can be imported with ```np.array([1,2,3])```
%% Cell type:code id: tags:
``` python
import numpy as np
test_array = np.array([1,3,3,8])
print(test_array)
```
%% Output
[1 3 3 8]
%% Cell type:markdown id: tags:
| **Method** | **Description** | **Example Code** | **Output** |
| --------------------- | ---------------------------------------------- | ----------------------- | ---------------------------------------------------- |
| **`np.array`** | Create an array from a Python list or tuple. | `np.array([1, 2, 3])` | `[1 2 3]` |
| **`np.zeros`** | Create an array filled with zeros. | `np.zeros((2, 3))` | `[[0. 0. 0.] [0. 0. 0.]]` |
| **`np.ones`** | Create an array filled with ones. | `np.ones((2, 3))` | `[[1. 1. 1.] [1. 1. 1.]]` |
| **`np.empty`** | Create an uninitialized array (random values). | `np.empty((2, 2))` | Random values like `[[0. 0.] [0. 0.]]` |
| **`np.arange`** | Create an array with a range of values. | `np.arange(0, 10, 2)` | `[0 2 4 6 8]` |
| **`np.linspace`** | Create evenly spaced values between a range. | `np.linspace(0, 1, 5)` | `[0. 0.25 0.5 0.75 1. ]` |
| **`np.eye`** | Create an identity matrix. | `np.eye(3)` | `[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]` |
| **`np.random.rand`** | Create an array with random values (uniform). | `np.random.rand(2, 3)` | Random values like `[[0.5 0.7 0.1] [0.3 0.9 0.4]]` |
| **`np.random.randn`** | Create random values with normal distribution. | `np.random.randn(2, 3)` | Random values like `[[1.2 -0.4 0.8] [-0.7 1.5 0.3]]` |
| **`np.full`** | Create an array filled with a specific value. | `np.full((2, 3), 7)` | `[[7 7 7] [7 7 7]]` |
%% Cell type:markdown id: tags:
NumPy provides several ways to create arrays. Below are the most common methods:
%% Cell type:markdown id: tags:
## Operation with numpy arrays
* as with all libraries numpy comes with many built in functions let's have a look at the most common functions to manipulate numpy arrays
%% Cell type:markdown id: tags:
| **Operation** | **Description** | **Example Code** | **Output** |
| ------------- | --------------------------------------- | ------------------------------------------------------ | ----------------------------------------- |
| **Slice** | Extract part of an array. | `arr[1:4]` (from `arr = np.array([1, 2, 3, 4, 5])`) | `[2 3 4]` |
| **Split** | Split an array into smaller arrays. | `np.array_split(np.array([1, 2, 3, 4, 5]), 2)` | `[array([1, 2, 3]), array([4, 5])]` |
| **Join** | Combine multiple arrays into one. | `np.concatenate((np.array([1, 2]), np.array([3, 4])))` | `[1 2 3 4]` |
| **Copy** | Create a copy of an array. | `copy = arr.copy()` (from `arr = np.array([1, 2, 3])`) | A new array identical to `arr`: `[1 2 3]` |
| **Search** | Find index of specific values. | `np.where(np.array([1, 2, 3, 4]) == 3)` | `(array([2]),)` (index of value `3`) |
| **Sort** | Sort array elements in ascending order. | `np.sort(np.array([3, 1, 2]))` | `[1 2 3]` |
| **Filter** | Extract elements meeting a condition. | `np.array([1, 2, 3, 4])[np.array([1, 2, 3, 4]) > 2]` | `[3 4]` |
%% Cell type:code id: tags:
``` python
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:4])
```
%% Output
[2 3 4]
%% Cell type:markdown id: tags:
## Filter
%% Cell type:markdown id: tags:
%% Cell type:code id: tags:
``` python
import numpy as np
arr = np.array([41, 42, 43, 44])
filter = arr > 42
newarr = arr[filter]
print(newarr)
```
%% Output
[43 44]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment