pytutorial/numpy/resize/README.md
David Rotermund be26e2cdea
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-30 18:48:24 +01:00

132 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Resize: Compensation for bad planning?
{:.no_toc}
<nav markdown="1" class="toc-class">
* TOC
{:toc}
</nav>
## Top
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
**Try to avoid using this function!** Do better planning.
Resize does its work on the linear memory segment.
* If it is resized to a bigger array, the beginning of the original memory segment is copied into the new end segment.
* If it is resized to a smaller array, the memory segment is shrunken by cutting of the end.
## [numpy.resize](https://numpy.org/doc/stable/reference/generated/numpy.resize.html)
```python
numpy.resize(a, new_shape)
```
> Return a new array with the specified shape.
>
> If the new array is larger than the original array, then the new array is filled with repeated copies of a. Note that this behavior is different from a.resize(new_shape) which fills with zeros instead of repeated copies of a.
```python
import numpy as np
a = np.arange(1, 10).reshape((3, 3))
print(a)
print()
b = np.resize(a, (2, 3))
print(b)
print()
b = np.resize(a, (3, 1))
print(b)
print()
b = np.resize(a, (5, 5))
print(b)
```
Output:
```python
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 3]
[4 5 6]]
[[1]
[2]
[3]]
[[1 2 3 4 5]
[6 7 8 9 1]
[2 3 4 5 6]
[7 8 9 1 2]
[3 4 5 6 7]]
```
## This is not [numpy.ndarray.resize](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.resize.html#numpy.ndarray.resize)
```python
ndarray.resize(new_shape, refcheck=True)
```
> Change shape and size of array in-place.
I added a copy because it does not work on views (*ValueError: cannot resize this array: it does not own its data*) , which reshape creates.
```python
import numpy as np
a = np.arange(1, 10).reshape((3, 3)).copy()
print(a)
print()
a.resize((5, 5))
print(a)
```
Output:
```python
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 3 4 5]
[6 7 8 9 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
```
## More bad function
**Try to avoid using these functions!**
### [numpy.delete](https://numpy.org/doc/stable/reference/generated/numpy.delete.html)
```python
numpy.delete(arr, obj, axis=None)
```
> Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by arr[obj].
### [numpy.insert](https://numpy.org/doc/stable/reference/generated/numpy.insert.html)
```python
numpy.insert(arr, obj, values, axis=None)
```
> Insert values along the given axis before the given indices.
### [numpy.append](https://numpy.org/doc/stable/reference/generated/numpy.append.html)
```python
numpy.append(arr, values, axis=None)
```
> Append values to the end of an array.