Machine learning, self-study notes summary - multi-classification logistic regression

References: https://blog.csdn.net/Cowry5/article/details/80367832

 1 import numpy as np
 2 import pandas as pd
 3 import matplotlib.pyplot as plt
 4 from scipy.io import loadmat
 5 from scipy.optimize import minimize
 6 def load_data(path):
 7     data=loadmat(path)
 8     X=data['X']
 9     y=data['y']
10     # print(type(X),type(y))
11     return X,y
12 path=r'C:\Users\Earth\Desktop\Coursera-ML-using-matlab-python-master\machine-learning-ex3\ex3\ex3data1.mat'
13 X,y=load_data(path)
14 print(np.unique(y))
15 print(X.shape,y.shape)
16 print(len(X))
17 
18 def plotanimage():
19     pick_one=np.random.randint(0,5000)
20     image=X[pick_one,:]
21     # 选择pick_one这一行的所有列
22     fig,ax=plt.subplots(figsize=(1,1))
23     ax.matshow(image.reshape((20,20)),cmap='gray_r')
24     plt.xticks([])
25     plt.yticks([])
26     plt.show()
27     print('this should be {}'.format(y[pick_one]))
28 
29 # plotanimage()
30 def sigmoid(z):
31     return 1/(1+np.exp(-z))
32 
33 def costReg(theta,X,y,lam):
34     first=np.mean(-y*np.log(sigmoid(X@theta))-(1-y)*np.log(1-sigmoid(X@theta)))
35     theta1=theta[1:]
36     second=theta1.T@theta1*lam/ (2 * len (X-))
 37 [      # here second need is not a scalar matrix, transpose θ1 is multiplied by itself can give all the corresponding element square and, if θ1 * θ1 obtained is a vector 
38 is      return First + SECOND
 39  DEF gradient (Theta, X-, Y, Lam):
 40      First = (XT @ (Sigmoid (X-@ Theta) -Y)) / len (X-)
 41 is      Theta1 Theta = [. 1 :]
 42 is      SECOND np.concatenate = ([np.array ([0]), Lam Theta1 * / len (X-)])
 43 is      return First + SECOND
 44 is  DEF one_vs_all (X-, Y, Lam, K):
 45      all_theta np.zeros = ((K, X.shape [. 1 ]))
 46 is      for Iin range(1,K+1):
47         theta=np.zeros(X.shape[1])
48         y_i=np.array([1 if label==i else 0 for label in y])
49         ret=minimize(fun=costReg,x0=theta,args=(X,y_i,lam),method='TNC',jac=gradient,options={'disp':True})
50         all_theta[i-1,:]=ret.x
51         # 序列为i-1的行,所有列替换成ret.x
52     return all_theta
53 def predict_all(X,all_theta):
54     h=sigmoid(X@all_theta.T)
55     h_argmax=np.argmax(h,axis=1)
56     h_argmax=h_argmax+1
57     print('the type of argmax is ',type(h_argmax))
58     print('the shape of argmax is',h_argmax.shape)
59     return h_argmax
60 raw_X,raw_y=load_data(path)
61 X=np.insert(raw_X,0,1,axis=1)
62 print('the shape of X is',X.shape)
63 #raw_X to modify the object 0 to be inserted before the 0-th row or column objects, value to be inserted, axis = dimension to insert 1, if 0 is inserted into the row, it is an insertion of a column 
64 Y = raw_y.flatten ()
 65  Print ( ' The Shape of IS Y ' , y.shape)
 66 all_theta one_vs_all = (X-, Y, 1,10 )
 67  Print ( ' The Shape of All Theta IS ' , all_theta.shape)
 68 = Result predict_all (X-, all_theta)
 69  Print (result.shape)
 70  DEF cal_accuracy (Result, Y):
 71 is      RES = [. 1 IF Result [I] == Y [I] the else 0 for I in range(len(y))]
72     return np.mean(res)
73 print(result==y)
74 # print(cal_accuracy(result,y))
75 # theta2=np.zeros((10,X.shape[1]))
76 # theta2 = theta2[1:]
77 # print(theta2.shape)
78 # t=(theta2@theta2) / (2*len(X))
79 # print(t.shape)
80 # t1=theta2*theta2
81 # print(t1.shape)

Idea Summary:

1, first read from the specified data to X, y, according to the initialization requires a θ

2, assume write function and the cost function, wherein the normalized should be noted that the first penalty cost function is not [theta] 0 , [theta] is therefore necessary to vary treatment

3, write the gradient function according to the formula

4, θ be optimized using advanced algorithms, to note that multi-classification problems are optimized according to each class, and finally the value assigned to θ, the middle can be used for cycles to achieve

5, good training θ later generations assume their function, for multi-classification problems, think the biggest value is the probability of predicted category

6, to predict the results of statistical processing, and then the actual value, the calculation accuracy rate

 

Some commonly used functions found on the numpy grasp not impressive enough, including slicing, dimension reduction and other operations, in addition to drawing this one more strange in the course of practice, the need to address gaps

When you apply the formula of multiplying matrices, for whom and who multiplied, the need to transpose sometimes respond, however, the best way is to play to operate according to their shape matrix multiplication characteristics.

Guess you like

Origin www.cnblogs.com/flyingtester/p/10993135.html