Coursera吴恩达机器学习ML-week3-ex2逻辑回归(未标准化)-python实现

import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import classification_report
from sklearn import preprocessing
scale = False   #是否进行标准化
data = np.genfromtxt("ex2data1.txt",delimiter = ",")
x = data[:,0:2]
y = data[:,2]

def plot():
    x0 = []
    y0 = []
    x1 = []
    y1 = []
    for i in range(len(x)):
        if(y[i]==0):
            x0.append(x[i,0])
            y0.append(x[i,1])
        else:
            x1.append(x[i,0])
            y1.append(x[i,1])
    scatter0 = plt.scatter(x0,y0,c='b',marker = 'o')
    scatter1 = plt.scatter(x1,y1,c='r',marker = 'x')
    plt.legend(handles = [scatter0,scatter1],labels = ['label0','label1'],loc = 'best')
    
plot()
plt.show()
y = y.reshape(-1,1)
X = np.concatenate((np.ones((100,1)),x),axis =1)
theta = np.zeros(3).reshape(3,1)
def sigmoid(x):
    return 1.0/(1+np.exp(-x))
def cost(xMat,yMat,theta):
    left = np.multiply(yMat,np.log(sigmoid(xMat*theta)))
    right = np.multiply(1-yMat,np.log(1-sigmoid(xMat*theta)))
    return np.sum(left+right)/-(len(xMat))

#print(cost(np.matrix(X),np.matrix(y),theta))
def gradscent(xArr,yArr,theta):
    if scale==True:
        xArr = preprocessing.scale(xArr)
    yMat = np.mat(yArr)
    xMat  = np.mat(xArr)
    lr = 0.004
    epochs = 200000
    costlist = []
    m,n = np.shape(xMat)
    theta = np.mat(theta)
    for i in range(epochs+1):
        h = sigmoid(xMat*theta)
        theta_grad = xMat.T*(h-yMat)/m
        theta = theta-lr*theta_grad
        costlist.append(cost(xMat,yMat,theta))
    return theta,costlist
theta,costlist = gradscent(X,y,theta)

print(theta)

if scale == False:
    plot()
    x_test = [[30],[100]]
    y_test = (-theta[0] - x_test*theta[1])/theta[2]
    plt.plot(x_test,y_test,'k')
def predict(x, theta):
    if scale == True:
        x = preprocessing.scale(x)
    xMat = np.mat(x)
    theta = np.mat(theta)
    return [1 if x >= 0.5 else 0 for x in sigmoid(xMat*theta)]

y_pre = np.array(predict(X, theta))
y_pre=y_pre.reshape(len(y_pre),1)
print(np.mean(y_pre==y))


如果有疑问,可以加群讨论:484266833

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值