<jupyter_start><jupyter_text>NumPy Ndarray Properties<jupyter_code>print(x.shape) # 3-dimensional data structure
print(x.ndim) # number of dimensions
print(x.dtype) # data type of all the individual elements
print(x.size) # 12 elements
print(x.nbytes) # 12*2 = 24 bytes (16 bits = 2 bytes)
print(x.T) # transpose<jupyter_output>[[[ 1  0]
  [ 4 -3]]

 [[ 2 -1]
  [ 5 -4]]

 [[ 3 -2]
  [ 6 -5]]]<jupyter_text>NumPy Constants<jupyter_code>print(np.inf) # positive infinity
print(np.NAN) # not a number
print(np.NINF) # negative infinity
print(np.NZERO) # positive zero
print(np.PZERO) # negative zero
print(np.e) # mathematical constant
print(np.euler_gamma)
print(np.pi)<jupyter_output>inf
nan
-inf
-0.0
0.0
2.718281828459045
0.5772156649015329
3.141592653589793<jupyter_text>Ones and Zeros<jupyter_code>x = np.empty([3, 3], np.uint8) # unsigned 8 bit integer
print(x) # 3x3 matrix with random values
# 1's in diagonal and 0's elsewhere
y = np.eye(5, dtype = np.uint8, k=0) # k decides the position of diagonal
print(y)
# Identity matrix
x = np.identity(5, dtype = np.uint8)
print(x)
x = np.ones((2, 5, 5), dtype = np.int16)
print(x)
x = np.zeros((2, 5, 5), dtype = np.int16)
print(x)
# An Ndarray with all elements = 4
x = np.full((3, 3, 3), dtype = np.int16, fill_value = 4)
print(x)<jupyter_output>[[[4 4 4]
  [4 4 4]
  [4 4 4]]

 [[4 4 4]
  [4 4 4]
  [4 4 4]]

 [[4 4 4]
  [4 4 4]
  [4 4 4]]]<jupyter_text>Matrix creation routines<jupyter_code># An array with ones at and below the given diagonal and zeros elsewhere
x = np.tri(5, 5, k=0, dtype = np.uint16) # k decides the position of diagonal
print(x)
x = np.ones((5,5), dtype = np.uint8)
print(x)
y = np.tril(x, k = -1) # lower triangular
print(y)
z = np.triu(x, k = -1) # upper triangular
print(z)<jupyter_output>[[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]
[[0 0 0 0 0]
 [1 0 0 0 0]
 [1 1 0 0 0]
 [1 1 1 0 0]
 [1 1 1 1 0]]
[[1 1 1 1 1]
 [1 1 1 1 1]
 [0 1 1 1 1]
 [0 0 1 1 1]
 [0 0 0 1 1]]<jupyter_text>Generating Ndarrays with Random values<jupyter_code># Single dimension array with random elements (integers)
x = np.random.randint(low = 0, high = 9, size = 10)
print(x)
# Random array of size 2x2
x = np.random.rand(3, 3)
print(x)
x = np.random.rand(2,2,2,2,2)
print(x)<jupyter_output>[3 1 6 8 8 6 3 4 8 8]
[[ 0.45077861  0.21873748  0.69434719]
 [ 0.54198292  0.63762532  0.33481276]
 [ 0.24788689  0.46260629  0.12736025]]
[[[[[ 0.89526797  0.90755097]
    [ 0.70374728  0.93365549]]

   [[ 0.53797336  0.91621877]
    [ 0.43715583  0.72205062]]]


  [[[ 0.41262713  0.44696716]
    [ 0.16522338  0.17473343]]

   [[ 0.36023849  0.95184136]
    [ 0.27656488  0.08654361]]]]



 [[[[ 0.57652475  0.85177763]
    [ 0.85737021  0.23093653]]

   [[ 0.8088866   0.69382727]
    [ 0.17663422  0.74221186]]]


  [[[ 0.56674506  0.40397094]
    [ 0.2893048   0.21294813]]

   [[ 0.71702791  0.21540947]
    [ 0.87361464  0.06957232]]]]]<jupyter_text>Array Manipulation Routines<jupyter_code>x = np.arange(6)
print(x)
y = x.reshape((3, 2)) # reshaped into 3x2 matrix
print(y)
x = np.array([[0, 1, 2],[3, 4, 5]], dtype = np.uint8)
print(x)
y = np.reshape(x,6) # value of x will be kept intact
print(y)
y = np.ravel(x) # flattens the matrix
print(y)
y = x.flatten('C') # row order by default
print(y)
y = x.flatten('F') # column order
print(y)
# ravel is faster as it doesn't occupy memory
x = np.array([1, 2, 3], dtype = np.uint8)
y = np.array([4, 5, 6], dtype = np.uint8)
print(x.dtype)
print(x)
print(y)
z = np.stack((x, y))
print(z)
z = np.stack((x, y), axis = 0) # stacking along a different axis
print(z)
z = np.stack((x, y), axis = 1)
print(z)
z = np.dstack((x, y)) # stack arrays in depth wise sequence
print(z)
print(z.shape)
z = np.hstack((x, y)) # horizontal stacking
print(z)
print(z.shape)
z = np.vstack((x, y)) # vertical stacking
print(z)
print(z.shape)
x = np.arange(9)
print(x)
a, b, c = np.split(x, 3)
print(a, b, c)
x = np.random.rand(4, 4, 4)
y, z = np.split(x, 2)
print(y, z)
y, z = np.dsplit(x, 2) # depth
print(y, z)
y, z = np.hsplit(x, 2) # horizontal split
print(y, z)
y, z = np.vsplit(x, 2) # vertical split
print(y, z)
x = np.arange(16).reshape(4, 4)
print(x)
y = np.flip(x, axis = -1)
print(y)
y = np.flip(x, axis = 0)
print(y)
y = np.flip(x, axis = 1)
print(y)
y = np.fliplr(x) # flip left to right
print(x)
print(y)
y = np.flipud(x) # flip up-down
print(x)
print(y)
y = np.roll(x, 8)
print(y)
y = np.rot90(x) # rotated entire matrix anti-clockwise by 90 degrees
print(x)
print(y)<jupyter_output>[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[[ 3  7 11 15]
 [ 2  6 10 14]
 [ 1  5  9 13]
 [ 0  4  8 12]]<jupyter_text>Bitwise operations<jupyter_code>x = np.array([0, 1, 0, 1], np.uint8)
y = np.array([0, 0, 1, 1], np.uint8)
print(np.bitwise_and(x, y))
print(np.bitwise_or(x, y))
print(np.bitwise_xor(x, y))

# Since unsigned integer of 8 bits is used,
# binary representation of 0 = 00000000 and hence 255 = 11111111
# binary representation of 1 = 00000001 and hence 254 = 11111110
print(np.bitwise_not(x))<jupyter_output>[0 0 0 1]
[0 1 1 1]
[0 1 1 0]
[255 254 255 254]<jupyter_text>Statistical functions<jupyter_code>a = np.arange(10)
print(a)
print(np.median(a))
print(np.average(a))
print(np.mean(a))
print(np.std(a))
print(np.var(a))
a = np.random.randint(low = 0, high = 10, size = 10)
print(np.median(a))
print(np.average(a))
print(np.mean(a))
print(np.std(a))
print(np.var(a))
print(np.histogram(a))<jupyter_output>(array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int64), array([ 0. ,  0.9,  1.8,  2.7,  3.6,  4.5,  5.4,  6.3,  7.2,  8.1,  9. ]))
{'fraction_non_alphanumeric': 0.17601582164688961, 'fraction_numerical': 0.14563106796116504, 'mean_word_length': 3.6826599326599325, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '67838', 'license': 'MIT', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2538, 'n_words': 713, 'path': 'NumPyPractice.ipynb', 'repo_name': 'saanikagupta/JupyterNotebooks', 'size': '5562'}