numpy.stack最通俗的理解

本文详细介绍了numpy中的stack()函数如何工作,包括如何使用axis参数控制新轴的位置,并通过实例展示了不同参数设置下数组形状的变化。
该文章已生成可运行项目,

numpy.stack(arrays, axis=0)

沿着新轴连接数组的序列。

axis参数指定新轴在结果尺寸中的索引。例如,如果axis=0,它将是第一个维度,如果axis=-1,它将是最后一个维度。

  • 参数: 数组:array_like的序列每个数组必须具有相同的形状。axis:int,可选输入数组沿其堆叠的结果数组中的轴。
  • 返回: 堆叠:ndarray堆叠数组比输入数组多一个维。

上面是官方给出的解释,很难理解。

我们先从增加维度说起。

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> a.shape
(3,)
>>> b.shape
(3,)
>>> np.stack((a, b), axis=0).shape
(2, 3)
>>> np.stack((a, b), axis=1).shape
(3, 2)

我先说一说这里(2,3)(3,2)中的2是怎么来的:因为有ab两个array。如果这里我们增加一个c= np.array([3, 4, 5]),那么这里我们改变原来的np.stack就会变成下面这样:

>>> c = np.array([3, 4, 5])
>>> np.stack((a, b, c), axis=0).shape
(3, 3)
>>> np.stack((a, b, c), axis=1).shape
(3, 3)

那么因为这里是a,b,c三个array,所以这里2变成了3

接着说说这个axis参数的意义,我们可以理解这里的axis就是要增加哪一个维度,比如说这里的axis=0,就是增加第一维度,所以这里的(2,3)中的2在第一个位置。axis=1,就是增加第二维度,所以这里的(3,2)中的2在第二个位置。

我现在举一个稍微复杂的例子

>>> a = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
>>> b = np.array([[4, 5, 6], [4, 5, 6], [4, 5, 6]])
>>> a.shape
(3, 3)
>>> b.shape
(3, 3)
>>> np.stack((a, b), axis=0).shape
(2, 3, 3)
>>> np.stack((a, b), axis=1).shape
(3, 2, 3)
>>> np.stack((a, b), axis=2).shape
(3, 3, 2)

这里的2就是指的ab,而2放在什么位置是根据axis来确定的。

接着说一下矩阵的坐标

a			a的元素对应的坐标
[1 2 3]		(0,0) (0,1) (0,2)
[1 2 3]		(1,0) (1,1) (1,2)
[1 2 3]		(2,0) (2,1) (2,2)

很好理解。

接着以np.stack((a, b), axis=1)为例子

>>> np.stack((a, b), axis=1).shape
(3, 2, 3)
>>> np.stack((a, b), axis=1)
array([[[1, 2, 3],
        [4, 5, 6]],

       [[1, 2, 3],
        [4, 5, 6]],

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

原来我们a[0][0]=1,现在中间加上一个维度(因为这里axis=1),就变成了a[0][0][0]=1,注意这里为什么中间是0,因为np.stack((a, b), axis=1)中,ab的前面。同理

a			a的元素对应的坐标
[1 2 3]		(0,0,0) (0,0,1) (0,0,2)
[1 2 3]		(1,0,0) (1,0,1) (1,0,2)
[1 2 3]		(2,0,0) (2,0,1) (2,0,2)

那么b[0][0]=4,因为np.stack((a, b), axis=1)中,ba的后面。所以b[0][1][0]=4

b			b的元素对应的坐标
[4 5 6]		(0,1,0) (0,1,1) (0,1,2)
[4 5 6]		(1,1,0) (1,1,1) (1,1,2)
[4 5 6]		(2,1,0) (2,1,1) (2,1,2)

接着将对应坐标的数组合,就得到了新的array

					   元素对应的坐标
array([[[1, 2, 3],		(0,0,0) (0,0,1) (0,0,2)
        [4, 5, 6]],		(0,1,0) (0,1,1) (0,1,2)

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

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

还有一个更加简单的理解方式(堆叠)

对于axis=1,就是横着切开,对应行横着堆

对于axis=2,就是竖着切开,对应行竖着堆

对于axis=0,就是不切开,两个堆一起。

就是这么的简单_!

觉得不错,点个赞吧b( ̄▽ ̄)d

本文章已经生成可运行项目
def allrad(F_nm, hull, N_sph=None, jobs_count=1): """Loudspeaker signals of All-Round Ambisonic Decoder. Parameters ---------- F_nm : ((N_sph+1)**2, S) numpy.ndarray Matrix of spherical harmonics coefficients of spherical function(S). hull : LoudspeakerSetup N_sph : int Decoding order. jobs_count : int or None, optional Number of parallel jobs, 'None' employs 'cpu_count'. Returns ------- ls_sig : (L, S) numpy.ndarray Loudspeaker L output signal S. References ---------- Zotter, F., & Frank, M. (2012). All-Round Ambisonic Panning and Decoding. Journal of Audio Engineering Society, Sec. 6. Examples -------- .. plot:: :context: close-figs ls_setup = spa.decoder.LoudspeakerSetup(ls_x, ls_y, ls_z) ls_setup.pop_triangles(normal_limit=85, aperture_limit=90, opening_limit=150) ls_setup.ambisonics_setup(update_hull=True) spa.plot.decoder_performance(ls_setup, 'ALLRAD') """ if hull.ambisonics_hull: ambisonics_hull = hull.ambisonics_hull else: raise ValueError('Run LoudspeakerSetup.ambisonics_setup() first!') if hull.kernel_hull: kernel_hull = hull.kernel_hull else: raise ValueError('Run LoudspeakerSetup.ambisonics_setup() first!') if N_sph is None: N_sph = hull.characteristic_order N_sph_in = int(np.sqrt(F_nm.shape[0]) - 1) assert (N_sph == N_sph_in) # for now if N_sph_in > kernel_hull.N_kernel: warn("Undersampling the sphere. Needs higher N_Kernel.") # virtual t-design loudspeakers J = len(kernel_hull.points) # virtual speakers expressed as VBAP phantom sources (Kernel) G_k = vbap(src=kernel_hull.points, hull=ambisonics_hull, jobs_count=jobs_count) # SH tapering coefficients a_n = sph.max_rE_weights(N_sph) a_n = sph.unity_gain(a_n) a_nm = sph.repeat_per_order(a_n) # virtual Ambisonic decoder _k_azi, _k_zen, _k_r = utils.cart2sph(kernel_hull.points[:, 0], kernel_hull.points[:, 1], kernel_hull.points[:, 2]) # band-limited Dirac Y_bld = sph.sh_matrix(N_sph, _k_azi, _k_zen, sh_type='real') # ALLRAD Decoder D = 4 * np.pi / J * G_k.T @ Y_bld # apply tapering to decoder matrix D = D @ np.diag(a_nm) # remove imaginary loudspeakers if ambisonics_hull.imaginary_ls_idx is not None: D = np.delete(D, ambisonics_hull.imaginary_ls_idx, axis=0) # loudspeaker output signals ls_sig = D @ F_nm return ls_sig 这个函数中的输入参数到底是什么物理意义,可以解释的通俗易懂一点吗,和def ambisonics_setup(self, update_hull=False, imaginary_ls=None, characteristic_order=None, N_kernel=50): """Prepare loudspeaker hull for ambisonic rendering. Sets the `kernel_hull` as an n-design of twice `N_kernel`, and updates the ambisonic hull with an additional imaginary loudspeaker, if desired. Parameters ---------- update_hull : bool, optional imaginary_ls : (L, 3), cartesian, optional Imaginary loudspeaker positions, if set to 'None' calls 'find_imaginary_loudspeaker()' for 'update_hull'. characteristic_order : int, optional Characteristic Ambisonic order, 'None' calls 'get_characteristic_order()' N_kernel : int, optional Examples -------- .. plot:: :context: close-figs ls_setup.ambisonics_setup(update_hull=True) N_e = ls_setup.characteristic_order ls_setup.ambisonics_hull.show(title=f"Ambisonic Hull, $N_e={N_e}$") """ if characteristic_order is None: self.characteristic_order = self.get_characteristic_order() else: self.characteristic_order = characteristic_order if N_kernel is None: warn('Setting setup kernel order =', self.characteristic_order) N_kernel = self.characteristic_order if (not update_hull and imaginary_ls is not None): warn('Not updating hull but imaginary_ls position given.') ambi_ls = self.points if update_hull: if imaginary_ls is None: new_imaginary_ls = find_imaginary_loudspeaker(self) # add imaginary speaker to hull ambi_ls = np.vstack([ambi_ls, new_imaginary_ls]) # mark imaginary speaker (last one) imaginary_ls_idx = ambi_ls.shape[0] - 1 else: imaginary_ls = np.atleast_2d(imaginary_ls) assert (imaginary_ls.shape[1] == 3) # add imaginary loudspeaker(s) to hull ambi_ls = np.vstack([ambi_ls, imaginary_ls]) # mark imaginary speaker (last one(s)) imaginary_ls_idx = np.arange(ambi_ls.shape[0] - imaginary_ls.shape[0], ambi_ls.shape[0]) else: imaginary_ls_idx = None # This new triangulation is now the rendering setup ambisonics_hull = LoudspeakerSetup(ambi_ls[:, 0], ambi_ls[:, 1], ambi_ls[:, 2]) # mark imaginary speaker index ambisonics_hull.imaginary_ls_idx = imaginary_ls_idx # discretization hull virtual_speakers = grids.load_n_design(2 * N_kernel) # Avoid any extra calculation on this dense grid kernel_hull = get_hull(virtual_speakers[:, 0], virtual_speakers[:, 1], virtual_speakers[:, 2]) del ambisonics_hull.ambisonics_hull del ambisonics_hull.kernel_hull self.ambisonics_hull = ambisonics_hull self.kernel_hull = kernel_hull self.kernel_hull.N_kernel = N_kernel这个函数有什么关系,就是我现在有Ambisonics信号和sofa文件,我到底该怎么用这个库解码
最新发布
10-16
学习Python基础,尤其是寻找通俗易懂的教程或资料,可以从以下几个方面入手: ### Python基础学习的建议 1. **选择适合零基础的教程** 对于初学者,推荐选择系统性强、讲解细致的教程。例如,可以选择《史上Python入门基础教程(保姆级教程)》这样的内容,它不仅涵盖Python的基础语法,还详细讲解了编程思维和实际应用案例。这种教程适合零基础的小白,能够循序渐进地掌握Python的核心知识[^3]。 2. **注重代码实践** 学习编程离不开动手实践。可以参考一些手把手教学的教程,比如“自动化之python基础,0基础手把手教学,细的教程通俗易懂”。这类教程通常会结合实际案例,比如通过`if...else`语句来模拟生活中的决策逻辑,帮助初学者理解程序的运行机制[^4]。 3. **掌握基础语法与结构** Python的语法简洁清晰,采用缩进表示代码块。例如,以下是一个简单的条件判断示例: ```python money = 10 if money > 20: print('买盒利群') else: print('买煊赫门') ``` 这种直观的代码结构使得编程更加容易理解和学习[^4]。 4. **利用丰富的学习资源** Python拥有庞大的社区支持,提供了丰富的教程、文档和在线资源。可以参考一些优质的学习平台,例如知乎、CSDN、掘金等,这些平台上有许多针对初学者的Python教程,涵盖从基础语法到高级应用的各个方面[^3]。 5. **学习Python的内置功能和第三方库** Python内置了丰富的数据类型和库,支持多种编程范式,如面向过程、面向对象和函数式编程。此外,Python还有大量的第三方库,如NumPy、Pandas、Django、Flask等,这些库和框架大大扩展了Python的应用范围,提高了开发效率[^3]。 6. **避免盲目自学** 对于零基础的学习者来说,不建议在网上随意查找资料自学,因为现在网上的Python爬虫教程虽多,但真正面向零基础的却不多。建议选择系统性强、有专业老师指导的课程,这样不仅能够学习Python爬虫,还能掌握其他Python相关的内容,提升工作前景[^2]。 7. **关注学习路径** 在学习Python基础时,可以按照以下路径进行: - **基础语法**:包括变量、数据类型、运算符、流程控制(如条件语句、循环语句)等。 - **函数与模块**:学习如何定义和调用函数,以及如何使用模块来组织代码。 - **数据结构**:掌握列表、元组、字典、集合等常用数据结构。 - **文件操作**:学习如何读写文件,处理文本数据。 - **面向对象编程**:理解类和对象的概念,掌握封装、继承、多态等特性。 - **异常处理**:学习如何处理程序中的错误和异常。 - **常用库的使用**:如NumPy、Pandas等,提升数据处理能力[^3]。 ### 推荐的学习资源 - **书籍**:《Python编程:从入门到实践》、《Python核心编程》等。 - **在线课程**:B站上的“史上Python入门基础教程(保姆级教程)”、知乎上的“通俗易懂的Python入门基础详细教程”等。 - **社区**:Stack Overflow、GitHub、Python官方文档等,这些都是解决编程问题的好去处。
评论 11
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值