台大机器学习基石(Machine Learning Foundations)(Quiz3--作业三)的Python实现(带详细注释)

该博客详细介绍了如何使用Python实现台大机器学习基石课程Quiz3中的第13-15题及第18-20题的线性回归和逻辑回归问题。内容包括生成数据集、1st和2nd阶线性回归模型的训练和评估,以及逻辑回归模型的梯度下降和随机梯度下降算法。
摘要由CSDN通过智能技术生成

Quiz 3  (q13-15, q18-20)

源码文件链接:https://github.com/Alex-YP-Jiang/Machine-Learning-Foundations-Taiwan-University-Quiz-1-4-Python-Codes

 

>>> # Q13-15  1st order and feature-transformed(2nd order) Linear Regression
>>> import random, math
>>> from pylab import*


>>> def training_set(N,noise,x_l,x_r):  # Generates noisy quadratic-separable binary class. data points.
    x_1 = []
    x_2 = []
    y = []
    for i in range(N):
        x1 = uniform(x_l,x_r)  # The bug with 'uniform([,])' induced interesting stuff: 1).random.choice() vs. numpy.random.choice(); 2).'sign(array)' gives an array of
        x2 = uniform(x_l,x_r)  # signs! 3).'a has to be 1-dimensional' for numpy's choice()!!!
        s = sign(x1**2 + x2**2 - 0.6)
        y_noise = choice([s,-s], p = [1-noise, noise])
        x_1.append(x1)
        x_2.append(x2)
        y.append(y_noise)
    return [x_1,x_2,y]

 

>>> def lin_reg(D):  # D is the output of the function above.
    x_1 = D[0]
    x_2 = D[1]
    y = array(D[2])
    x0 = 1
    N = len(y)
    input_matrix = []
    for i in range(N):
        x_n = [x0,x_1[i],x_2[i]]
        input_matrix.append(x_n)
    X = matrix(input_matrix)  # 'numpy.matrix([row1,row2...])'
    X_psu_inv = linalg.pinv(X)  # 'numpy.linalg.pinv(M)' for getting the pseudo inverse of M
    w_lin = matmul(X_psu_inv, y)  # 'numpy.matmul()' can have arguments as matrix,array or lists, it returns arrays if arguments are lists/arrays.
    a = array(w_lin)  # To reduce the output matrix to an array that contains only 1 array as its element.
    w_lin = a[0]
    return w_lin


>>> def E_in(w, D):
    x_1 = D[0]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值