def coursera_LR(filename): f_LR = C1.load_tonumpy(filename) m, n = shape(f_LR) #get X and the lable Y, #attention please the features X, first column is all 1,so we need to make change to fit this X_feature = zeros([m, n]) X_feature[:, 0] = 1 X_feature[:, 1:-1] = f_LR[:, 0:-2] y_lable = f_LR[:, -1] #get LR model from sklearn.linear model LR = linear_model.LinearRegression() #training the LR model to get the weights of the LR LR.fit(X_feature, y_lable) weight_LR = LR.coef_ #coef_ is weight of the LR print("the training weights of the LR model is:") print weight_LR print LR.predict #ploting for better viewing #ploting together:scatter and plot_surface ax1 = plt.subplot(111, projection='3d') #build 3d project ax1.scatter(X_feature[:, 1], X_feature[:, 2], y_lable) #second subplot,showing the surface of the regression #ax2=plt.subplot(111,projection='3d') #build 3d project X = arange(500.0, 5000.0, 500.0) Y = arange(0.0, 9.0, 1.0) X, Y = meshgrid(X, Y) Z = X * weight_LR[1] + Y * weight_LR[2] ax1.plot_surface(X, Y, Z) #showing plt.show()
def Logistic_sklearn_prepare(filename): f_not_scaling = C1.load_tonumpy(filename) #need scaling f_scaling = C2.Logistic_scaling(f_not_scaling) #the first column should be all 1 f_scaling_universe = C2.Logistic_universe_form(f_scaling) return f_scaling_universe
def view_before(filename): f = C1.load_tonumpy(filename) m, n = shape(f) feature_0, feature_1 = devide(f) #data have devided into two parts! #ploting! #y=0 plt.scatter(feature_0[:, 0], feature_0[:, 1], s=m, c='r', marker='*') #y=1 plt.scatter(feature_1[:, 0], feature_1[:, 1], s=m, c='b', marker='o') #showing() plt.show()
def view_after_scaling(filename): f = C1.load_tonumpy(filename) m, n = shape(f) #scaling f_scaling = Logistic_scaling(f) #devide into two parts feature_0, feature_1 = devide(f_scaling) #ploting! #y=0 plt.scatter(feature_0[:, 0], feature_0[:, 1], s=m, c='r', marker='*') #y=1 plt.scatter(feature_1[:, 0], feature_1[:, 1], s=m, c='b', marker='o') #showing() plt.show()
def coursera_LR(filename): f_LR=C1.load_tonumpy(filename) m,n=shape(f_LR) #get X and the lable Y, #attention please the features X, first column is all 1,so we need to make change to fit this X_feature=zeros([m,n]) X_feature[:,0]=1 X_feature[:,1:-1]=f_LR[:,0:-2] y_lable=f_LR[:,-1] #get LR model from sklearn.linear model LR=linear_model.LinearRegression() #training the LR model to get the weights of the LR LR.fit(X_feature,y_lable) weight_LR=LR.coef_#coef_ is weight of the LR print("the training weights of the LR model is:" ) print weight_LR
def Logistic_prepare(filename): #not scaling! f_not_scaling=C1.load_tonumpy(filename) #devide for ploting feature_not_scaling_0,feature_not_scaling_1=C2.devide(f_not_scaling) #the first column should be all 1 m0,n0=shape(f_not_scaling) fprepare_not_scaling=zeros([m0,n0+1]) fprepare_not_scaling[:,0]=1.0 fprepare_not_scaling[:,1:]=f_not_scaling #scaling f_scaling=C2.Logistic_scaling(f_not_scaling) #devide for ploting feature_scaling_0,feature_scaling_1=C2.devide(f_scaling) m1,n1=shape(f_scaling) #the first column should be all 1 fprepare_scaling=zeros([m1,n1+1]) fprepare_scaling[:,0]=1.0 fprepare_scaling[:,1:]=f_scaling return fprepare_not_scaling,feature_not_scaling_0,feature_not_scaling_1,fprepare_scaling,feature_scaling_0,feature_scaling_1
def coursera_LR(filename): f_LR = C1.load_tonumpy(filename) m, n = shape(f_LR) #get X and the lable Y, #attention please the features X, first column is all 1,so we need to make change to fit this X_feature = zeros([m, n]) X_feature[:, 0] = 1 X_feature[:, 1] = f_LR[:, 0] y_lable = f_LR[:, 1] #get LR model from sklearn.linear model LR = linear_model.LinearRegression() #training the LR model to get the weights of the LR LR.fit(X_feature, y_lable) weight_LR = LR.coef_ #coef_ is weight of the LR print("the training weights of the LR model is:") print weight_LR #ploting for better viewing #scatter and plot is different plt.scatter(X_feature[:, 1], y_lable, color='black') plt.plot(X_feature[:, 1], LR.predict(X_feature), color='blue') plt.show()
def Logistic_prepare(filename): #not scaling! f_not_scaling=C1.load_tonumpy(filename) #devide for ploting feature_not_scaling_0,feature_not_scaling_1=C2.devide(f_not_scaling) #feature_mapping f_mapping=C2.feature_mapping(f_not_scaling) #the first column should be all 1 m,n=shape(f_mapping) fprepare_not_scaling=zeros([m,n+1]) fprepare_not_scaling[:,0]=1.0 fprepare_not_scaling[:,1:]=f_mapping ##scaling #f_scaling=C2.Logistic_scaling(f_not_scaling) ##devide for ploting #feature_scaling_0,feature_scaling_1=C2.devide(f_scaling) #m1,n1=shape(f_scaling) ##the first column should be all 1 #fprepare_scaling=zeros([m1,n1+1]) #fprepare_scaling[:,-1]=1.0 #fprepare_scaling[:,0:-1]=f_scaling return fprepare_not_scaling,feature_not_scaling_0,feature_not_scaling_1