python实现logistic分类算法代码
作者:Mr_Ten 时间:2021-12-24 14:19:15
最近在看吴恩达的机器学习课程,自己用python实现了其中的logistic算法,并用梯度下降获取最优值。
logistic分类是一个二分类问题,而我们的线性回归函数
的取值在负无穷到正无穷之间,对于分类问题而言,我们希望假设函数的取值在0~1之间,因此logistic函数的假设函数需要改造一下
由上面的公式可以看出,0 < h(x) < 1,这样,我们可以以1/2为分界线
cost function可以这样定义
其中,m是样本的数量,初始时θ可以随机给定一个初始值,算出一个初始的J(θ)值,再执行梯度下降算法迭代,直到达到最优值,我们知道,迭代的公式主要是每次减少一个偏导量
如果将J(θ)代入化简之后,我们发现可以得到和线性回归相同的迭代函数
按照这个迭代函数不断调整θ的值,直到两次J(θ)的值差值不超过某个极小的值之后,即认为已经达到最优解,这其实只是一个相对较优的解,并不是真正的最优解。 其中,α是学习速率,学习速率越大,就能越快达到最优解,但是学习速率过大可能会让惩罚函数最终无法收敛,整个过程python的实现如下
import math
ALPHA = 0.3
DIFF = 0.00001
def predict(theta, data):
results = []
for i in range(0, data.__len__()):
temp = 0
for j in range(1, theta.__len__()):
temp += theta[j] * data[i][j - 1]
temp = 1 / (1 + math.e ** (-1 * (temp + theta[0])))
results.append(temp)
return results
def training(training_data):
size = training_data.__len__()
dimension = training_data[0].__len__()
hxs = []
theta = []
for i in range(0, dimension):
theta.append(1)
initial = 0
for i in range(0, size):
hx = theta[0]
for j in range(1, dimension):
hx += theta[j] * training_data[i][j]
hx = 1 / (1 + math.e ** (-1 * hx))
hxs.append(hx)
initial += (-1 * (training_data[i][0] * math.log(hx) + (1 - training_data[i][0]) * math.log(1 - hx)))
initial /= size
iteration = initial
initial = 0
counts = 1
while abs(iteration - initial) > DIFF:
print("第", counts, "次迭代, diff=", abs(iteration - initial))
initial = iteration
gap = 0
for j in range(0, size):
gap += (hxs[j] - training_data[j][0])
theta[0] = theta[0] - ALPHA * gap / size
for i in range(1, dimension):
gap = 0
for j in range(0, size):
gap += (hxs[j] - training_data[j][0]) * training_data[j][i]
theta[i] = theta[i] - ALPHA * gap / size
for m in range(0, size):
hx = theta[0]
for j in range(1, dimension):
hx += theta[j] * training_data[i][j]
hx = 1 / (1 + math.e ** (-1 * hx))
hxs[i] = hx
iteration += -1 * (training_data[i][0] * math.log(hx) + (1 - training_data[i][0]) * math.log(1 - hx))
iteration /= size
counts += 1
print('training done,theta=', theta)
return theta
if __name__ == '__main__':
training_data = [[1, 1, 1, 1, 0, 0], [1, 1, 0, 1, 0, 0], [1, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 1]]
test_data = [[0, 1, 0, 0, 0], [0, 0, 0, 0, 1]]
theta = training(training_data)
res = predict(theta, test_data)
print(res)
运行结果如下
来源:https://blog.csdn.net/u012722531/article/details/79274528
标签:python,logistic,分类算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
对python csv模块配置分隔符和引用符详解
2023-04-14 13:52:20
php版银联支付接口开发简明教程
2023-11-24 00:27:41
Python提示[Errno 32]Broken pipe导致线程crash错误解决方法
2022-12-02 02:20:42
Selenium chrome配置代理Python版的方法
2022-02-24 14:26:08
Python requests.post方法中data与json参数区别详解
2022-02-06 09:06:20
如何让新页面在新窗口打开?
2009-04-12 19:41:00
Python查找最长不包含重复字符的子字符串算法示例
2021-05-25 23:05:02
![](https://img.aspxhome.com/file/2023/8/66398_0s.png)
纯CSS去除按钮以及链接点击时虚线
2009-05-21 10:40:00
tensorflow将图片保存为tfrecord和tfrecord的读取方式
2022-01-22 11:32:04
![](https://img.aspxhome.com/file/2023/4/77004_0s.jpg)
PHP中把stdClass Object转array的几个方法
2023-11-19 06:56:33
WEB页面工具语言XML应用分类之运用
2008-05-29 10:58:00
IE10增强对HTML5和CSS3的支持
2011-09-16 20:16:28
ASPJPEG组件简要攻略之水印、缩略图和描边代码
2008-12-17 12:08:00
最简便的备份MySQL数据库的方法
2008-12-25 13:16:00
Python简单读写Xls格式文档的方法示例
2021-11-02 13:27:30
![](https://img.aspxhome.com/file/2023/9/84059_0s.png)
怎样设计网站首页?(解答)
2007-11-04 18:56:00
Oracle Index 的三个问题
2010-07-26 12:53:00
使用python检查yaml配置文件是否符合要求
2021-06-23 05:27:53
python原类、类的创建过程与方法详解
2023-01-26 06:59:27
浅谈Golang内存逃逸
2023-08-27 07:51:55