Vectors are ordered arrays of numbers. The elements of a vector are all the same type. A vector does not, for example, contain both characters and numbers.
Here, we will represent vectors as NumPy 1-D arrays.
1-D array, shape (n,): n elements indexed [0] through [n-1]
a = np.zeros(4);
a = np.array([5,4,3,2]);
both of them above, one-dimensional vector a
with four elements.
a.shape = (4,)
indicating a 1-d array with 4 elements.
import numpy as np
a = np.arange(10);
print(a)
print(f"a[-1] = {a[-1]}")
print("a[2:7:1] = ", a[2:7:1])
a = np.array([1,2,3,4,5,6])
print(f"a : {a}")
# negate elements of a
b = -a
print(f"b = -a : {b}")
# sum all elements of a, returns a scalar
b = np.sum(a)
print(f"b = np.sum(a) : {b}")
b = np.mean(a)
print(f"b = np.mean(a): {b}")
b = a**2
print(f"b = a**2 : {b}")
a = np.array([ 1, 2, 3, 4])
b = np.array([-1,-2, 3, 4])
print(f"Binary operators work element wise: {a + b}")
a = np.array([1, 2, 3, 4])
# multiply a by a scalar
b = 5 * a
print(f"b = 5 * a : {b}")
Matrices, are two dimensional arrays.
The elements of a matrix are all of the same type.
Matrices include a second index. The two indexes describe [row, column].
X = np.array([[1],[2],[3],[4]])
a = np.zeros((1, 5))
import numpy as np
# it is a 2 Dimensional array or matrix
X = np.array([[1],[2],[3],[4]])
print(X)
w = np.array([2])
print(w)
c = np.dot(X[3], w)
print(c)
a = np.zeros((1, 5))
print(f"a shape = {a.shape}, a = {a}")
a = np.zeros((2, 1))
print(f"a shape = {a.shape}, a = {a}")
a = np.random.random_sample((1, 1))
print(f"a shape = {a.shape}, a = {a}")
a = np.array([[5], [4], [3]]);
print(f" a shape = {a.shape}, np.array: a = {a}")
a = np.arange(6).reshape(-1, 2)
print(a)
print(a[2, 0]) #Access an element
print(a[2]) #access a row
a = np.arange(20).reshape(-1, 10)
print(a)
print(a[0, 2:7:1])
print(a[:, 2:7:1])
print(a[:,:])
print(a[1,:])
print(a[1])
Reference Slicing and Indexing for more details.
Slicing creates an array of indices using a set of three values (start:stop:step).
let’s continue to Vectorization