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

Update numpy.md #306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
10 changes: 9 additions & 1 deletion _tutorials/numpy.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ The biggest advantage of NumPy is its ability to handle numerical arrays. For ex
a = [1, 2, 3, 4, 5]
b = []
for i in a:
b.append(a**2)
b.append(i**2)
print(b)
```

and you will get `[1, 4, 9, 16, 25]` for `b`. Now, if you want to do the same with a 2-dimensional array, the base Python to do this is:
Expand All @@ -40,6 +41,7 @@ b = [[],[]]
for i in range(len(a)):
for j in range(len(a[i])):
b[i].append(a[i][j]**2)
print(b)
```

This would give you `b` equal to `[[1, 4], [9, 16]]`. To do the same with a 3D array you would need 3 nested loops and to do it in 4D would require 4 nested loops. However, with NumPy you can take the square of an array of any dimensions using the same line of code and no loops:
Expand Down Expand Up @@ -158,6 +160,9 @@ for i in [a, b]:
i[:, :, 0] += 1.
elif dimensions == 4:
i[:, :, :, 0] += 1.

print(a)
print(b)
```
While there is nothing wrong with this code, and it will do what we want, there is a better method:
```python
Expand All @@ -169,6 +174,9 @@ b = np.zeros((3, 3, 3, 3))
# add 1 to the first columns
for i in [a, b]:
i[..., 0] += 1.

print(a)
print(b)
```
`...` is called the ellipsis, and it is used to select all unspecified dimensions.

Expand Down