Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2023-12-28 15:54:51 +01:00 committed by GitHub
parent 7511619e90
commit d5439698d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,3 +28,115 @@ numpy.tile(A, reps)
>
> Note : Although tile may be used for broadcasting, it is strongly recommended to use numpys broadcasting operations and functions.
**Very very important!!! First use numpy.newaxis to create the required additional axis and then use tile.**
Adding a new dimension:
```python
import numpy as np
a = np.arange(1, 5)
print(a) # -> [1 2 3 4]
print(a.shape) # -> (4,)
b = a[np.newaxis, :]
print(b.shape) # -> (1, 4)
print(np.may_share_memory(a, b)) # -> True (View)
c = np.tile(a, (1, 1))
print(c.shape) # -> (1, 4)
print(np.may_share_memory(a, c)) # -> False (Copy)
```
Examples:
```python
import numpy as np
a = np.arange(1, 5)[np.newaxis, :]
print(a) # -> [[1 2 3 4]]
print(a.shape) # -> (1, 4)
c = np.tile(a, (1, 1))
print(c) # -> [[1 2 3 4]]
print(c.shape) # -> (1, 4)
c = np.tile(a, (4, 1))
print(c)
print(c.shape) # -> (4, 4)
c = np.tile(a, (1, 4))
print(c) # -> [[1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4]]
print(c.shape) # -> (1, 16)
```
Output:
```python
[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]]
```
Be very careful if you havent newaxis-ed...
```python
import numpy as np
a = np.arange(1, 5)
print(a) # -> [1 2 3 4]
print(a.shape) # -> (4,)
c = np.tile(a, (4))
print(c) # -> [1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4]
print(c.shape) # -> (16,)
c = np.tile(a, (1, 4))
print(c) # -> [[1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4]]
print(c.shape) # -> (1, 16)
c = np.tile(a, (4, 1))
print(c)
print(c.shape) # -> (4, 4)
print()
c = np.tile(a, (4, 1, 1))
print(c)
print(c.shape) # -> (4, 1, 4)
print()
c = np.tile(a, (1, 4, 1))
print(c)
print(c.shape) # -> (1, 4, 4)
print()
c = np.tile(a, (1, 1, 4))
print(c) # -> [[[1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4]]]
print(c.shape) # -> (1, 1, 16)
```
Output:
```python
[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]]
[[[1 2 3 4]]
[[1 2 3 4]]
[[1 2 3 4]]
[[1 2 3 4]]]
[[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]]]
```