Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edited Python Numpy built-in functions .reshape() #5523

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions content/numpy/concepts/built-in-functions/terms/reshape/reshape.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,43 @@ This produces the following output:
[5]
[6]]
```

Optionally, a third parameter can be added to the syntax: `order`.

`order` reads the elements from `array` in the current index order and places the elements into the reshaped array using the same index order. It can be set to `'C'`, `'F'`, or `'A'`. `C` means to read/write the elements using C-like index order, or in other words, the elements are placed row by row. The last axis index (columns) changes first (fastest), and the first axis index (rows) changes afterward (slowest). `F` means to read/write the elements using Fortran-like index order, where the elements are placed column by column. Here, the first axis index (rows) changes faster while the last axis index (columns) changes slower. Using `C` or `F` in `.reshape()` means that the function is only reshaping the way that indexes are placed in the array, and not like data physically stored in the computer memory (memory layout). `A` means to read/write the elements in Fortran-like index order if `array` is Fortran contiguous in memory, meaning, in a sequencial and continuous manner without gaps between elements, or in C-like index order otherwise.

```pseudo
numpy.reshape(array, newshape, order = 'C')
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be edited in the ##Syntax section itself. @NelsonSanti What we can do is, go to the ## Syntax section above, modify that syntax by adding the order parameter as well. Then after the syntax, use bullet points to address the parameters. Example:

numpy.reshape(array, newshape, order = 'C')
  • array: Describe this
  • newshape: describe this
  • order: Describe this
    • C: describe
    • F: describe
    • A: descibe

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way a proper structure can be maintained


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Codebyte Example

The following example creates an `ndarray` then uses `order` as an optional parameter for `.reshape()` to change its dimensions.

```py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```py
```codebyte/python

import numpy as np

nd1 = np.array([[10, 20, 30], [40, 50, 60]])

print(nd1)
print(np.reshape(nd1, (3, 2), order='C')) # row by row
print(np.reshape(nd1, (3, 2), order='F')) # column by column
```

This produces the following output:

```shell
[[10 20 30]
[40 50 60]]


[[10 20]
[30 40]
[50 60]]


[[10 50]
[40 30]
[20 60]]
```

> [!Note]
> The last output just reflects the natural behaviour from Fortran indexation order
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This produces the following output:
```shell
[[10 20 30]
[40 50 60]]
[[10 20]
[30 40]
[50 60]]
[[10 50]
[40 30]
[20 60]]
```
> [!Note]
> The last output just reflects the natural behaviour from Fortran indexation order

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codebyte examples are not to be accompanied by output blocks, so we can remove this

Loading