机器学习基础知识笔记
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()

本文介绍了使用Python进行机器学习的基础操作,包括numpy中的reshape和resize数组操作、数组拼接、sigmoid激活函数的实现以及梯度下降算法的应用实例。
5万+

被折叠的 条评论
为什么被折叠?



