逻辑回归_非线性

33 篇文章 0 订阅
24 篇文章 0 订阅

手写

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()

在这里插入图片描述

调用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()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值