8000 added some basics about numpy arrays · sdevikar/python@938f6d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 938f6d9

Browse files
author
sdevikar
committed
added some basics about numpy arrays
1 parent 60ff9ac commit 938f6d9

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

python2/13_numpy_basics.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import numpy as np
2+
3+
a = np.arange(15).reshape(3, 5)
4+
print(a)
5+
6+
###
7+
# This will create the following array:
8+
# [
9+
# [ 0 1 2 3 4]
10+
# [ 5 6 7 8 9]
11+
# [10 11 12 13 14]
12+
# ]
13+
#
14+
# Notice that we have 3 arrays of length 5 each.
15+
# Read the array as follows:
16+
# The last value in the shape tuple is the length of the innermost array
17+
# The value to the left in the tuple i.e. 3 in (3,5) is the number of such arrays
18+
###
19+
20+
b = np.arange(30).reshape(3, 5, 2)
21+
print(b)
22+
23+
###
24+
# This will create the following array
25+
# [
26+
# [[ 0 1]
27+
# [ 2 3]
28+
# [ 4 5]
29+
# [ 6 7]
30+
# [ 8 9]]
31+
#
32+
# [[10 11]
33+
# [12 13]
34+
# [14 15]
35+
# [16 17]
36+
# [18 19]]
37+
#
38+
# [[20 21]
39+
# [22 23]
40+
# [24 25]
41+
# [26 27]
42+
# [28 29]]
43+
# ]
44+
# Notice that the reading method above still holds. i.e. in shape tuple (3,5,2)
45+
# 2 is the length of the innermost array
46+
# The value to its left, i.e. 5, is the number of the innermost array
47+
# The value further to the left, i.e. 3, is the number of arrays of arrays of length 2
48+
49+
50+
# Indexing the arrays
51+
print(a[0]) # this should return the first row i.e. [0 1 2 3 4]
52+
print(a[0:2]) # this should return row 0 and row 1. Row 2 is not included
53+
54+
# in order to specify an individual element, row and column indexes,
55+
# which are called axes in numpy arrays, should be specified as comma separated values
56+
print("a[0,1] = {}".format(a[0,1])) # this should return 1
57+
print("a[0,1] = {}".format(a[0][1])) # this is the same as a[0,1]
58+
59+
# So, in order to access 15 in array b, we can do the following
60+
print("b[1,2,1] = {}".format(b[1,2,1]))

0 commit comments

Comments
 (0)
0