我在下面构建了一个简单的线性回归器...
import numpy as np
x = np.array([[0.0], [0.33], [0.66], [1.0]])
y = np.array([[1.0, 2.0, 3.0, 4.0]]).T
w = np.random.random((1, 1))
for j in xrange(100000):
a2 = np.dot(x, w) + 1
w += x.T.dot((y - a2))
print(a2)
现在我在尝试开发它以包含多变量数据......
import numpy as np
x = np.array([[0.0], [0.33], [0.66], [1.0]])
x2 = np.array([[0.0], [0.33], [0.66], [1.0]])
y = np.array([[1.0, 2.0, 3.0, 4.0]]).T
w = np.random.random((1, 1))
for j in xrange(100000):
mx = np.dot(x, w) + np.dot(x2, w)
w += (np.sum(x, x2)).T.dot((y - a2))
print(mx)
似乎没有让我把x和x2数组加在一起。此外,该模型预先产生了无限的值。请给我一些指示?只是numpy和python请不要学习,因为学习机器学习的真正方法是从零开始。随意更改x和y数组中的数据。使用matplotlib绘制数据和回归线的加分点!如果结果不好那么当然没关系,因为它毕竟是线性回归。再次感谢
答案 0 :(得分:0)
我认为你需要的是 numpy concatenate 功能:
import numpy as np
x = np.array([[0.0], [0.33], [0.66], [1.0]])
x2 = np.array([[0.0], [0.33], [0.66], [1.0]])
y = np.array([[1.0, 2.0, 3.0, 4.0]]).T
w = np.random.random((1, 2))
x_all = np.concatenate((x,x2),axis = 1)
for j in xrange(100):
mx = np.dot(x_all, w.T)
w += (x_all.T.dot((y - mx))).T
print(mx)
这有效,但我不确定权重的更新方式是否正确。
可以在此处找到从头开始的线性回归的一些示例:
https://machinelearningmastery.com/implement-simple-linear-regression-scratch-python/
https://mubaris.com/2017-09-28/linear-regression-from-scratch