Wednesday 9 January 2019

NumPy Scalars, Vectors, Matrices, Tensors Examples

Paste the code below (but not the output section) into a Colab (https:\\colab.research.google.com\) Jupyter Notebook,if you would like to execute it.
-------------------------------------------------------------------------------------------------

import numpy as np

# Numpy uses array() to create objects of ndarray type.

###########################################
# Vectors
#   0-Dimensions (aka the origin of a Vector Space)
v0 = np.array(0) # also [0.], [0., 0.], [0., 0., 0.], ...
print('v0      : ', v0      )
print('v0.shape: ', v0.shape)
print('v0.ndim : ', v0.ndim )
#   1-Dimension  (aka Scalar)
v1 = np.array([1.])
print('v1      : ', v1      )
print('v1.shape: ', v1.shape)
print('v1.ndim : ', v1.ndim )
#   2-Dimensions
v2 = np.array([1., 2.])
print('v2      : ', v2      )
print('v2.shape: ', v2.shape)
print('v2.ndim : ', v2.ndim )
#   3-Dimensions
v3 = np.array([1., 2., 3.])
print('v3.shape: ', v3.shape)
print('v3.ndim : ', v3.ndim )
print('v3      : ', v3      )
#   4-Dimensions
v4 = np.array([1., 2., 3., 4.])
print('v4      : ', v4      )
print('v4.shape: ', v4.shape)
print('v4.ndim : ', v4.ndim )

###########################################
# Matrices
#   0-Dimensions (aka Scalar)
m0 = np.array(4.)
print('m0      : ', m0      )
print('m0.shape: ', m0.shape)
print('m0.ndim : ', m0.ndim )
#   1-Dimension (aka Scalar (if one coordinate),
#                aka Vector (if more than one coordinate))
m1 = np.array([1.])
print('m1      : ', m1      )
print('m1.shape: ', m1.shape)
print('m1.ndim : ', m1.ndim )
#   2-Dimensions
m2 = np.array([[1., 2.],
               [3., 4.]])
print('m2      : ', m2      )
print('m2.shape: ', m2.shape)
print('m2.ndim : ', m2.ndim )
#   3-Dimensions
m3 = np.array([[1., 2., 3.],
               [4., 5., 6.],
               [7., 8., 9.]])
print('m3      : ', m3      )
print('m3.shape: ', m3.shape)
print('m3.ndim : ', m3.ndim )
#   4-Dimensions
m4 = np.array([[ 1.,  2.,  3.,  4.],
               [ 5.,  6.,  7.,  8.],
               [ 9., 10., 11., 12.],
               [13., 14., 15., 16.]])
print('m4      : ', m4      )
print('m4.shape: ', m4.shape)
print('m4.ndim : ', m4.ndim )

# Tensors
###########################################
#   0-Dimensions (aka Scalar)
t0 = np.array(4.)
print('t0      : ', t0      )
print('t0.shape: ', t0.shape)
print('t0.ndim : ', t0.ndim )
#   1-Dimension
t1 = np.array([1., 2., 3.])
print('t1      : ', t1      )
print('t1.shape: ', t1.shape)
print('t1.ndim : ', t1.ndim )
#   2-Dimensions
t2 = np.array([[1., 2.],
               [3., 4.]])
print('t2      : ', t2      )
print('t2.shape: ', t2.shape)
print('t2.ndim : ', t2.ndim )
#   3-Dimensions
t3 = np.array([[[ 1.,  2.,  3.],
                [ 4.,  5.,  6.],
                [ 7.,  8.,  9.]],
               [[10., 11., 12.],
                [13., 14., 15.],
                [16., 17., 18.]],
               [[19., 20., 21.],
                [22., 23., 24.],
                [25., 26., 27.]]])
print('t3      : ', t3      )
print('t3.shape: ', t3.shape)
print('t3.ndim : ', t3.ndim )
#   4-Dimensions
t4 = np.array([[[[  1.,   2.,   3.,   4.],
                 [  5.,   6.,   7.,   8.],
                 [  9.,  10.,  11.,  12.],
                 [ 13.,  14.,  15.,  16.]],
                [[ 17.,  18.,  19.,  20.],
                 [ 21.,  22.,  23.,  24.],
                 [ 25.,  26.,  27.,  28.],
                 [ 29.,  30.,  31.,  32.]],
                [[ 33.,  34.,  35.,  36.],
                 [ 37.,  38.,  39.,  40.],
                 [ 41.,  42.,  43.,  44.],
                 [ 45.,  46.,  47.,  48.]],
                [[ 49.,  50.,  51.,  52.],
                 [ 53.,  54.,  55.,  56.],
                 [ 57.,  58.,  59.,  60.],
                 [ 61.,  62.,  63.,  64.]]],
               [[[ 65.,  66.,  67.,  68.],
                 [ 69.,  70.,  71.,  72.],
                 [ 73.,  74.,  75.,  76.],
                 [ 77.,  78.,  79.,  80.]],
                [[ 81.,  82.,  83.,  84.],
                 [ 85.,  86.,  87.,  88.],
                 [ 89.,  90.,  91.,  92.],
                 [ 93.,  94.,  95.,  96.]],
                [[ 97.,  98.,  99., 100.],
                 [101., 102., 103., 104.],
                 [105., 106., 107., 108.],
                 [109., 110., 111., 112.]],
                [[113., 114., 115., 116.],
                 [117., 118., 119., 120.],
                 [121., 122., 123., 124.],
                 [125., 126., 127., 128.]]],
               [[[129., 130., 131., 132.],
                 [133., 134., 135., 136.],
                 [137., 138., 139., 140.],
                 [141., 142., 143., 144.]],
                [[145., 146., 147., 148.],
                 [149., 150., 151., 152.],
                 [153., 154., 155., 156.],
                 [157., 158., 159., 160.]],
                [[161., 162., 163., 164.],
                 [165., 166., 167., 168.],
                 [169., 170., 171., 172.],
                 [173., 174., 175., 176.]],
                [[177., 178., 179., 180.],
                 [181., 182., 183., 184.],
                 [185., 186., 187., 188.],
                 [189., 190., 191., 192.]]],
               [[[193., 194., 195., 196.],
                 [197., 198., 199., 200.],
                 [201., 202., 203., 204.],
                 [205., 206., 207., 208.]],
                [[209., 210., 211., 212.],
                 [213., 214., 215., 216.],
                 [217., 218., 219., 220.],
                 [221., 222., 223., 224.]],
                [[225., 226., 227., 228.],
                 [229., 230., 231., 232.],
                 [233., 234., 235., 236.],
                 [237., 238., 239., 240.]],
                [[241., 242., 243., 244.],
                 [245., 246., 247., 248.],
                 [249., 250., 251., 252.],
                 [253., 254., 255., 256.]]]])
print('t4      : ', t4      )
print('t4.shape: ', t4.shape)
print('t4.ndim : ', t4.ndim )

# Note how the data structures are like: Javascript, Perl, and JSON.
------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------

v0      :  0
v0.shape:  ()
v0.ndim :  0
v1      :  [1.]
v1.shape:  (1,)
v1.ndim :  1
v2      :  [1. 2.]
v2.shape:  (2,)
v2.ndim :  1
v3.shape:  (3,)
v3.ndim :  1
v3      :  [1. 2. 3.]
v4      :  [1. 2. 3. 4.]
v4.shape:  (4,)
v4.ndim :  1
m0      :  4.0
m0.shape:  ()
m0.ndim :  0
m1      :  [1.]
m1.shape:  (1,)
m1.ndim :  1
m2      :  [[1. 2.]
 [3. 4.]]
m2.shape:  (2, 2)
m2.ndim :  2
m3      :  [[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]
m3.shape:  (3, 3)
m3.ndim :  2
m4      :  [[ 1.  2.  3.  4.]
 [ 5.  6.  7.  8.]
 [ 9. 10. 11. 12.]
 [13. 14. 15. 16.]]
m4.shape:  (4, 4)
m4.ndim :  2
t0      :  4.0
t0.shape:  ()
t0.ndim :  0
t1      :  [1. 2. 3.]
t1.shape:  (3,)
t1.ndim :  1
t2      :  [[1. 2.]
 [3. 4.]]
t2.shape:  (2, 2)
t2.ndim :  2
t3      :  [[[ 1.  2.  3.]
  [ 4.  5.  6.]
  [ 7.  8.  9.]]

 [[10. 11. 12.]
  [13. 14. 15.]
  [16. 17. 18.]]

 [[19. 20. 21.]
  [22. 23. 24.]
  [25. 26. 27.]]]
t3.shape:  (3, 3, 3)
t3.ndim :  3
t4      :  [[[[  1.   2.   3.   4.]
   [  5.   6.   7.   8.]
   [  9.  10.  11.  12.]
   [ 13.  14.  15.  16.]]

  [[ 17.  18.  19.  20.]
   [ 21.  22.  23.  24.]
   [ 25.  26.  27.  28.]
   [ 29.  30.  31.  32.]]

  [[ 33.  34.  35.  36.]
   [ 37.  38.  39.  40.]
   [ 41.  42.  43.  44.]
   [ 45.  46.  47.  48.]]

  [[ 49.  50.  51.  52.]
   [ 53.  54.  55.  56.]
   [ 57.  58.  59.  60.]
   [ 61.  62.  63.  64.]]]


 [[[ 65.  66.  67.  68.]
   [ 69.  70.  71.  72.]
   [ 73.  74.  75.  76.]
   [ 77.  78.  79.  80.]]

  [[ 81.  82.  83.  84.]
   [ 85.  86.  87.  88.]
   [ 89.  90.  91.  92.]
   [ 93.  94.  95.  96.]]

  [[ 97.  98.  99. 100.]
   [101. 102. 103. 104.]
   [105. 106. 107. 108.]
   [109. 110. 111. 112.]]

  [[113. 114. 115. 116.]
   [117. 118. 119. 120.]
   [121. 122. 123. 124.]
   [125. 126. 127. 128.]]]


 [[[129. 130. 131. 132.]
   [133. 134. 135. 136.]
   [137. 138. 139. 140.]
   [141. 142. 143. 144.]]

  [[145. 146. 147. 148.]
   [149. 150. 151. 152.]
   [153. 154. 155. 156.]
   [157. 158. 159. 160.]]

  [[161. 162. 163. 164.]
   [165. 166. 167. 168.]
   [169. 170. 171. 172.]
   [173. 174. 175. 176.]]

  [[177. 178. 179. 180.]
   [181. 182. 183. 184.]
   [185. 186. 187. 188.]
   [189. 190. 191. 192.]]]


 [[[193. 194. 195. 196.]
   [197. 198. 199. 200.]
   [201. 202. 203. 204.]
   [205. 206. 207. 208.]]

  [[209. 210. 211. 212.]
   [213. 214. 215. 216.]
   [217. 218. 219. 220.]
   [221. 222. 223. 224.]]

  [[225. 226. 227. 228.]
   [229. 230. 231. 232.]
   [233. 234. 235. 236.]
   [237. 238. 239. 240.]]

  [[241. 242. 243. 244.]
   [245. 246. 247. 248.]
   [249. 250. 251. 252.]
   [253. 254. 255. 256.]]]]
t4.shape:  (4, 4, 4, 4)
t4.ndim :  4

No comments:

Post a Comment