我在阅读这篇关于从头开始实现线性回归的代码:
# convert from data frames to numpy matrices
X = np.matrix(X.values)
y = np.matrix(y.values)
theta = np.matrix(np.array([0,0]))
当我看到这一行时:
当我遇到了这样的代码:
np.matrix(np.array([0,0]))
我在想为什么这个人不直接写
np.matrix([0,0])
。我在jupyter notebook中运行两者,得到相同的输出:
theta = np.matrix([0,0])
theta2 = np.matrix(np.array([0,0]))
print(theta,theta2,type(theta),type(theta2))
输出结果:[[0 0]] [[0 0]] <class 'numpy.matrix'> <class 'numpy.matrix'>
这两者之间有区别吗?np.array
的额外部分是否会增加theta的功能?如果我用后者替换前者,最终代码是否能正常运行?
np.matrix
已经有了一个“PendingDepricationWarning”警告。我认为演示作者只是使用了他习惯的编码风格,没有经过一群 Stack Overflow 读者的调试。 - hpaulj