机器学习基础(十)机器学习数学操作代码补充

机器学习基础知识笔记

1. 关于reshape 和 resize的操作

import numpy as np

# 补充基础知识 reshape resize
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(array)
array1 = array.reshape(2, 5)
print(array1)
array1[0][0] = 100
print(array)

array2 = array.resize(2, 5)
print(array)

# 多列一行
array3 = array.reshape(-1, 1)
print(array3)

# 一行多列
array4 = array.reshape(1, -1)
print(array4)

在这里插入图片描述

2. 数组的拼接

# 数组的拼接
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
array3 = np.concatenate((array1, array2))
print(array3)

在这里插入图片描述

3. 激活函数

import numpy as np
import matplotlib.pyplot as plt

# 定义sigmoid激活函数
def sigmoid(x):
    return 1 / (1 + np.exp(-x))
    pass

# 绘制函数的几何图形
xi = np.arange(-10, 10.1, 0.1)
yi = sigmoid(xi)

plt.plot(xi, yi)
plt.grid(linestyle="--")
plt.show()

在这里插入图片描述

4. 梯度下降函数

  • 梯度下降采用数值计算的方式求取凸函数极值。
#  初始代码
import numpy as np
import matplotlib.pyplot as plt
#  假设函数 f(x) = x'2 + 5x + 10, 导数2*x + 5,这一步求导的函数应该也有函数计算
#  使用梯度下降求取上述函数极值

#  待计算函数
def predict(x):
    return x**2 + 5*x + 10
    pass

#  梯度函数
def gradient(x) :
    return 2*x + 5
    pass

#  梯度下降算法求解极值
def train(x0, alpha = 0.01, tol = 0.000000001, times = 10000):
    t = 0
    while t < times:
        f0 = predict(x0)
        g_x0 = gradient(x0)
        x1 = x0 - alpha * g_x0
        f1 = predict(x1)
        if(np.absolute(f0 - f1) < tol):
            break
            pass
        x0 = x1
        pass
    return x0
    pass

x0 = train(2)
print(x0)

px = np.arange(-12.5, 7.6, 0.1)
py = predict(px)
plt.plot(px, py)
plt.show()

得到结果:
在这里插入图片描述
在这里插入图片描述

  • 采用点图的方式将梯度下降的过程可视化:
import numpy as np
import matplotlib.pyplot as plt
#  假设函数 f(x) = x'2 + 5x + 10, 导数2*x + 5,这一步求导的函数应该也有函数计算
#  使用梯度下降求取上述函数极值

#  待计算函数
def predict(x):
    return x**2 + 5*x + 10
    pass

#  梯度函数
def gradient(x) :
    return 2*x + 5
    pass

xList = []
yList = []

#  梯度下降算法求解极值
def train(x0, alpha = 0.1, tol = 0.000000001, times = 10000):
    t = 0
    while t < times:
        f0 = predict(x0)
        xList.append(x0)
        yList.append(f0)
        g_x0 = gradient(x0)
        x1 = x0 - alpha * g_x0
        f1 = predict(x1)
        if(np.absolute(f0 - f1) < tol):
            break
            pass
        x0 = x1
        pass
    return x0
    pass

x0 = train(2)
print(x0)

px = np.arange(-12.5, 7.6, 0.1)
py = predict(px)
plt.plot(px, py)
plt.scatter(xList, yList, c='red')
plt.show()

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值