Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
parent
7511619e90
commit
d5439698d5
1 changed files with 112 additions and 0 deletions
|
@ -28,3 +28,115 @@ numpy.tile(A, reps)
|
|||
>
|
||||
> Note : Although tile may be used for broadcasting, it is strongly recommended to use numpy’s 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 haven’t 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]]]
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue