... | ... | @@ -64,50 +64,72 @@ I may also reference a couple examples from the previous: |
|
|
We can only scratch the surface during a one (1) hour workshop.
|
|
|
|
|
|
|
|
|
# Examples
|
|
|
# A Few Short Examples
|
|
|
|
|
|
The first few examples will be short and focus on arrays and statistics. These
|
|
|
examples are inspired by [*Coffee Break NumPy* by Christian
|
|
|
Mayer](https://www.amazon.com/Coffee-Break-NumPy-Science-Mastery/dp/1076932614).
|
|
|
My original intention was to focus on a few short (10 to 20 line) examples that
|
|
|
focused on arrays and statistics... with a little linear algebra at the end.
|
|
|
|
|
|
If you are interested in a collection of problems to develop your NumPy
|
|
|
understanding... [*Coffee Break NumPy* by Christian
|
|
|
Mayer](https://www.amazon.com/Coffee-Break-NumPy-Science-Mastery/dp/1076932614)
|
|
|
is an excellent read. In fact... I read the book during the 2020 Winter Break.
|
|
|
|
|
|
Let us start with a few short NumPy basics (e.g., arrays and broadcasting) then
|
|
|
build Matrix Solver!.
|
|
|
|
|
|
|
|
|
## Creating Arrays
|
|
|
|
|
|
```python
|
|
|
array_size = 8
|
|
|
zeroes_array = np.zeros(array_size)
|
|
|
print(zeroes_array)
|
|
|
print()
|
|
|
```
|
|
|
The code snippets from this section are extracted from
|
|
|
[array_creation.py](https://git-community.cs.odu.edu/tkennedy/python-workshop/-/blob/master/NumPy/array_creation.py).
|
|
|
|
|
|
```python
|
|
|
array_size = 12
|
|
|
ones_array = np.ones(array_size)
|
|
|
print(ones_array)
|
|
|
print()
|
|
|
```
|
|
|
NumPy arrays can be...
|
|
|
|
|
|
```python
|
|
|
# Contents are "whatever happens to be in memory"
|
|
|
array_size = 16
|
|
|
unitialized_array = np.empty(array_size)
|
|
|
print(unitialized_array)
|
|
|
print()
|
|
|
```
|
|
|
- Initialized to all zeroes
|
|
|
|
|
|
```python
|
|
|
python_list = [2, 4, 8, 16, 32, 64]
|
|
|
np_array = np.array(python_list)
|
|
|
print(np_array)
|
|
|
print()
|
|
|
```
|
|
|
```python
|
|
|
array_size = 8
|
|
|
zeroes_array = np.zeros(array_size)
|
|
|
print(zeroes_array)
|
|
|
print()
|
|
|
```
|
|
|
|
|
|
```python
|
|
|
python_list = [2., 4., 8., 16., 32., 64.]
|
|
|
np_array = np.array(python_list)
|
|
|
print(np_array)
|
|
|
print()
|
|
|
```
|
|
|
- Initialized to all ones
|
|
|
|
|
|
```python
|
|
|
array_size = 12
|
|
|
ones_array = np.ones(array_size)
|
|
|
print(ones_array)
|
|
|
print()
|
|
|
```
|
|
|
|
|
|
- Allocated and left uninitialized
|
|
|
|
|
|
```python
|
|
|
# Contents are "whatever happens to be in memory"
|
|
|
array_size = 16
|
|
|
unitialized_array = np.empty(array_size)
|
|
|
print(unitialized_array)
|
|
|
print()
|
|
|
```
|
|
|
|
|
|
- Created from a Python List of `int`s
|
|
|
|
|
|
```python
|
|
|
python_list = [2, 4, 8, 16, 32, 64]
|
|
|
np_array = np.array(python_list)
|
|
|
print(np_array)
|
|
|
print()
|
|
|
```
|
|
|
|
|
|
- Created from a Python List of `floats`s
|
|
|
|
|
|
```python
|
|
|
python_list = [2., 4., 8., 16., 32., 64.]
|
|
|
np_array = np.array(python_list)
|
|
|
print(np_array)
|
|
|
print()
|
|
|
```
|
|
|
|
|
|
|
|
|
## Broadcasting
|
... | ... | |