import numpy as np
np.random.seed(0)
# Creating a one dimensional array.
one = np.random.randint(10, size = 6)
print(one)
[5 0 3 3 7 9]
# Creating a two dimensional array.
two = np.random.randint(10, size = (2, 5))
print(two)
[[3 5 2 4 7] [6 8 8 1 6]]
# Creating a three dimensional array.
three = np.random.randint(10, size = (3, 4, 5))
print(three)
[[[7 7 8 1 5] [9 8 9 4 3] [0 3 5 0 2] [3 8 1 3 3]] [[3 7 0 1 9] [9 0 4 7 3] [2 7 2 0 0] [4 5 5 6 8]] [[4 1 4 9 8] [1 1 7 9 9] [3 6 7 2 0] [3 5 9 4 4]]]
# Using the attributes to determine the dimension of the arrays.
print("one ==>", one.ndim )
print("two ==>", two.ndim )
print("three ==>", three.ndim )
one ==> 1 two ==> 2 three ==> 3
print("one ==>", one.shape)
print("two ==>", two.shape)
print("three ==>", three.shape)
one ==> (6,) two ==> (2, 5) three ==> (3, 4, 5)
# Using the attribute to determine the size of the arrays.
print("one ==>", one.size)
print("two ==>", two.size)
print("three ==>", three.size)
one ==> 6 two ==> 10 three ==> 60
# Using the attribute to determine the datatype of the arrays.
print("one ==>", one.dtype)
print("two ==>", two.dtype)
print("three ==>", three.dtype)
one ==> int64 two ==> int64 three ==> int64
# Using the attribute to determine the item size of the arrays.
print("one ==>", one.itemsize)
print("two ==>", two.itemsize)
print("three ==>", three.itemsize)
one ==> 8 two ==> 8 three ==> 8
print("one ==>", one.nbytes)
print("two ==>", two.nbytes)
print("three ==>", three.nbytes)
one ==> 48 two ==> 80 three ==> 480
# Array Indexing: Accessing Single Elements
oneValue = one[2]
print(oneValue)
3
# Accessing the elements in a multi dimensional array
print(two)
twoValue = two[0, 4]
print(twoValue)
[[3 5 2 4 7] [6 8 8 1 6]] 7
# Changing the values of the array
print(one)
one[3] = 6
print(one)
one[4] = 4.56 # It will be silently truncated
print(one)
[5 0 3 3 7 9] [5 0 3 6 7 9] [5 0 3 6 4 9]
# Array Slicing: Accessing Subarrays
# The syntax of the slicing is ==> x[start:stop:step] ==> default value is:
# [0: size of the dimension: 1]
a = np.random.randint(10, size = (10))
print(a)
print("------------------------")
print(a[4: 9: 2])
print("------------------------")
print(a[1::2])
print("------------------------")
print(a[::2])
print("------------------------")
print(a[:9:])
print("------------------------")
# This is the best example for reversing an array
print(a[::-1])
print("------------------------")
# I tries on strings and it worked !!!
a = "University of Regina is awesome !!!"
print(a)
print("------------------------")
print(a[::-1])
[6 4 4 3 4 4 8 4 3 7] ------------------------ [4 8 3] ------------------------ [4 3 4 4 7] ------------------------ [6 4 4 8 3] ------------------------ [6 4 4 3 4 4 8 4 3] ------------------------ [7 3 4 8 4 4 3 4 4 6] ------------------------ University of Regina is awesome !!! ------------------------ !!! emosewa si anigeR fo ytisrevinU
print(two)
print("------------------------")
twoValue = two[:2, :3]
print(twoValue)
print("------------------------")
twoValue = two[:2, ::3]
print(twoValue)
print("------------------------")
twoValue = two[::-1, ::-1]
print(twoValue)
[[3 5 2 4 7] [6 8 8 1 6]] ------------------------ [[3 5 2] [6 8 8]] ------------------------ [[3 4] [6 1]] ------------------------ [[6 1 8 8 6] [7 4 2 5 3]]
# Accessing array rows and columns.
print(two)
print("------------------------")
print(two[:, 0]) # first coloumn
print("------------------------")
print(two[0, :]) # first row
print("------------------------")
print(two[0]) # Entire row
[[3 5 2 4 7] [6 8 8 1 6]] ------------------------ [3 6] ------------------------ [3 5 2 4 7] ------------------------ [3 5 2 4 7]
# Modifing the array without using copy() function.
print(two)
print("------------------------")
twoValue = two[:2, :2]
print(twoValue)
print("------------------------")
twoValue[0, 0] = 25
print(twoValue)
print("------------------------")
print(two)
[[3 5 2 4 7] [6 8 8 1 6]] ------------------------ [[3 5] [6 8]] ------------------------ [[25 5] [ 6 8]] ------------------------ [[25 5 2 4 7] [ 6 8 8 1 6]]
# Modifing the array with using copy() function.
print(two)
print("------------------------")
twoValue = two[:2, :2].copy()
print(twoValue)
print("------------------------")
twoValue[0, 0] = 45
print(twoValue)
print("------------------------")
print(two)
[[25 5 2 4 7] [ 6 8 8 1 6]] ------------------------ [[25 5] [ 6 8]] ------------------------ [[45 5] [ 6 8]] ------------------------ [[25 5 2 4 7] [ 6 8 8 1 6]]
# Reshaping of Arrays using reshape() in a more advanced way
grid = np.arange(1, 10).reshape(3, 3)
print(grid)
[[1 2 3] [4 5 6] [7 8 9]]
# Reshaping the arrays using newaxis
print(one)
print("------------------------")
print(one.reshape(3, 2))
print("------------------------")
print(one[ np.newaxis, :])
print("------------------------")
print(one[:, np.newaxis])
[5 0 3 6 4 9] ------------------------ [[5 0] [3 6] [4 9]] ------------------------ [[5 0 3 6 4 9]] ------------------------ [[5] [0] [3] [6] [4] [9]]
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate([a, b])
print(c)
print("------------------------")
d = np.array([7, 8, 9])
e = np.concatenate([c, d])
print(e)
print("------------------------")
print(two)
print("------------------------")
f = np.concatenate([two, two])
print(f)
print("------------------------")
# concatenate along the second axis (zero-indexed)
g = np.concatenate([two, two], axis = 1)
print(g)
print("------------------------")
[1 2 3 4 5 6] ------------------------ [1 2 3 4 5 6 7 8 9] ------------------------ [[25 5 2 4 7] [ 6 8 8 1 6]] ------------------------ [[25 5 2 4 7] [ 6 8 8 1 6] [25 5 2 4 7] [ 6 8 8 1 6]] ------------------------ [[25 5 2 4 7 25 5 2 4 7] [ 6 8 8 1 6 6 8 8 1 6]] ------------------------
a = np.array([1, 2, 3])
print(a)
print("------------------------")
b = np.array([[4, 5, 6],
[7, 8, 9]])
print(b)
print("------------------------")
print(np.vstack([a, b]))
print("------------------------")
# horizontally stack the arrays
y = np.array([[99],
[99]])
print(np.hstack([b, y]))
print("------------------------")
[1 2 3] ------------------------ [[4 5 6] [7 8 9]] ------------------------ [[1 2 3] [4 5 6] [7 8 9]] ------------------------ [[ 4 5 6 99] [ 7 8 9 99]] ------------------------
# Splitting of arrays
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
print(a)
print("------------------------")
b, c, d = np.split(a, [2, 5])
print(b, c, d)
[1 2 3 4 5 6 7 8 9 0] ------------------------ [1 2] [3 4 5] [6 7 8 9 0]
grid = np.arange(16).reshape(4, 4)
print(grid)
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]]
upper, lower = np.vsplit(grid, [2])
print(upper)
print("------------------------")
print(lower)
[[0 1 2 3] [4 5 6 7]] ------------------------ [[ 8 9 10 11] [12 13 14 15]]
left, right = np.hsplit(grid, [2])
print(left)
print("------------------------")
print(right)
[[ 0 1] [ 4 5] [ 8 9] [12 13]] ------------------------ [[ 2 3] [ 6 7] [10 11] [14 15]]