我有一个包含大约 4900 万个项目 (7000*7000) 的大型数组,在其中我需要找到最大的 N 个项目及其索引,忽略所有 NaN。我无法事先删除这些 NaN,因为我需要第一个数组中最大 N 个项目的索引值,以便从另一个数组中提取数据,该另一个数组与第一个数组相比,在不同的索引中具有 NaN。我试过
np.argpartition(first_array, -N)[-N:]
这对于没有 NaN 的数组非常有效,但如果有 NaN,则 nan 将作为最大的项,因为它在 python 中被视为无穷大。
x = np.array([np.nan, 2, -1, 2, -4, -8, -9, 6, -3]).reshape(3, 3)
y = np.argpartition(x.ravel() , -3)[-3:]
z = x.ravel()[y]
# this is the result I am getting === [2, 6, nan]
# but I need this ==== [2, 2, 6]
Run Code Online (Sandbox Code Playgroud) 我所拥有的:
我正在创建一个数据类,并说明其元素的类型:
class Task():
n_items: int
max_weight: int
max_size: int
items: numpy.array(Item) # incorrect way of doing it
Run Code Online (Sandbox Code Playgroud)
我想做的事情是
我想声明,该items将是“Item”类的对象的 numpy 数组
我刚刚开始使用 numpy..
得到下面的 np 表,想计算每个单元格占列总数的百分比。
data = np.array([[7,16,17], [12,11,3]])
headers = ["Grundskola", "Gymn", "Akademisk"]
# tabulate data
table = tabulate(data, headers, tablefmt="github")
# output
print(table)
| Grundskola | Gymn | Akademisk |
|--------------|--------|-------------|
| 7 | 16 | 17 |
| 12 | 11 | 3 |
Run Code Online (Sandbox Code Playgroud)
到:
| Grundskola | Gymn | Akademisk |
|--------------|--------|-------------|
| 39%| 59% | 85% |
| 61%| 41% | 15% |
Run Code Online (Sandbox Code Playgroud)
我知道 np.sum(data2, axis=0/1) 会给我总数,但我如何使用它来计算数组。
数组的大小可以不同...
我有一个numpy.ndarray形状(1,28,28),值是 [0,1] 范围内的浮点。我的最终目标是将数组保存为 png 图像。
即使在转置数组并将其与 255 相乘以获得 uint 值之后,它仍然会抛出错误*** TypeError: Cannot handle this data type: (1, 1, 1), |u1。
我正在使用以下代码:
im = Image.fromarray((img.transpose(1,2,0) * 255).astype(np.uint8))
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
PS我已经在这里研究过这个建议。
我正在尝试从原始形状重塑数组,以使每一行的元素沿对角线下降:
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)
这两者之间甚至有什么区别,为什么在这种特定情况下它们的行为如此不同? …
我正在尝试使用 setattr 将属性添加到 Numpy ndarray,但出现错误:
import numpy as np
x = np.array([1, 2, 4])
setattr(x, 'new_attr', 1)
Run Code Online (Sandbox Code Playgroud)
AttributeError:numpy.ndarray 对象没有属性 new_attr
如何向 Numpy ndarray 添加新属性?
回溯错误
Traceback (most recent call last):
File "C:\Users\trial2\trial.py", line 55, in <module>
image_stack(image)
File "C:\Users\trial2\trial.py", line 41, in image_stack
transposed_axes = np.transpose(img, axes=concat)
File "<__array_function__ internals>", line 5, in transpose
File "C:\Users\trial2\venv\lib\site-packages\numpy\core\fromnumeric.py", line 660, in transpose
return _wrapfunc(a, 'transpose', axes)
File "C:\Users\trial2\venv\lib\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc
return bound(*args, **kwds)
ValueError: axes don't match array
Run Code Online (Sandbox Code Playgroud)
回溯错误也存在,如果这有助于解决问题,我需要为已作为参数输入到函数中的图像计算新的转置轴。该函数获取图像的形状,然后计算图像的转置轴。问题发生在将图像数组转置到新轴时。我无法弄清楚为什么它不起作用的问题。我是否需要将 concat 变量转换为元组才能正常工作,还是其他原因导致了问题
def image_stack(image):
img_shape = cv2.imread(image).shape
img = np.asarray(img_shape)
print(type(img))
n = len(img)
val_1 = list(range(1, n - 1, 2))
val_2 = list(range(0, n …Run Code Online (Sandbox Code Playgroud) ndarray使用 pydantic 时如何初始化?
这段代码抛出一个ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
from pydantic.dataclasses import dataclass
import numpy as np
@dataclass
class TestNumpyArray:
numpyArray: np.ndarray = np.zeros(10)
testNumpyArray = TestNumpyArray()
Run Code Online (Sandbox Code Playgroud) 对于多维数组,我想沿某些维度求和,并由其他维度索引定义总和范围。这是一个例子:
>>> import numpy as np
>>> x = np.arange(2*3*4).reshape((2,3,4))
>>> x
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> wanted = [[sum(x[i,j,i:j]) for j in range(x.shape[1])] for i in range(x.shape[0])]
>>> wanted
[[0, 4, 17], [0, 0, 21]]
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法来做到这一点而无需 for 循环或列表理解?我的数组相当大。
numpy-ndarray ×10
numpy ×8
python ×7
python-3.x ×4
arrays ×1
diagonal ×1
image ×1
opencv ×1
percentage ×1
pydantic ×1
sorting ×1
transpose ×1