Python教程

python机器学习数学基础

本文主要是介绍python机器学习数学基础,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Lab 1: Python exercises for COMP24111

1. Instruction

There are a total of 12 exercises. You are asked to complete all of them. Print out the final results, and comment if needed. Make sure your results can be reproduced by your TA using Restart & Run All.

2. Exercises

Start with our provided supporting tutorial “Instruction on Mathematical Programming in Python”, if needed, also the file “Maths Knowledge Overview - COMP24112”. After you are familiar with the basics, you can proceed to the exercises.

Exercise 1

Input a 4 by 4 magic square matrix
输入4×4魔方矩阵
A = [ 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 ] . \textbf{A}= \left[ \begin{array}{cccc} 16 & 3 & 2 & 13 \\ 5 & 10 & 11 & 8 \\ 9 & 6 & 7 & 12 \\ 4 & 15 & 14 & 1 \\ \end{array} \right]. A=⎣⎢⎢⎡​16594​310615​211714​138121​⎦⎥⎥⎤​.

Generate a 4 by 4 matrix B \textbf{B} B with random integer entries. Set C \textbf{C} C to be a 4 by 4 matrix with 1’s on the diagonal and 0’s elsewhere. Check to see that, A \textbf{A} A, B \textbf{B} B and C \textbf{C} C are all present. Compute the sum of the third column of A \textbf{A} A.
随机生成4×4的由整数构成的矩阵 B \textbf{B} B。生成对角线是1其他地方是0的4×4矩阵 C \textbf{C} C。计算出矩阵 A \textbf{A} A第三列的和。
import numpy as np
A = np.array([[16,3,2,13],[5,10,11,8],[9,6,7,12],[4,15,14,1]])
B = np.random.randint(low=0,high=20,size=16).reshape(4,4)
C = np.diag([1, 1, 1, 1])
sumOfTheThirdColumnOfA = np.sum(A,axis=1)[2]
print(sumOfTheThirdColumnOfA)

Exercise 2

Generate a 4 by 2 matrix consisting of the odd-numbered columns of B \textbf{B} B using the colon operator.
用冒号操作矩阵 B \textbf{B} B的奇数列,生成4*2矩阵
print(B)
matrix = B[:,::2]
print(matrix)

Exercise 3

Generate a 4 by 2 matrix containing two different columns that are randomly selected from the columns of B \textbf{B} B. Generate a 3 by 4 matrix containing three different rows that are randomly selected from the rows of B \textbf{B} B.
随机从矩阵 B \textbf{B} B中选取2列生成4×2矩阵。随机从矩阵 B \textbf{B} B中选3行生成3×4矩阵。
print(B)
matrix1 = B[:,np.random.permutation(4)[:2]]
print(matrix1)
matrix2 = B[np.random.permutation(4)[:3],:]
print(matrix2)

Exercise 4

The matrix A \textbf{A} A is a magic square, in which the sum of each row, each column, and each diagonal is the same. First check whether A \textbf{A} A satisfies this magic square condition, then sort the 2nd row of A \textbf{A} A by using appropriate commands.
矩阵 A \textbf{A} A是一个魔方矩阵,即所有行、列以及对角线上的和相同。首先验证 A \textbf{A} A是否符合魔方矩阵的条件,然后用相近的命令对 A \textbf{A} A的第2行进行排序。
#np.allclose方法:判断两个数组中是否所有元素都相等
if (np.allclose(np.sum(A,axis=0),np.sum(A,axis=1))) & (np.allclose(np.sum(A,axis=0),np.sum(np.diagonal(A)))):
print(“A satisfies the magic square condition!”)
print(A)
print(np.sort(A,axis=1)[1])

Exercise 5

Set B \textbf{B} B as an identity matrix, multiplication of A \textbf{A} A by B \textbf{B} B should yield A \textbf{A} A. Check this. What does component by component multipilication give? Check this. B \textbf{B} B是单位矩阵, 逐项乘得到什么?检查这个。
B = np.eye(4)
print(np.allclose(A@B,A))

Exercise 6

Count the number of values of A \textbf{A} A which are greater than 10 using logical operators.
print(A)
print(“A中元素大于10的数量为:” ,np.count_nonzero(A>10))

Exercise 7

The reciprocal of a number x x x is 1 / x 1/x 1/x. Use for loops to create a new matrix B \textbf{B} B, where each element in B \textbf{B} B is the reciprocal of the corresponding element in A \textbf{A} A, and sum the columns of B \textbf{B} B. Now create the same matrix B \textbf{B} B and calculate its column sums in a single operation, using matrix commands, without a for loop.
for i in range(4):
for j in range(4):
B[i,j] = np.reciprocal(A[i,j].astype(np.float32))
j+=1
i+=1
print(“用for循环:”)
print(“A矩阵”,A)
print(“B矩阵”,B)
B = np.reciprocal(A.astype(np.float32))
print(“不用for循环:”)
print(“A矩阵”,A)
print(“B矩阵”,B)

Exercise 8

Plot the log and square root of the integers from 1 1 1 to 100 100 100 in one single figure. Provide axis names, legend and title for the figure.
from matplotlib import pyplot as plt
x = np.arange(1,101)
y_log = np.log(x)
plt.plot(x, y_log,label=‘log’)
y_sqrt = np.sqrt(x)
plt.plot(x,y_sqrt,label=“sqrt”)
plt.legend()
plt.xlabel(“x”)
plt.ylabel(“y”)
plt.title(“the log and square root of the integers from 1 to 100”)
plt.show()

Exercise 9

Let $\boldsymbol{y} $ be an N N N-dimensional column vector with y i y_i yi​ denoting its i i i-th element.
Let X \textbf{X} X be an N N N by D D D matrix with x i \boldsymbol{x}_{i} xi​ denoting its i i i-th column.
Let w \boldsymbol{w} w be a D D D-dimensional column vector.
Choose a value for N N N and a value for D D D as you like, generate y \boldsymbol{y} y, X \textbf{X} X and w \boldsymbol{w} w containing random elements.
Use matrix operations and for loops to verify the following two quantities are equal.
g 1 = X T X w − X T y \boldsymbol{g}_1 = \textbf{X}^T\textbf{X} \boldsymbol{w}-\textbf{X}^T \boldsymbol{y} g1​=XTXw−XTy
g 2 = ∑ i = 1 N ( x i T w − y i ) x i \boldsymbol{g}_2 = \sum_{i=1}^N \left(\boldsymbol{x}_{i}^T \boldsymbol{w}- y_i\right) \boldsymbol{x}_{i} g2​=i=1∑N​(xiT​w−yi​)xi​

#令N=3,D=4
N = 3
D = 4
y = np.random.randint(low=0,high=10,size=N)[:,None]
X = np.random.randint(low=0,high=10,size=12).reshape(N,D)
w = np.random.randint(low=0,high=10,size=D)[:,None]

g1 = X.T@X@w-X.T@y

print(“g1”,g1)
g2 = 0
for i in range(N):

g2 += (X[i,:]@w-y[i,:])*(X[i,:][:,None])

print(“g2”,g2)
print(g1==g2)

Exercise 10

Calculate the derivative of function f ( x ) = x 2 + 3 x + 1 f(x) = x^2+3x+1 f(x)=x2+3x+1 at 10 randomly chosen values of x x x. Write a user-defined function for calculating an approximated derivative of an input function based on

d f ( x ) d x = f ( x + Δ x ) − f ( x ) Δ x   . \frac{d f(x)}{d x}=\frac{f(x+\Delta x)-f(x)}{\Delta x} ~ . dxdf(x)​=Δxf(x+Δx)−f(x)​ .

Use this function to calculate the approximated derivative for f ( x ) = x 2 + 3 x + 1 f(x) = x^2+3x+1 f(x)=x2+3x+1 at the same 10 chosen values of x x x for Δ x = 1 \Delta x = 1 Δx=1, Δ x = 0.1 \Delta x = 0.1 Δx=0.1 and Δ x = 0.001 \Delta x = 0.001 Δx=0.001. Plot your derivatives and the approximated derivatives in the same figure. Which Δ x \Delta x Δx gives you a better approximation?
x = np.random.randint(1,50,size=10)
df = 2x+3
print(“10个随机数为:”,x)
print(“10个随机数在f(x)下的导数为:”,df)
def approximated(x,deltaX):
return ((x+deltaX)2+3(x+deltaX)+1-(x*2+3
x+1))/deltaX
y1 = approximated(x,1)
y2 = approximated(x,0.1)
y3 = approximated(x,0.001)
print(y1)
print(y2)
print(y3)
plt.plot(x,df,label=‘without approximated’,color=‘black’)
plt.plot(x,y1,label=‘deltaX=1’,color=‘red’)
plt.plot(x,y2,label=‘deltaX=0.1’,color=‘blue’)
plt.plot(x,y3,label=‘deltaX=0.001’,color=‘yellow’)
plt.legend()
plt.show()

Exercise 11

Given two d d d-dimensional data points x = [ x 1 , x 2 , … , x d ] \boldsymbol{x} = [x_1,x_2,\dots,x_d] x=[x1​,x2​,…,xd​] and y = [ y 1 , y 2 , … , y d ] \boldsymbol{y}=[y_1,y_2,\dots,y_d] y=[y1​,y2​,…,yd​]. The Euclidean distance between these two points is computed as

d ( x , y ) = ∑ i = 1 d ( x i − y i ) 2 = x x T − 2 x y T + y y T . d(\boldsymbol{x},\boldsymbol{y}) = \sqrt{\sum_{i=1}^d (x_i-y_i)^2} = \sqrt{\boldsymbol{x}\boldsymbol{x}^T - 2\boldsymbol{x}\boldsymbol{y}^T+\boldsymbol{y}\boldsymbol{y}^T}. d(x,y)=i=1∑d​(xi​−yi​)2 ​=xxT−2xyT+yyT ​.

Generate 5 5 5 random 3 3 3-dimensional points and store them in a 5 × 3 5\times 3 5×3 matrix called A \textbf{A} A ( 5 5 5 rows and 3 3 3 columns). Generate another 8 8 8 random 3 3 3-dimensional points and store them in a 8 × 3 8\times 3 8×3 matrix called B \textbf{B} B ( 8 8 8 rows and 3 3 3 columns). Calculate the Euclidean distance between the 5 5 5 and 8 8 8 points, and store these distances in a 5 × 8 5\times 8 5×8 matrix called D \textbf{D} D using for loops ( 5 5 5 rows and 8 8 8 colums).
A = np.random.randint(20,size=15).reshape(5,3)
B = np.random.randint(30,size=24).reshape(8,3)
print(A)
print(B)
row = A.shape[0]
column = A.shape[1]
D = np.ones([5,8])
B = B
#循环
for i in range(5):
for k in range(8):
a = A[i,:]
b = B[k,:]
D[i,k] = np.sqrt(np.sum((a-b)**2))
print(D.shape)
print(D)

Exercise 12

Repeat Exercise 11 using matrix operations only, without loops.
m = np.shape(A)[0]
n = np.shape(B)[0]

求得矩阵M为 m*n维

M = np.dot(A, B.T)

对于H,我们只需要A . A^T的对角线元素

np.square(A)是A中都每一个元素都求平方

np.square(A).sum(axis=1) 是将每一行都元素都求和,axis是按行求和(原因是行向量)

np.matrix() 是将一个列表转为矩阵,该矩阵为一行多列

求矩阵都转置,为了变成一列多行

np.tile是复制,沿Y轴复制1倍(相当于没有复制),再沿X轴复制n倍

H = np.tile(np.matrix(np.square(A).sum(axis=1)).T,(1,n))

对于H,我们只需要B . B^T的对角线元素

np.square(B)是B中都每一个元素都求平方

np.square(B).sum(axis=1) 是将每一行都元素都求和,axis是按行求和(原因是行向量)

np.matrix() 是将一个列表转为矩阵,该矩阵为一行多列

np.tile是复制,沿Y轴复制m倍(相当于没有复制),再沿X轴复制1倍

K = np.tile(np.matrix(np.square(B).sum(axis=1)),(m,1))

H对M在y轴方向上传播,即H加和到M上的第一行,K对M在x轴方向上传播,即K加和到M上的每一列

print(np.sqrt(-2 * M + H + K))

这篇关于python机器学习数学基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!