This article is mainly to share with you about how Python programming to distinguish the linearity, the need for small partners to look at.
"" "Author:victoriacreated on:2017.9.15 11:45" "" Import pandas as Pdimport NumPy as Npimport matplotlib.pyplot as Pltdef L DA (X0, X1): "" "Get the optimal params of LDA model given training data. Input:X0:np.array with shape [N1, d] X1:np.array with shape [N2, d] Return:omega:np.array wit h shape [1, d]. Optimal params of LDA. "" "#shape [1, d] mean0 = Np.mean (X0, axis=0, keepdims=true) mean1 = Np.mean (X1, axis=0, keepdims=true) Sw = ( X0-MEAN0). T.dot (X0-mean0) + (X1-MEAN1). T.dot (x1-mean1) Omega = Np.linalg.inv (Sw). dot (mean0-mean1). T) return omegaif __name__== "__main__": #read data from xls Work_book = Pd.read_csv (".. /data/watermelon_3a.csv ", header=none) Positive_data = work_book.values[work_book.values[:,-1] = = 1.0,:] Negative_ data = work_book.values[work_book.values[:,-1] = = 0.0,:] Print (positive_data) #LDA Omega = LDA (negative_data[: , 1:-1], positive_data[:, 1:-1]) #plot Plt.plot (positive_data[:, 1], positive_data[:, 2], "Bo") Plt.plot (negative_data[:, 1], negative_data[:, 2], "r+") lda_left = 0 Lda_right =-(omega[0]*0.9)/omega[1] Plt.plot ([0, 0.9], [Lda_left, Lda_right], ' G ') plt.xlabel (' Density ') plt.ylabel (' s Ugar rate ') plt.title ("LDA") plt.show ()
Related recommendations:
A concise introductory guide to linear discriminant analysis
Linear discriminant Analysis