我有一个二维数组,想要按列获取所有唯一数字的出现次数。
这是一个例子:
import numpy as np
a = np.array([[2,2,3,3],
[2,3,3,3],
[3,3,4,4]])
Run Code Online (Sandbox Code Playgroud)
结果应该是
[[2,1,0,0],
[1,2,2,2],
[0,0,1,1]])
Run Code Online (Sandbox Code Playgroud)
例如,第一行是2每列中出现的数字,0 表示2不在第三和第四列中。第二行是数字的出现3,最后一行是数字的出现4。简而言之,我想获取每个排序的唯一值的每列计数。
我尝试过np.unique(a, return_counts=True, axis=0),但得到了错误的结果:
(array([[2, 2, 3, 3],
[2, 3, 3, 3],
[3, 3, 4, 4]]),
array([1, 1, 1]))
Run Code Online (Sandbox Code Playgroud) 我需要将一组数组的类型设置为np.ndarray每个数组的类型。由于我需要多次执行此操作,因此我正在尝试一个for循环,在执行循环时似乎可以正确地从 转换为list,np.ndarray但是一旦结束,每个数组仍然是类型list,下面的这个块帮助我意识到它正在发生,但我不知道为什么会发生
那么,为什么会发生这种情况呢?以及如何解决它?提前致谢
import numpy as np
pos = [1,1,1]
vel = [1,1,2]
# vel = np.array([1,1,2])
accel = [1,1,3]
print('\nredefining...')
for elem in [pos,vel,accel]:
# checks if the array is of the np.ndarray class
print(type(elem))
if not isinstance(elem, np.ndarray):
elem = np.array(elem)
print(type(elem))
print('---------------------------')
print('\nafter the redefinition:')
print(type(pos))
print(type(vel))
print(type(accel))
Run Code Online (Sandbox Code Playgroud)
输出:
redefining...
<class 'list'>
<class 'numpy.ndarray'>
---------------------------
<class 'list'>
<class 'numpy.ndarray'>
---------------------------
<class 'list'>
<class 'numpy.ndarray'>
---------------------------
after the …Run Code Online (Sandbox Code Playgroud) 我正在尝试使图像的背景透明。我已经把所有我想用黑色透明的部分都切掉了。但是python给了我一条关于只服用RBG的错误信息。我得到的错误消息是“无法将大小为 4 的序列复制到维度为 3 的数组轴” 那么如何让 python 识别 alpha 通道并允许我操作它?
import matplotlib.pyplot as plt
import os.path
import numpy as np
import PIL
def mask(row, column) :
for row in range(0, 383) :
for column in range(0, 86) :
img[row][column] = [0, 0, 0]
for row in range(230, 383) :
for column in range(0, 286) :
img[row][column] = [0, 0, 0]
for row in range(0, 50) :
for column in range(0, 286) :
img[row][column] = [0, 0, 0]
for row in …Run Code Online (Sandbox Code Playgroud) This numpy behavior seems a little weird.
>>> type(np.array([1, np.nan]).repeat(2)[2])
<class 'numpy.float64'>
Run Code Online (Sandbox Code Playgroud)
But when I make the first param a string
>>> type(np.array(["a", np.nan]).repeat(2)[2])
<class 'numpy.str_'>
Run Code Online (Sandbox Code Playgroud)
How do I fix it?
我想创建5x5 numpy数组.声明如下:
aa = np.array([[2.82842712, 2.23606798, 2.0, 2.23606798, 2.82842712],
[2.23606798, 1.41421356, 1.0, 1.41421356, 2.23606789],
[2.0, 1.0, 0.0, 1.0, 2.0],
[2.23606789, 1,41421356, 1.0, 1.41421356, 2.23606789],
[2.82842712, 2.23606789, 2.0, 2.23606798, 2.82842712]])
Run Code Online (Sandbox Code Playgroud)
但当我检查它的大小是5x1.
控制台阵列中的打印如下:
MATRICE aa:
[[2.82842712, 2.23606798, 2.0, 2.23606798, 2.82842712]
[2.23606798, 1.41421356, 1.0, 1.41421356, 2.23606789]
[2.0, 1.0, 0.0, 1.0, 2.0]
[2.23606789, 1, 41421356, 1.0, 1.41421356, 2.23606789]
[2.82842712, 2.23606789, 2.0, 2.23606798, 2.82842712]]
Shape: (5,)
Run Code Online (Sandbox Code Playgroud)
有谁看到我错了?
我有两个2D数组,例如:
A=array[[4,5,6],
[0,7,8],
[0,9,0]]
B = array[[11,12,13],
[14,15,16],
[17,18,19]]
Run Code Online (Sandbox Code Playgroud)
在元素值为0的数组A中,我想将数组B中的相同值替换为0,并将更改后的矩阵存储在新变量中,并保留旧的B矩阵。
提前致谢。