我有以下代码来估计新的 numpy 数组是否适合内存:
import numpy as np
from subprocess import Popen, PIPE
def getTotalMemory():
output, _= Popen(r'wmic ComputerSystem get TotalPhysicalMemory', stdout=PIPE).communicate()
val = ''.join([c for c in str(output) if c.isdigit()])
return int(val)
def getFreeMemory():
output, _= Popen(r'wmic OS get FreePhysicalMemory', stdout=PIPE).communicate()
val = ''.join([c for c in str(output) if c.isdigit()])
# FreePhysicalMemory returns kilobytes, hence the multiplication with 1024
return 1024 * int(val)
def estimateNumpySize(dim, square = False, nptype = np.float64):
# Make sure that the actual nptype is an …Run Code Online (Sandbox Code Playgroud) 数组的哪一维称为数组的最内维?我正在浏览一些 tensorflow 文档“ctc loss”,其中描述了一种称为数组最内维的行话。在我看来,答案可能有三种可能。1. 如果从右边看,最左边。2. 如果我们从左边看,最右边。3. 中间,如果我们从两边看。
有人可以解释一下最内层维度在这里的含义吗?. 感谢您解决我的疑问。
我正在寻找有效的替代方法来计算二维向量之间的余弦角。您对这个问题的见解将大有帮助。
vectors是存储向量的二维数组。所述的形状vectors阵列是(N, 2)其中N是矢量的数目。vectors[:, 0]有 x 分量和vectors[:, 1]y 分量。
我必须找到vectors. 例如,如果在 中有三个向量 A、B、C vectors,我需要找到A and B、B and C、 和之间的角度A and C。
我已经实施了它并想知道替代方法。
vectors = np.array([[1, 3], [2, 4], [3, 5]])
vec_x = vectors[:, 0]
vec_y = vectors[:, 1]
a1 = np.ones([vec_x.shape[0], vec_x.shape[0]]) * vec_x
a2 = np.ones([vec_x.shape[0], vec_x.shape[0]]) * vec_y
a1b1 = a1 * a1.T
a2b2 = a2 * …Run Code Online (Sandbox Code Playgroud) 有一个 C++ 函数返回浮点数向量。如何在不复制的情况下将此向量转换为 NumPy 数组?现在我这样做:
cdef np.ndarray arr = np.ascontiguousarray(cpp_vector, dtype=np.float)
return arr
Run Code Online (Sandbox Code Playgroud)
但这在大向量上工作非常慢(假设发生复制)。
假设我有一个二维布尔数组。我想获得切片/或类似切片的列表,其中每个切片表示包含 True 值的数组的最小(大小)子区域,而其边界包含所有 False。
我可以循环每行和每列并在满足此类条件时存储索引,但我想知道您是否知道另一种方法或库可以有效地执行此操作?您可以假设原始布尔数组的边界始终为 False/0。
示例 1
示例 2
编辑 !添加了具有正确解决方案的新示例。对困惑感到抱歉。
def custom_asymmetric_train(y_true, y_pred):
residual = (y_true - y_pred).astype("float")
grad = np.where(residual>0, -2*10.0*residual, -2*residual)
hess = np.where(residual>0, 2*10.0, 2.0)
return grad, hess
Run Code Online (Sandbox Code Playgroud)
我想写下这样的声明:
case when residual>=0 and residual<=0.5 then -2*1.2*residual
when residual>=0.5 and residual<=0.7 then -2*1.*residual
when residual>0.7 then -2*2*residual end )
Run Code Online (Sandbox Code Playgroud)
但是np.where不能写 &(and) 逻辑。当逻辑在 python 中时,我该如何编写这种情况np.where。
谢谢
我想将数组拆分为带有掩码和索引的数组,
如下所示
a = array([ 0, 1, 2, 3, 4, 5]))
b = [0,2,3]
Run Code Online (Sandbox Code Playgroud)
进入
c = array([[0, 2, 3], [1, 3, 4], [2, 4, 5]])
Run Code Online (Sandbox Code Playgroud)
我可以在没有循环的情况下执行此操作吗?
编辑:
更多例子...
说,我们有一个a形状的数组,[10, 10, 10]
其中a[x, y, :] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
现在给了面具 b = [0, 3, 7]
我希望输出是一个c具有形状的数组,[10, 10, 3, 3]
其中c[x, y, :, :] = [[0, 3, 7], [1, 4, 8], [2, 5, 9]]
我真的希望不要错过一些之前已经澄清过的东西,但我在这里找不到任何东西。
任务看起来很简单,但我失败了。我想在 for 循环中连续将一个 numpy 数组附加到另一个数组:
step_n = 10
steps = np.empty([step_n,1])
for n in range(step_n):
step = np.random.choice([-1, 0, 1], size=(1,2))
#steps.append(step) -> if would be lists, I would do it like that
a = np.append(steps,step)
#something will be checked after each n
print(a)
Run Code Online (Sandbox Code Playgroud)
输出应为 ofc 类型<class 'numpy.ndarray'>,如下所示:
[[-1. 0.]
[ 0. 0.]
[-1. -1.]
[ 1. -1.]
[ 1. 1.]
[ 0. -1.]
[-1. 1.]
[-1. 0.]
[ 0. -1.]
[ 1. 1.]]
Run Code Online (Sandbox Code Playgroud)
但是,由于某些(最可能是显而易见的)原因,代码失败了。有人可以给我一个提示吗?
我正在尝试从原始形状重塑数组,以使每一行的元素沿对角线下降:
np.random.seed(0)
my_array = np.random.randint(1, 50, size=(5, 3))
Run Code Online (Sandbox Code Playgroud)
array([[45, 48, 1],
[ 4, 4, 40],
[10, 20, 22],
[37, 24, 7],
[25, 25, 13]])
Run Code Online (Sandbox Code Playgroud)
我希望结果如下所示:
my_array_2 = np.array([[45, 0, 0],
[ 4, 48, 0],
[10, 4, 1],
[37, 20, 40],
[25, 24, 22],
[ 0, 25, 7],
[ 0, 0, 13]])
Run Code Online (Sandbox Code Playgroud)
这是我能得到的最接近的解决方案:
my_diag = []
for i in range(len(my_array)):
my_diag_ = np.diag(my_array[i], k=0)
my_diag.append(my_diag_)
my_array1 = np.vstack(my_diag)
Run Code Online (Sandbox Code Playgroud)
array([[45, 0, 0],
[ 0, 48, 0],
[ 0, 0, 1],
[ 4, …Run Code Online (Sandbox Code Playgroud) 我正在阅读关于不可变 numpy 数组的这个问题,并且在对其中一个答案的评论中,有人表明给定的技巧在y = x[:]使用而不是y = x.
>>> import numpy as np
>>> x = np.array([1])
>>> y = x
>>> x.flags.writeable = False
>>> y[0] = 5
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
y[0] = 5
ValueError: assignment destination is read-only
>>> del x, y
>>> x = np.array([1])
>>> y = x[:]
>>> x.flags.writeable = False
>>> y[0] = 5
>>> x
array([5])
Run Code Online (Sandbox Code Playgroud)
(Python 3.7.2,numpy 1.16.2)
这两者之间甚至有什么区别,为什么在这种特定情况下它们的行为如此不同? …
numpy-ndarray ×10
python ×9
numpy ×8
python-3.x ×4
c++ ×1
cython ×1
diagonal ×1
python-2.7 ×1
tensorflow ×1
vector ×1