NymPy Matrix

Arange

#arange
vec1 = np.arange(0,11)
print("vec1={}".format(vec1))

vec1 = np.arange(0,10, step=2)
print("vec1={}".format(vec1))

#results:
vec1=[ 0 1 2 3 4 5 6 7 8 9 10]
vec1=[0 2 4 6 8]

Linspace

#linspace
vec2 = np.linspace(0, 5, num=5)
print("vec2={}".format(vec2))

vec2 = np.linspace(0,5,num=5,endpoint=False)
print("vec2={}".format(vec2))

vec2 = np.linspace(0,5,num=10,endpoint=True)
print("vec2={}".format(vec2))

results:
vec2=[0. 1.25 2.5 3.75 5. ]
vec2=[0. 1. 2. 3. 4.]
vec2=[0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778
3.33333333 3.88888889 4.44444444 5. ]

Other methods

#ones,eye,empty, full,random
vec3 = np.ones((3,3))
print("vec3=""{}".format(vec3))

vec3 = np.empty((3,3))
print("vec3=""{}".format(vec3))

vec3 = np.full((3,2), 7)
print("vec3=""{}".format(vec3))

vec3 = np.eye(3) *9.9
print("vec3=""{}".format(vec3))

vec3 = np.random.random((4,5))
print("vec3=""{}".format(vec3))

results:
ad1. vec3=
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
ad.3 vec3=
[[7 7]
[7 7]
[7 7]]
ad.4 vec3=
[[9.9 0. 0. ]
[0. 9.9 0. ]
[0. 0. 9.9]]
ad.5 vec3=
[[0.04031738 0.43108875 0.50419445 0.21100702 0.28018735]
[0.83468929 0.68595346 0.1951048 0.64249235 0.4666575 ]
[0.41041352 0.26075428 0.72763207 0.53344999 0.96080089]
[0.0299817 0.25032282 0.1049782 0.7117831 0.42239281]]

Matrix from array compliations

#matrix
m = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print("m=""{}".format(m))
row1 = m[1, :]
row2 = m[:2, :]
print("row1=""{}".format(row1))  # Prints "[1,2,3,4]
print("row2=""{}".format(row2))  # Prints "[5,6,7,8]
print(np.array([m[0, 0], m[1, 1], m[2, 0]]))

results:
m=
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
row1=[5 6 7 8]
row2=[[1 2 3 4]
[5 6 7 8]]
[1 6 9]

m1 = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
m2 = np.array([1, 0, 1])
m = m1 + m2  # Add v to each row of x using broadcasting
print("m=""{}".format(m))

m=
[[ 2 2 4] #1+1, 2+0, 3+1
[ 5 5 7] #4+1,5+0,6+1
[ 8 8 10] #7+1,8+0,9+1
[11 11 13]] #10+1,11+0,12+1

NumPy Arrays

list = [2, 4, 6, 8, 14, 16]
vec1 = np.array(list)
print("vec1={}".format(vec1))
print(type(vec1))
print("vec1 shape={}".format(vec1.shape))
print(vec1.dtype)

#results:
vec1=[ 2 4 6 8 14 16]
class ‘numpy.ndarray’
vec1 shape=(6,)
int32

vec1 = np.array([1, 2, 3, 4], ndmin=5)
print("vec1={}".format(vec1))
print('number of dimensions :', vec1.ndim)

#results:
vec1=[[[[[1 2 3 4]]]]]
number of dimensions : 5

vec2 = np.array(list[:3])
print("vec2 = {}".format(vec2))
vec3 = np.array(list[3:])
print("vec3 = {}".format(vec3))
print("vec23 =", np.array(vec2),np.array(vec3))
print("vec2+vec3 =", np.array(vec2+vec3))

#results:
vec2 = [2 4 6]
vec3 = [ 8 14 16]
vec23 = [2 4 6] [ 8 14 16]
vec2+vec3 = [10 18 22] #2+8,4+14,6+16

vec4 = np.array([[1,2,3], [3,6,9]])
print('2nd element on 1st dim: ', vec4[0, 1])
print('3rd element on 2nd dim: ', vec4[1, 2])

#results:
2nd element on 1st dim: 2
3rd element on 2nd dim: 9

arr1 = np.array([1,3,5,7,9])
arr2 = (arr1.copy()) *2
arr3 = arr2 + arr1
arr4 = arr3.view()
arr4[0] = 11
print(arr1)
print(arr2)
print(arr3)
print(arr4)

#results:
[1 3 5 7 9]
[ 2 6 10 14 18]
[11 9 15 21 27]
[11 9 15 21 27]

Loops in one-, two-, three-dimensional arrays

arr1 = np.array([1, 2, 3])
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
arr3 = np.array([[[1, 2, 3], [4, 5, 6]],
                 [[7, 8, 9], [10, 11, 12]]])

for x in arr1:
      print(x)

for x in arr2:
  for y in x:
      print(y)

for x in arr3:
  for y in x:
      for z in y:
          print(z)

Join, split, sort

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr3 = np.array([[3,2,1],[9,8,7]])
arr = np.concatenate((arr1, arr2))
arr_sp = np.array_split(arr,2)
print(arr)
print(arr_sp)
print(np.sort(arr3))

#Results:
[1 2 3 4 5 6]
[array([1, 2, 3]), array([4, 5, 6])]
[[1 2 3]
[7 8 9]]