斯坦福大学机器学习作业题Problem Set #1: Supervised Learning Logistic regression

    

(a)


(b)自己写了个程序跑了下

# -*- coding: utf-8 -*-
import numpy as np
import math
import matplotlib.pyplot as plt
def readx(filename):       #读取文件
    fr=open(filename)
    arraylines = fr.readlines()
    row = len(arraylines)
    x = np.zeros((row, 3))
    i=0
    for line in arraylines:
        line=line.strip()
        line=line.split()
        x[i][0]=line[0]
        x[i][1]=line[1]
        x[i][2] = 1   #多加一列1,方便计算b
        i=i+1
    return x

def ready(filename):
    fr=open(filename)
    arraylines = fr.readlines()
    row = len(arraylines)
    y=np.zeros((row,1))
    i=0
    for line in arraylines:
        line=line.strip()
        line=line.split()
        y[i][0] = line[0]
        i = i + 1
    return y

def figure(x,y,theta):        #画图
    plt.title('Training data')
    plt.xlabel('x1')
    plt.ylabel('x2')
    m=[]
    n=[]
    for i in range(len(y)):
        if y[i]==1:
            m.append(i)
        else:
            n.append(i)
    p1 = plt.scatter(x[n,0], x[n, 1], marker='x', color='m', label='1', s=30)
    p2 = plt.scatter(x[m, 0], x[m, 1], marker='+', color='c', label='-1', s=50)
    y1=[]
    for i in x[:,0]:
        y1.append((-1 / theta[1]) * (theta[0] * i + theta[2]))
    p3 = plt.plot(x[:,0],y1)
    plt.legend(loc='upper right')
    plt.show()

def derivative(x,y,theta):       #计算一阶导数向量和二阶海涅矩阵
    sum1=[0,0,0]
    n = 3
    sum2=np.zeros((n,n))
    m = len(y)
    for i in range(m):
        z=0
        xx=np.zeros((n,n))
        for j in range(n):
            z=z+theta[j] * x[i][j]  #计算theta的转置乘x
        for k in range (n):
            for l in range(n):
                xx[k][l]=x[i][k]*x[i][l]
        h=1/(1+math.exp(-y[i]*z))
        sum1=sum1+(1-h)*(-y[i])*x[i]
        sum2=sum2+(1-h)*h*xx
    sum1.shape = (-1, 1)
    return sum1/m,sum2/m

def newton():                    #利用牛顿下降法求theta
    x=readx('x.txt')
    y=ready('y.txt')
    theta=[0,0,0]
    n = 3
    g, H = derivative(x, y, theta)
    while (np.linalg.norm(g,ord=2))>=0.0001:
        theta_inv=np.dot(np.linalg.inv(H),g)
        for i in range(n):
            theta[i]=theta[i]-theta_inv[i][0]
        g, H = derivative(x, y, theta)
    figure(x, y,theta)
    return theta

def predict(x):         #预测函数
    theta=newton()
    print theta
    z = theta[0] * x[0] + theta[1] * x[1] + theta[2]
    if z > 0:
        return 1
    else:
        return -1

x = [1, 2]
y = predict(x)
print y




又调用包跑了下程序

# -*- coding: utf-8 -*-
import numpy as np
import math
from sklearn.linear_model import LogisticRegression
def readx(filename):
    fr=open(filename)
    arraylines = fr.readlines()
    row = len(arraylines)
    x = np.zeros((row, 2))
    i=0
    for line in arraylines:
        line=line.strip()
        line=line.split()
        x[i][0]=line[0]
        x[i][1]=line[1]  #多加一列1,方便计算b
        i=i+1
        print i
    return x
def ready(filename):
    fr=open(filename)
    arraylines = fr.readlines()
    row = len(arraylines)
    y=[]
    i=0
    for line in arraylines:
        line=line.strip()
        y.append(line)
    return y
x=readx('x.txt')
y=ready('y.txt')
clf = LogisticRegression()
clf.fit(x, y)
s=np.array([[1,1]])
y_pred = clf.predict(s)
print y_pred
(c)画了个图




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值