手写
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt
from sklearn.linear_model import *
from sklearn.metrics import *
from sklearn import preprocessing
from numpy import genfromtxt
import random
from sklearn import *
from sklearn.preprocessing import *
def get_vertix(x_data,y_data):
print ("get_vertix.\n")
#******************类别为0********************
x0=[]
x1=[]
#******************类别为1********************
y0=[]
y1=[]
for i in range(len(y_data)):
if y_data[i]==0:
x0.append(x_data[i,0])
x1.append(x_data[i,1])
else:
y0.append(x_data[i,0])
y1.append(x_data[i,1])
return [x0,x1],[y0,y1]
def sigmoid(x):
return 1.0/(1+np.exp(-x))
def cost(x,y,ws):
left = np.multiply(y,np.log(sigmoid(x*ws)))
right = np.multiply(1-y,np.log(1-sigmoid(x*ws)))
return np.sum(left+right)/-(len(x))
def gradAscent(x,y,plot_=0):
print (getattr(gradAscent,'__name__').center(20,"-"))
if plot_ ==1:
x = preprocessing.scale(x)
x_=np.mat(x)
y_=np.mat(y)
lr=0.03
epochs=50000
cost_list=[]
m,n =np.shape(x_)
ws = np.mat(np.ones((n,1)))
for i in range(epochs):
h= sigmoid(x_*ws)
ws_ =x_.T*(h-y_)/m
ws = ws - lr*ws_
if (i+1) % 50 == 0:
cost_list.append(cost(x_,y_,ws))
return ws,cost_list
def cost_draw(cost_list):
print (len(cost_list))
x = np.linspace(0,10000,200)
plt.plot(x, cost_list, c='r')
plt.title('Train')
plt.xlabel('Epochs')
plt.ylabel('Cost')
plt.show()
def predict(x,ws,plot_):
if plot_ ==1:
x = preprocessing.scale(x)
x_ = np.mat(x)
ws = np.mat(ws)
return [1 if x>=0.5 else 0 for x in sigmoid(x*ws)]
def draw_plot(x0,x1):
#******************x0类别为0********************
#******************x1类别为1********************
print ("draw_plot.\n")
scatter_0= plt.scatter(x0[0],x0[1],c='b',marker='*')
scatter_1= plt.scatter(x1[0],x1[1],c='r',marker='+')
plt.legend(handles=[scatter_0,scatter_1],labels=["l1","l2"],loc="best")
#plt.show()
def main():
plot_=0
data = genfromtxt("LR-testSet2.txt",delimiter=",")
x_data = data[:,:-1]
y_data = data[:,-1,np.newaxis]
x_0,x_1 = get_vertix(x_data,y_data)
poly_reg = PolynomialFeatures(degree =3)
x_poly = poly_reg.fit_transform(x_data)
print ("run".center(10,"-"))
ws,cost_list=gradAscent(x_poly,y_data,plot_)
if plot_ == 0 :
x_min,x_max=x_data[:,0].min()-1,x_data[:,0].max()+1
y_min,y_max=x_data[:,1].min()-1,x_data[:,1].max()+1
xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))
z = sigmoid(poly_reg.fit_transform(np.c_[xx.ravel(),yy.ravel()]).dot(np.array(ws)))
for i in range(len(z)):
if z[i]>0.5:
z[i]=1
else:
z[i]=0
z = z.reshape(xx.shape)
cs = plt.contourf(xx,yy,z)
draw_plot(x_0,x_1)
plt.show()
predictions = predict(x_poly,ws,plot_)
print (classification_report(y_data,predictions))
main()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/425cdd1b561c037792685e54d308d34f.png)
调用skleran
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.datasets import make_gaussian_quantiles
from sklearn.preprocessing import PolynomialFeatures
def line_model(x_,y_):
logistic= linear_model.LogisticRegression()
logistic.fit(x_,y_)
x_min,x_max= x_[:,0].min()-1,x_[:,0].max()+1
y_min,y_max= x_[:,1].min()-1,x_[:,1].max()+1
xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))
z=logistic.predict(np.c_[xx.ravel(),yy.ravel()])
z= z.reshape(xx.shape)
cs = plt.contourf(xx,yy,z)
plt.scatter(x_[:,0],x_[:,1],c=y_)
plt.show()
print ('score:',logistic.score(x_,y_))
def no_line_model(x_,y_):
poly_reg = PolynomialFeatures(degree = 5)
x_poly = poly_reg.fit_transform(x_)
logistic= linear_model.LogisticRegression()
logistic.fit(x_poly,y_)
x_min,x_max= x_[:,0].min()-1,x_[:,0].max()+1
y_min,y_max= x_[:,1].min()-1,x_[:,1].max()+1
xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))
z=logistic.predict(poly_reg.fit_transform(np.c_[xx.ravel(),yy.ravel()]))
z= z.reshape(xx.shape)
cs = plt.contourf(xx,yy,z)
fig= plt.figure(1)
plt.scatter(x_[:,0],x_[:,1],c=y_)
plt.show()
print ('score:',logistic.score(x_poly,y_))
fig.savefig("tfig.png")
def main():
x_,y_ =make_gaussian_quantiles(n_samples=500,n_features=2,n_classes=2)
print("start\n")
line_model(x_,y_)
no_line_model(x_,y_)
main()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cb88bc02bb26a3fc17757ad022ef16c1.png)