python函数应用(1)

 2023-09-06 阅读 30 评论 0

摘要:1.append函数纵向合并 a = np.array([[1,2],[3,4]]) b = np.array([[5,6],[7,8]]) c = np.append(a,b,0) print c [[1 2] [3 4] [5 6] [7 8]] c = np.append(a,b)输出[1 2 3 4 5 6 7 8] 2.swapaxes函数 a = np.array([[1,2,3],[4,5,6]]) a.swapaxes(0,1)

1.append函数纵向合并

a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
c = np.append(a,b,0)
print c

[[1 2]
[3 4]
[5 6]
[7 8]]

c = np.append(a,b)输出[1 2 3 4 5 6 7 8]

2.swapaxes函数

a = np.array([[1,2,3],[4,5,6]])
a.swapaxes(0,1)

array([[1, 4],
[2, 5],
[3, 6]])

3.dot函数:计算矩阵内积

a = np.mat([[1,2,3],[4,5,6]])
b = np.mat([[7,8],[9,10],[11,12]])
c = np.dot(a,b)
print c

[[ 58 64]
[139 154]]

4.numpy.apply_along_axis(func, axis, arr, *args, **kwargs):

必选参数:func,axis,arr。其中func是我们自定义的一个函数,函数func(arr)中的arr是一个数组,函数的主要功能就是对数组里的每一个元素进行变换,得到目标的结果。

其中axis表示函数func对数组arr作用的轴。

可选参数:*args, **kwargs。都是func()函数额外的参数。

返回值:numpy.apply_along_axis()函数返回的是一个根据func()函数以及维度axis运算后得到的的数组.

实例:

def my_func(a):

return (a[0] + a[-1]) * 0.5

b=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

np.apply_along_axis(my_func, 0, b)

输出: array([ 5., 6., 7., 8.])

np.apply_along_axis(my_func, 1, b)

输出: array([ 2.5, 6.5, 10.5])

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/8731.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息