def f(x):
x=x/5.
return x
def g(x):
x/=5.
return x
x_var = np.arange(5,dtype=np.double)
f(x_var)
print x_var
g(x_var)
print x_var
Output:
[ 0. 1. 2. 3. 4.]
[ 0. 0.2 0.4 0.6 0.8]
Run Code Online (Sandbox Code Playgroud)
这种行为对我来说有点奇怪,我一直以为x/= 5.相当于x = x/5..但很明显,g(x)函数不会使用/ =操作创建新引用.有人可以为此提供解释吗?
我一直试图弄清楚我的docstring中这个表达式有什么问题.我sphinx.ext.mathjax在python sphinx v1.2b中使用扩展名.文档字符串:
.. math::
w_k^* = \min_{w_k} \ell_k(w_k) + \lambda\left(\alpha||w_k||_1
+ \frac{1}{2}(1-\alpha) ||w_k||^2\right)
Run Code Online (Sandbox Code Playgroud)
出现的是:

但它继续产生这个奇怪的警告,并没有渲染表达式:
WARNING: Block quote ends without a blank line; unexpected unindent.
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我删除\alpha,\left,\right,\frac符号,表达呈现精细无警告.不确定为什么\lambda会得到支持而不是\alpha.
X是焦虑矩阵,其中p远大于n.假设n = 1000且p = 500000.当我运行时:
X = np.random.randn(1000,500000)
S = X.dot(X.T)
Run Code Online (Sandbox Code Playgroud)
尽管结果大小为1000 x 1000,但执行此操作最终会占用大量内存.一旦操作完成,内存使用将恢复.有没有办法解决?
是否有将向量表示为 NumPy 中的一维或二维 ndarray 的标准做法?我正在从将向量表示为二维数组的 MATLAB 转移。
以下代码甚至不会在我的系统上完成:
import numpy as np
from scipy import sparse
p = 100
n = 50
X = np.random.randn(p,n)
L = sparse.eye(p,p, format='csc')
X.T.dot(L).dot(X)
Run Code Online (Sandbox Code Playgroud)
有没有解释为什么这个矩阵乘法挂起?