线性回归-吴恩达-课后练习(新手个人学习笔记)

example_1

1、人口数量预测收益,根据ex1data1(人口、收益)完成单变量线性回归
2、根据ex1data2(面积、数量、价格)完成多变量线性回归预测房屋价格

单变量线性回归

import numpy as np
import matplotlib.pyplot as plt

def load_txt():
    data = np.loadtxt("ex1data1.txt",delimiter = ',')
    x = data[:,0].reshape(-1,1)
    y = data[:,1].reshape(-1,1)
    return x,y
    
x,y = load_txt()
#print(x,y)
#fig,ax = plt.subplot(figsize(12,8))
#plt.scatter(x,y,label = 'Traning Data')

def cost_J (x,y,w,b):
    m,n = x.shape
    cost = [0]
    for i in range(m):
        fwb = np.dot(x[i],w)+b
        cost += (fwb - y[i])**2
    cost = cost/(2*m)
    return cost

def gradient(x,y,w,b):
    m,n = x.shape
    dj_dw = np.zeros((n))
    dj_db = 0.
    for i in range (m):
        fwb = np.dot(x[i],w) + b
        err = fwb - y[i]
        for j in range(n):
            dj_dw[j] = dj_dw[j] + err*x[i,j]
        dj_db = dj_db + err
    dj_dw = dj_dw / m
    dj_db = dj_db / m
    return dj_dw,dj_db     

def gradient_descent(x,y,w_in,b_in,alpha,iters):
    w = w_in
    b = b_in
    cost = np.zeros(iters).reshape(-1,1)
    for i in range (iters):
        dj_dw,dj_db = gradient(x, y, w, b) 
        w = w - alpha*dj_dw
        b = b - alpha*dj_db
        cost[i] = cost_J(x,y,w,b)
    return w,b,cost

x,y=load_txt()  # 读取数据
w = np.array([[0.]])    
b = 0.		  # 初始设置w和b两个参数为零
alpha = 0.01  # 学习率为alpha
iters = 15000 # 迭代iters次
#print(w)
#print(x.shape)
#cost = cost_J(x,y,w,b)
#print(cost)
w_out,b_out,cost_out = gradient_descent(x,y,w,b,alpha,iters)
print (f"w is {w_out}\n  \nb is {b_out} \n\ncost is{cost_out}")

print(cost_out.shape)

fig,ax = plt.subplots(1,1)
x_iters = np.arange(iters).reshape(-1,1)
print(x_iters.shape)
ax.plot(x_iters,cost_out)

x_t = np.linspace(x.min(),x.max(),10)# 以人口最小值为起点,最大值为终点,创建元素个数为100的等差数列
f_t = b_out + (w_out * x_t) # 一次函数
#ax.scatter(x,y)
plt.figure(figsize =(8,7))
plt.plot(x_t,f_t.reshape(-1,1))
plt.scatter(x,y,marker ='o',c ='r')
plt.show()

以下将scikit-learn的线性回归算法应用单变量线性回归
可以学习一下pandas库
import pandas as pd
data = pd.read_csv( ‘ex1data1.txt’, header=None, names=[‘Population’, ‘Profit’])
print(data.head())
python矩阵中matrix()和array()函数区别:
辨析numpy中flatten,ravel以及A1的区别-----展平

import numpy as np
import matplotlib.pyplot as plt
import copy
from sklearn import linear_model

def load_txt():
    data = np.loadtxt("ex1data1.txt",delimiter = ',')
    x = data[:,0].reshape(-1,1)  # x取data的第一列,并将其设置为行数自动设置,列数固定为一列
    y = data[:,1].reshape(-1,1)
    return x,y

x1,y=load_txt()
model = linear_model.LinearRegression()
X = copy.deepcopy(x1)
model.fit(X, y)
f = model.predict(X).ravel()  
'''
# .ravel、.flatten、.A1   
# flatten(),以及ravel()对于array直接展平为一维数组,对于matrix展平的同时保留原有维数和类型
# A1命令只针对matrix类型,直接展平为一维数组,而不是一维矩阵
'''
#print(x)
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(X, f, 'r', label='Prediction')
ax.scatter(x1, y,c='b',  label='Traning Data')
ax.legend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs. Population Size')
plt.show()

在这里插入图片描述

多变量线性回归

#导入函数库
import numpy as np
import matplotlib.pyplot as plt
#编写相关函数
def loadtxt():
    data = np.loadtxt("ex1data2.txt",delimiter = ',')
    x = data[:,:2]
    y = data[:,2]
    return x,y

def cost_function(x,y,w,b):
    m = x.shape[0]
    cost = 0.00
    w = w.reshape(-1,1)
    for i in range (m):
        fwb = np.dot(x[i],w)+b
        cost = cost + (fwb - y[i])**2
    cost = cost / (2*m)
    return cost

def gradient_function(x,y,w,b):
    m,n = x.shape
    dj_dw = np.zeros(n)
    dj_db = 0.
    for i in range (m):
        err = (np.dot(x[i],w)+b)-y[i]
        for j in range (n):
            dj_dw[j] = dj_dw[j] + err * x[i,j]
        dj_db = dj_db + err
    dj_dw = dj_dw / m
    dj_db = dj_db / m
    return dj_dw,dj_db

def gradient_descent_function(x,y,w,b,alpha,iters):
    cost = np.zeros(iters)
    for i in range(iters):
        dj_dw,dj_db = gradient_function(x,y,w,b)
        w = w - alpha * dj_dw
        b = b - alpha * dj_db
        cost[i] = cost_function(x,y,w,b)
    return w,b,cost

#特征缩放
def feature_scaling_function(x,y):
    mu_x = np.mean(x,axis = 0)
    std_x = np.std(x,axis = 0)
    x_norm = (x-mu_x) / std_x
    #print("mu_x:",mu_x)
    #print("std_x:",std_x)

    mu_y = np.mean(y,axis = 0)
    std_y = np.std(y,axis = 0)
    y_norm = (y-mu_y) / std_y
    #print("mu_y:",mu_y)
    #print("std_y:",std_y)
    return x_norm,y_norm
#主函数
x,y = loadtxt()
#print (f"x is \n{x}\n\ny is \n{y}")
x1,y1 = feature_scaling_function(x,y)
y1 = y1.reshape(-1,1)
#print (f"x is \n{x}\n\ny is \n{y}")
w = np.zeros(2)
b = 0.

#cost = cost_function(x,y,w,b)    
#print (cost)    
alpha = 0.01
iters = 1500
w_out , b_out ,cost_out = gradient_descent_function(x1,y1,w,b,alpha,iters)
print (w_out,b_out,cost_out)
#cost = cost_function(x,y,w_out,b_out)    
#print (cost) 
#画出iters-cost的图形
fig, ax = plt.subplots(figsize=(12, 8))
ax.plot(np.arange(iters), cost_out, 'r')  
ax.set_xlabel('iters')  # 设定x轴名称
ax.set_ylabel('cost')  # 设定y轴名称 
plt.show() 

example_2

gradient_descent函数可以参照此链接运用矩阵 进行优化算法

逻辑回归

建立一个分类的模型,根据两门成绩预测录取或者不录取

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv("ex2data1.txt",names = ["grade_1","grade_2","result"],skiprows = 1)
#也可用下面这种方法,但我不知道这两种有什么区别
# data = np.load("ex2data1.txt",delimiter = ',')
# grade1 = data[:,0]
# grade2 = data[:,1]
# result = data[:,2]
data.insert(0, 'ones', 1)
x = data.iloc[:,0:3]
y = data.iloc[:,3:4]
#转矩阵
x = np.matrix(x)
y = np.matrix(y)
theta = np.matrix(np.zeros((3,1)))  #b,w1,w2
    
R1 = data["result"].isin([1])  #取result为1的行
R0 = data["result"].isin([0])
pos = data[R1] 
neg = data[R0]
#print(x)
#print(y) 
#画数据的散点图
fig,ax = plt.subplots(1,1)
ax.scatter(pos["grade_1"],pos["grade_2"],marker = "o",c = "r",label = "pos_result")
ax.scatter(neg["grade_1"],neg["grade_2"],marker = "x",c = "b",label = "neg_result")
ax.legend (loc= 1)
# 函数定义
def sigmoid (z):
    
    return 1/(1+np.exp(-z))

def cost_function(x,y,theta):
    m = x.shape[0]
    cost1 = -(np.log(sigmoid(x @ theta) ).T @ y)
    cost2 = -(np.log(1-(sigmoid(x @ theta))).T @ (1-y))
    return (cost1+cost2)/m

def gradient_function(x,y,theta,alpha,iters):
    m,n = x.shape
    cost = np.zeros(iters)
    for i in range (iters):
        theta = theta - alpha/m * x.T @ (sigmoid(x@theta)-y)
        cost[i] = cost_function(x,y,theta)
    return theta,cost

边界函数直接用了该作者的

#边界函数
def boudary_line(theta):
    coef1 = - theta[0, 0] / theta[2, 0]  # 第一个系数
    coef2 = - theta[1, 0] / theta[2, 0]  # 第二个系数
    x = np.arange(0, 130, 0.01)
    f = coef1 + coef2 * x
    positive = data[data['result'].isin([1])]  # 找出所有录取结果为1的所有行
    negative = data[data['result'].isin([0])]  # 找出所有录取结果为0的所有行
    # 下面是散点图
    fig, ax = plt.subplots(figsize=(12, 8))
    ax.scatter(x=positive['grade_1'], y=positive['grade_2'], s=50, color='purple', marker='o', label='committed')
    ax.scatter(x=negative['grade_1'], y=negative['grade_2'], s=50, color='orange', marker='x', label='not_committed')
    plt.legend()  # 图例的位置
    ax.set_xlabel('grade_1')  # 设置x轴的标题
    ax.set_ylabel('grade_2')  # 设置y轴的标题
    plt.plot(x, f, 0.01) 
    plt.show()
    pass
#画出边界函数图像
iters = 20000
alpha = 0.001
theta1, cost_list = gradient_function(x, y, theta, alpha,iters)
iter_t = np.arange(iters).reshape(1,-1)
cost = cost_list.reshape(1,-1)
#print(iter_t)
#print(cost)
#这里想画出迭代次数和损失函数的关系,但用plot怎么也画不出来,用散点图表示出来了
fig, ax = plt.subplots(figsize=(12, 8))
ax.scatter(iter_t,cost, color='orange', label='not_committed')
ax.set_xlabel('iters')  # 设置x轴的标题
ax.set_ylabel('cost')  # 设置y轴的标题
plt.show()
#边界函数
boudary_line(theta1)

正则逻辑回归

根据ex2data2,判断预测接受or拒绝芯片
特征映射

为了能很好的拟合,所以我们要进行特征映射,从而得到非线性的决策边界。特征映射将低维特征向量(本例中为二维)转化为高维特征向量(本例中为28维),因为特征向量的个数变多,所以需要用正则化,否则容易出现过拟合。映射的关系图如下图所示
在这里插入图片描述

代价函数正则化(防止过拟合)
在这里插入图片描述


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv("ex2data2.txt",names = ["test1","test2","result"])
#print(data.head())


data_pos = data[data["result"].isin([1])]
data_neg = data[data["result"].isin({0})]
#print(pos)
#print(neg)
fig,ax = plt.subplots(1,1)
ax.scatter (data_pos["test1"],data_pos["test2"],marker = ".",c = "g",label = "yes")
ax.scatter (data_neg["test1"],data_neg["test2"],marker = "*",c = "r",label = "no")
ax.legend(loc = 1)
plt.show()

#导入数据
data.insert(0,"ones",1)
x = np.matrix(data.iloc[:,0:3])
y = np.matrix(data.iloc[:,3:4])
print(x.shape)
print(y.shape)
theta = np.matrix(np.zeros([3,1]))
print(theta)

def sigmoid(z):
    return 1/(1+np.exp(-z))

def cost_function(x,y,theta,lameda):
    m = len(x)
    cost1 = -(np.log(sigmoid(x @ theta)).T @ y)
    cost2 = -((1-(np.log(sigmoid(x @ theta)))).T @ (1-y))
    cost = cost1+cost2
    polish = (np.sum(np.power(theta,2)))*(lameda/(2*len(x)))
    return cost/m + polish

def gradient_function(x,y,theta,alpha,iters,lameda):
    cost = []
    for i in range (iters):
        theta = theta - (alpha/len(x))*(x.T @ ((sigmoid(x@theta) - y)))
        cost[i] = cost_function(x,y,theta,lameda)
    return theta,cost

def predic_function(x,y,theta):
    t = 0
    R = 0
    pre_range = sigmoid (x @ theta)
    pre_list = []
    for i in  (pre_range):
        if i < 0.5:
            pre_list.append(0)
        else:
            pre_list.append(1)
    while True:
        if pre_list[t] == y[t]:
            R += 1
        if t == 118:
            break
    return R/len(x)

print("预测正确率为:",predic_function(x,y,theta))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值