python - 线性回归 python
问题描述
我想在没有 scikit-learn 的情况下在 python 中实现线性回归,但我不知道为什么,变量“错误”总是增加而且不应该,我认为问题可能出在公式中,但我不知道在哪里。
算法好吗?
def linear_regression(X, y, learning_rate=0.01, max_num_iterations=500, opt=True):
# Your code goes here
# Your code goes here
w = np.zeros(len(X[0]))
error=0
gradient=X
variable =X
print(f"X:{X}")
for a in range(max_num_iterations):
o = np.sum(X*w)
error = y-o
print(f"sum1:{np.sum(X*w)}")
print(f"y:{y}")
print(f"error: {error}")
print (f"gradient1: {gradient}")
print(f"w: {w}")
for i in range(800):
gradient[i][0]=-X[i][0]*error[i]
gradient[i][1]=-X[i][1]*error[i]
gradient=-learning_rate*gradient
w=w+gradient
return w
解决方案
推荐阅读
- c# - 我无法使用 Visual Studio 2019 在 C# 中转换为双精度
- apache - 多线程如何影响 http keep-alive 连接?
- javascript - 如何使用 JS 在不同的 HTML 页面之间导航
- javascript - Discord.js 嵌入图像不起作用。(无法将“{'url': 'https://cdn.nekos.life/boobs/boobs105.gif'}”解释为字符串。)
- python-3.x - 如何完全删除 wxPython 中的按钮?
- html - 伪类 'nth-child()' 不适用于 ':hover"
- nim-lang - 如何通过索引从 Nim 中的 OrderedTable 获取元素?
- r - 从 R 中的现有数据框构建新数据框
- flutter - 如何在 Flutter 中大写文本
- node.js - 使用sails 1、nginx和node的反向代理非常依赖版本,如何修复?