因此,我想在具有给定大小的python中添加交替的Cos和Tan函数。例如,我要为每一行(Tan(a)、Cos(a)、Tan(a)、Cos(a)、Tan(a))添加一个大小为numpy的数组(10-5)。有人能给我一个提示,说明从哪里开始吗?
我尝试在计数器小于5的条件下设置一个使用while循环的计数器,但这看起来不起作用。会很感激你的建议。
发布于 2020-11-08 08:37:14
像这样吗?
arr[:, ::2] = np.tan(arr[:, ::2])
arr[:, 1::2] = np.cos(arr[:, 1::2])
我很大胆,假设您不太熟悉numpy中的切片表示法和索引,所以可以考虑查看docs:Numpy索引。
发布于 2020-11-08 03:33:26
解决方案
既然您要求交替,我想给出一个更通用的答案,包括三种类型的替换:
row
column
你要的这个row
和column
(两者兼而有之):像棋盘一样我将在随后的示例中使用这些虚拟数据:A、B、C
# dummy data
import numpy as np
angles = np.random.rand(5,4) * np.pi
A.预期产出:交替行
[[tan, tan, tan, tan],
[cos, cos, cos, cos],
[tan, tan, tan, tan],
[cos, cos, cos, cos],
[tan, tan, tan, tan]]
代码:
# import numpy as np
a = np.zeros(angles.shape)
a[0::2, :] = np.tan(angles[0::2, :])
a[1::2, :] = np.cos(angles[1::2, :])
B.预期产出:交替栏
[[tan, cos, tan, cos],
[tan, cos, tan, cos],
[tan, cos, tan, cos],
[tan, cos, tan, cos],
[tan, cos, tan, cos]]
代码:
# import numpy as np
a = np.zeros(angles.shape)
a[:, 0::2] = np.tan(angles[:, 0::2])
a[:, 1::2] = np.cos(angles[:, 1::2])
C.预期产出:交替行和列
[[tan, cos, tan, cos],
[cos, tan, cos, tan],
[tan, cos, tan, cos],
[cos, tan, cos, tan],
[tan, cos, tan, cos]]
代码:
# import numpy as np
a = np.zeros(angles.shape)
a[0::2, 0::2] = np.tan(angles([0::2, 0::2])) # tan: odd-row, odd-column
a[0::2, 1::2] = np.cos(angles([0::2, 1::2])) # cos: odd-row, even-column
a[1::2, 0::2] = np.cos(angles([1::2, 0::2])) # cos: even-row, odd-column
a[1::2, 1::2] = np.tan(angles([1::2, 1::2])) # tan: even-row, even-column
D.交替行使用numpy
矢量化的好处
您可以使用以下自定义函数:custom_trig_func()
。如果您可以跳过numpy
循环,那么将for
函数应用于numpy数组会更快。代码越矢量化,运行速度就越快。这里的方法避免了使用for
循环,并使用内置的numpy.tan
和numpy.cos
矢量化.
custom_trig_func(angles)
但是,如果您只希望使用最少的代码行来生成自定义函数所做的工作,请使用以下命令:
# angles is your input ndarray
even_rows = (np.arange(angles.shape[0]) % 2 == 0)
out = np.tan(angles)
out[even_rows, :] = np.cos(angles[even_rows, :])
print(out.shape) # out is your expected output
代码
import numpy as np
def custom_trig_func(angles: np.ndarray, validate=False) -> np.ndarray:
"""Returns an array of type -> numpy.ndarray and shape -> angles.shape, with
- odd rows operated on by numpy.tan() and,
- even rows operated on by numpy.cos().
"""
# determine odd and even row indices
rows = np.arange(angles.shape[0])
even_rows = (rows % 2 == 0)
odd_rows = ~even_rows
# create output array
out = np.tan(angles)
out[even_rows, :] = np.cos(angles[even_rows, :])
if validate:
assert np.all(out[odd_rows, :] == np.tan(angles[odd_rows, :])), "Validation error in applying TAN to odd rows"
assert np.all(out[even_rows, :] == np.cos(angles[even_rows, :])), "Validation error in applying COS to even rows"
return out
# dummy data
angles = np.random.rand(10,5) * np.pi
# apply custom function
custom_trig_func(angles=angles, validate=True)
发布于 2020-11-08 03:43:58
这是你要找的东西吗?
import numpy as np
import math
x=np.array([x for x in range(50)]).reshape(10,5)
y=x.copy()
for i in range(x.shape[0]):
y[i][0]=math.tan(x[i][0])
y[i][1]=math.cos(x[i][1])
y[i][2]=math.tan(x[i][2])
y[i][3]=math.cos(x[i][3])
y[i][4]=math.tan(x[i][4])
print(x)
print(y)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]
[30 31 32 33 34]
[35 36 37 38 39]
[40 41 42 43 44]
[45 46 47 48 49]]
[[ 0 0 -2 0 1]
[-3 0 0 0 0]
[ 0 0 0 0 7]
[ 0 0 3 0 0]
[ 2 0 0 0 -2]
[ 0 0 -3 0 0]
[-6 0 0 0 0]
[ 0 0 0 0 3]
[-1 0 2 0 0]
[ 1 0 0 0 -3]]
https://stackoverflow.com/questions/64734436
复制相似问题