如何解决如何在Python中将交替三角函数添加到数组中? 代码:代码:代码:代码
所以我想将交替的Cos和Tan函数添加到具有给定大小的python数组中。例如。我有一个大小为(10,5)的numpy数组,我想为每行添加(Tan(a),Cos(a),Tan(a),Cos(a),Tan(a))。有人可以提示从哪里开始吗?
我尝试设置一个使用while循环的计数器,条件是该计数器小于5,但这似乎不起作用。非常感谢您的建议。
解决方法
解决方案
自从您请求轮换以来,我想到了一种更通用的答案,其中包含三种轮换类型:
- 替代
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],[cos,cos,cos],[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]]
代码:
# 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]]
代码:
# 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,:])
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)
,
您要寻找的东西吗?
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]]
,
像这样吗?
arr[:,::2] = np.tan(arr[:,::2])
arr[:,1::2] = np.cos(arr[:,1::2])
我很大胆,假设您不太熟悉numpy中的切片符号和索引,因此您可以考虑阅读以下文档:{{3 }}。
,您可以遍历转置的numpy数组和三角函数(使用列表推导使其适合数组形状),这是我的原始尝试:
import numpy as np
a = np.arange(50).reshape(10,5)
b = [([np.tan,np.cos]*(round(a.shape[1]//2+1)))[:-1] if a.shape[1] % 2 != 0 else [np.tan,np.cos]*(round(a.shape[1]/2))]
a = np.array([y(x) for x,y in zip(a.T,*b)])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。