我想从矩阵中选择不同数量的第一个元素.数字在数组中指定.结果是一维数组.例如:
a = np.arange(25).reshape([5, 5])
numbers = np.array([3, 2, 0, 1, 2])
Run Code Online (Sandbox Code Playgroud)
我想要这个结果:
[0, 1, 2, 5, 6, 15, 20, 21]
Run Code Online (Sandbox Code Playgroud)
没有for循环.
我有数组:
A = np.array([1, 6, 4, 4, 5, 6])
B = np.array([5, 40, 4, 6, 54,7]) #same size as A but every element of B is greater than corresponding element of A
C = np.array([6, 3])
Run Code Online (Sandbox Code Playgroud)
我想找到的所有行A
,并B
使得C>=A
和C<=B
。A and B
表格对的相同行,因此必须一起选择。
因此,输出数组将是:
For C[0] = 6
Aout = [6, 4, 5, 6]
Bout = [40, 6, 54, 7]
ForC[1] = 3
Aout = [1]
Bout = [5]
Run Code Online (Sandbox Code Playgroud)
因此,最终输出如下:
Aout = [1, 6, …
Run Code Online (Sandbox Code Playgroud) 我有一个 3-d Numpy 数组flow
,如下所示:
flow = np.random.uniform(low=-1.0, high=1.0, size=(720,1280,2))
# Suppose flow[0] are x-coordinates. flow[1] are y-coordinates.
Run Code Online (Sandbox Code Playgroud)
需要计算每个 x,y 点的角度。这是我如何实施它:
def calcAngle(a):
assert(len(a) == 2)
(x, y) = a
# angle_deg = 0
angle_deg = np.angle(x + y * 1j, deg=True)
return angle_deg
fangle = np.apply_along_axis(calcAngle, axis=2, arr=flow)
# The above statement takes 14.0389318466 to execute
Run Code Online (Sandbox Code Playgroud)
每个点的角度计算需要14.0389318466 seconds
在我的 Macbook Pro 上执行。
有没有一种方法可以加快速度,可能是通过使用一些矩阵运算,而不是一次处理一个像素。
我想在井字游戏中检查所有可能的获胜者条件,我该如何以功能性方式重写?
board = numpy.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
player = 1
if any([(board[0, :] == player).all(),
(board[:, 0] == player).all(),
(board[1, :] == player).all(),
(board[:, 1] == player).all(),
(board[2, :] == player).all(),
(board[:, 2] == player).all()]):
print('Win')
Run Code Online (Sandbox Code Playgroud) 我有一个二进制数组,我想根据它们重复的长度翻转值.举个例子
Ar = [0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1];
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想翻转仅重复2次或更少次数的1,从而产生以下结果.
Ar = [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1];
Run Code Online (Sandbox Code Playgroud)
我有一个代码,首先我需要对值进行排序,然后我需要总结前10个元素.我很想使用Numba软件包来加快运行时间,但它不起作用,Numba的代码比Numpy慢.
我的第一次测试,仅用于总和:
import numpy as np
import numba
np.random.seed(0)
def SumNumpy(x):
return np.sum(x[:10])
@numba.jit()
def SumNumpyNumba(x):
return np.sum(x[:10])
Run Code Online (Sandbox Code Playgroud)
我的测试:
x = np.random.rand(1000000000)
%timeit SumNumpy(x)
%timeit SumNumpyNumba(x)
Run Code Online (Sandbox Code Playgroud)
结果:
100000个循环,最佳3:每循环6.8μs
1000000个循环,最佳3:715 ns每个循环
这是好的,Numba做得很好.但是当我一起尝试np.sort和np.sum:
def sumSortNumpy(x):
y = np.sort(x)
return np.sum(y[:10])
@numba.jit()
def sumSortNumpyNumba(x):
y = np.sort(x)
return np.sum(y[:10])
Run Code Online (Sandbox Code Playgroud)
并测试:
x = np.random.rand(100000)
%timeit sumSortNumpy(x)
%timeit sumSortNumpyNumba(x)
Run Code Online (Sandbox Code Playgroud)
结果:
100个循环,最佳3:每循环14.6毫秒
10个循环,最佳3:20.6 ms每个循环
Numba/Numpy变得比Numpy慢.所以我的问题是否有什么可以改善功能"sumSortNumpyNumba"?
我很感激帮助.
谢谢.
给定两个 np.arrays;
a = np.array([1, 6, 5, 3, 8, 345, 34, 6, 2, 867])
b = np.array([867, 8, 34, 75])
Run Code Online (Sandbox Code Playgroud)
我想得到一个与 b 具有相同维度的 np.array,其中每个值是 b 中的值出现在 a 中的索引,或者 np.nan 如果 b 中的值不存在于 a 中。
结果应该是;
[9, 4, 6, nan]
Run Code Online (Sandbox Code Playgroud)
a 和 b 将始终具有相同的维度数,但维度的大小可能不同。
就像是;
(伪代码)
c = np.where(b in a)
Run Code Online (Sandbox Code Playgroud)
但适用于数组(“in”不适用)
我更喜欢“单行”或至少是完全在阵列级别的解决方案,并且不需要循环。
谢谢!
请有人帮我解决我的问题.我正在从事基于图像处理的项目,我坚持了一点.我经过一些处理得到了这个图像,为了进一步处理我需要裁剪或检测只鹿并删除其他部分的图像.
这是我的初始形象:
我的结果应该是这样的:
如果我在图像中只获得一个最大的斑点并将其保存为图像,那将会更好.
我编写了一个程序来创建一堆二进制数,如下所示:
out = [0,1,1,0,1,1,1,0,0,0,1,0];
Run Code Online (Sandbox Code Playgroud)
我想1
在上面一起检查九位数的存在out
,例如当我们在输出中有这个时:
out_2 = [0,0,0,0,1,1,1,1,1,1,1,1,1];
Run Code Online (Sandbox Code Playgroud)
要么
out_3 = [0,0,0,1,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0];
Run Code Online (Sandbox Code Playgroud)
condition
变量应设置为1
.我们不知道out
变量的起点的确切位置.这是随机的.我只想在上面的变量中找到重复的值(一次或多次).
PS.
我们正在寻找一个通用的答案来找到其他重复的数字(这里不仅仅是1,而且不仅仅是二进制数据.这只是一个例子)
我有一个前导零的数组,我想从第一个非零元素的数组.例如,我有一个数组
x=[0,0,0,0,0,3,2,0,0,4,5]
Run Code Online (Sandbox Code Playgroud)
我想获得:
x=[3,2,0,0,4,5]
Run Code Online (Sandbox Code Playgroud)