The title is too long it! Document Download [ portal ]
Question 1
Description: Implementing logistic regression.
Here minimize the use of Matlab function instead of fminunc function, refer to the blog [ Portal ].
1 import numpy as np 2 import matplotlib.pyplot as plt 3 import scipy.optimize as op 4 5 #S函数 6 def sigmoid(z): 7 g = 1/(1+np.exp(-z)) 8 return g 9 10 #cost计算函数 11 def costFunction(theta, X, y): 12 theta = np.array(theta).reshape((np.size(theta),1)) 13 m = np.size(y) 14 h = sigmoid(np.dot(X, theta)) 15 . 1 = J / m * (- np.dot (yT, np.log (H)) - np.dot ((. 1-yT), np.log (l- H))) 16 return J.flatten () . 17 18 is DEF gradient (Theta, X-, Y): . 19 Theta = np.array (Theta) .reshape ((np.size (Theta),. 1 )) 20 is m = np.size (Y) 21 is H = Sigmoid (NP. DOT (X-, Theta)) 22 is Grad = 1 / m * np.dot (XT, H - Y) 23 is return grad.flatten () 24 25 26 is # read data, score 1 is the first column, the second column is score 2, the third column is Yes / NO 27 Data = np.loadtxt ( ' ex2data1.txt ' , DELIMITER = ',', dtype='float') 28 m = np.size(data[:, 0]) 29 # print(data) 30 31 #绘制样本点 32 X = data[:, 0:2] 33 y = data[:, 2:3] 34 pos = np.where(y == 1)[0] 35 neg = np.where(y == 0)[0] 36 X1 = X[pos, 0:2] 37 X0 = X[neg, 0:2] 38 plt.plot(X1[:, 0], X1[:, 1], 'k+') 39 plt.plot(X0[:, 0], X0[:, 1], 'yo') 40 plt.xlabel('Exam 1 score') 41 plt.ylabel('Exam 2 score') 42 43 #求解最优解 44 one = np.ones(m) 45 X = np.insert(X, 0, values=one, axis=1) 46 initial_theta = np.zeros(np.size(X, 1)) 47 result = op.minimize(fun=costFunction, x0=initial_theta, args=(X, y), method='TNC', jac=gradient) 48 # print(result) 49 theta = result.x 50 cost = result.fun 51 print('theta:', theta) 52 print('cost:', cost) 53 54 #绘制决策边界 55 plot_x = np.array([np.min(X[:, 1]), np.max(X[:, 2])]) 56 # print(plot_x) 57 plot_y = (-1/theta[2])*(theta[1]*plot_x+theta[0]) 58 # print(plot_y) 59 plt.plot(plot_x,plot_y) 60 plt.legend(labels=['Admitted', 'Not admitted' ]) 61 is plt.axis ([30, 100, 30, 100 ]) 62 is plt.show () 63 is 64 # forecast [4585] student performance, and accuracy of calculation 65 Theta = np.array (Theta). the RESHAPE ((np.size (Theta),. 1 )) 66 Z = np.dot ([. 1, 45, 85 ], Theta) 67 Prob = Sigmoid (Z) 68 Print ( ' the For A Student Scores with 45 and 85, Predict AN Admission Probability of WE ' , Prob) 69 P = np.round (Sigmoid (np.dot (X-, Theta))) 70 ACC = np.mean (P == Y) * 100 71 is Print ( ' Train the Accuracy:',acc,'%')
operation result:
Problem 2
Description: normalization implemented by logistic regression.
1 import numpy as np 2 import matplotlib.pyplot as plt 3 import scipy.optimize as op 4 5 #S函数 6 def sigmoid(z): 7 g = 1/(1+np.exp(-z)) 8 return g 9 10 #cost计算函数 11 def costFunction(theta, X, y, lamb): 12 theta = np.array(theta).reshape((np.size(theta), 1)) 13 m = np.size(y) 14 h = sigmoid(np.dot(X, theta)) 15 J = 1/m*(-np.dot(y.T, np.log(h)) - np.dot((1-y.T), np.log(1-h))) 16 # 添加项 17 theta2 = theta[1:, 0] 18 Jadd = lamb/(2*m)*np.sum(theta2**2) 19 J = J + Jadd 20 return J.flatten() 21 22 #求梯度 23 def gradient(theta, X, y, lamb): 24 theta = np.array(theta).reshape((np.size(theta), 1)) 25 m = np.size(y) 26 h = sigmoid(np.dot(X, theta)) 27 grad = 1/m*np.dot(X.T, h - y) 28 #添加项 29 theta[0,0] = 0 30 gradadd = lamb/m*theta 31 grad = grad + gradadd 32 return grad.flatten() 33 34 #求特征矩阵 35 def mapFeature(X1, X2): 36 degree = 6 37 out = np.ones((np.size(X1),1)) 38 for i in range(1, degree+1): 39 for j in range(0, i+1): 40 res = np.multiply(np.power(X1, i-J), np.power (an X2, J)) 41 is OUT = np.insert (OUT, np.size (OUT [0,:]), values RES =, = Axis. 1 ) 42 is return OUT 43 is 44 is 45 46 is # read data fetch, score 1 is the first column, the second column is the score 2, the third column is Yes / NO 47 data = np.loadtxt ( ' ex2data2.txt ' , DELIMITER = ' , ' , DTYPE = ' a float ' ) 48 = m np.size (Data [:, 0]) 49 50 # draw sample point 51 is X-= Data [:, 0: 2 ] 52 is Y = Data [2 :,:. 3 ] 53 is pos = np.where(y == 1)[0] 54 neg = np.where(y == 0)[0] 55 X1 = X[pos, 0:2] 56 X0 = X[neg, 0:2] 57 plt.plot(X1[:, 0], X1[:, 1], 'k+') 58 plt.plot(X0[:, 0], X0[:, 1], 'yo') 59 plt.xlabel('Microchip Test 1') 60 plt.ylabel('Microchip Test 2') 61 plt.legend(labels=['y = 1', 'y = 0']) 62 is 63 is # initialized data 64 X-mapFeature = (X-[:, 0], X-[:,. 1 ]) 65 # Print (X-) 66 Lamb. 1 = 67 initial_theta = np.zeros (np.size (X-,. 1 )) 68 69 # to find the optimal solution 70 Result = op.minimize (Fun = costFunction, X0 = initial_theta, args = (X-, Y, Lamb), Method = ' the TNC ' , JAC = gradient) 71 is # Print (Result) 72 cost = result.fun 73 is Theta = result.x 74 Print ( ' Theta: ', theta) 75 print('cost:', cost) 76 77 #绘制决策边界 78 u = np.linspace(-1, 1.5, 50) 79 v = np.linspace(-1, 1.5, 50) 80 z = np.zeros((np.size(u),np.size(v))) 81 theta = np.array(theta).reshape((np.size(theta), 1)) 82 for i in range(0, np.size(u)): 83 for j in range(0, np.size(v)): 84 z[i, j] = np.dot(mapFeature(u[i], v[j]), theta) 85 #Print (Z) 86 plt.contour (U, V, zT, [0]) 87 plt.show () 88 89 # calculation accuracy of 90 P = np.round (Sigmoid (np.dot (X-, Theta))) 91 is ACC = np.mean (P == Y) * 100 92 Print ( ' Train the Accuracy: ' , ACC, ' % ' )
operation result: