用序列创建的 numpy 数组 [英] numpy array creating with a sequence

查看:32
本文介绍了用序列创建的 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 MATLAB 过渡到 scipy(+numpy)+matplotlib.在实施某些事情时,我一直遇到问题.我想在三个不同的部分创建一个简单的向量数组.在 MATLAB 中,我会做类似的事情:

vector=[0.2,1:60,60.8];

这会产生一个包含 62 个位置的一维数组.我正在尝试使用 scipy 来实现这一点.我现在最接近的是这个:

a=[[0.2],linspace(1,60,60),[60.8]]

然而,这会创建一个列表,而不是一个数组,因此我无法将其重塑为向量数组.但是,当我这样做时,我收到一个错误

a=array([[0.2],linspace(1,60,60),[60.8]])ValueError:使用序列设置数组元素.

我相信我的主要障碍是我无法弄清楚如何在 MATLAB 中翻译这个简单的操作:

a=[1:2:20];

麻木.我知道如何访问数组中的位置,尽管在创建序列时不知道.任何帮助将不胜感激,谢谢!

解决方案

NumPy 实现了 MATLAB 的数组创建函数,vector,使用两个函数而不是一个函数——每个都隐含地指定了一个特定的轴,串联应该沿着这个轴发生.这些功能是:

  • r_(逐行串联)和

  • c_(按列)


因此,对于您的示例,NumPy 等效项是:

<预><代码>>>>将 numpy 导入为 NP>>>v = NP.r_[.2, 1:10, 60.8]>>>打印(五)[ 0.2 1. 2. 3. 4. 5. 6. 7. 8. 9. 60.8 ]

按列对应的是:

<预><代码>>>>NP.c_[.2, 1:10, 60.8]

slice 表示法按预期工作 [start:stop:step]:

<预><代码>>>>v = NP.r_[.2, 1:25:7, 60.8]>>>v数组([ 0.2, 1. , 8. , 15. , 22. , 60.8])

虽然如果虚数用作第三个参数,切片符号的行为类似于linspace:

<预><代码>>>>v = NP.r_[.2, 1:25:7j, 60.8]>>>v数组([ 0.2, 1. , 5. , 9. , 13. , 17. , 21. , 25. , 60.8])


否则,它的行为类似于 arange:

<预><代码>>>>v = NP.r_[.2, 1:25:7, 60.8]>>>v数组([ 0.2, 1. , 8. , 15. , 22. , 60.8])

I am on my transitional trip from MATLAB to scipy(+numpy)+matplotlib. I keep having issues when implementing some things. I want to create a simple vector array in three different parts. In MATLAB I would do something like:

vector=[0.2,1:60,60.8];

This results in a one dimensional array of 62 positions. I'm trying to implement this using scipy. The closest I am right now is this:

a=[[0.2],linspace(1,60,60),[60.8]]

However this creates a list, not an array, and hence I cannot reshape it to a vector array. But then, when I do this, I get an error

a=array([[0.2],linspace(1,60,60),[60.8]])
ValueError: setting an array element with a sequence.

I believe my main obstacle is that I can't figure out how to translate this simple operation in MATLAB:

a=[1:2:20];

to numpy. I know how to do it to access positions in an array, although not when creating a sequence. Any help will be appreciated, thanks!

解决方案

Well NumPy implements MATLAB's array-creation function, vector, using two functions instead of one--each implicitly specifies a particular axis along which concatenation ought to occur. These functions are:

  • r_ (row-wise concatenation) and

  • c_ (column-wise)


So for your example, the NumPy equivalent is:

>>> import numpy as NP

>>> v = NP.r_[.2, 1:10, 60.8]

>>> print(v)
     [  0.2   1.    2.    3.    4.    5.    6.    7.    8.    9.   60.8]

The column-wise counterpart is:

>>> NP.c_[.2, 1:10, 60.8]

slice notation works as expected [start:stop:step]:

>>> v = NP.r_[.2, 1:25:7, 60.8]

>>> v
  array([  0.2,   1. ,   8. ,  15. ,  22. ,  60.8])

Though if an imaginary number of used as the third argument, the slicing notation behaves like linspace:

>>> v = NP.r_[.2, 1:25:7j, 60.8]

>>> v
  array([  0.2,   1. ,   5. ,   9. ,  13. ,  17. ,  21. ,  25. ,  60.8])


Otherwise, it behaves like arange:

>>> v = NP.r_[.2, 1:25:7, 60.8]

>>> v
  array([  0.2,   1. ,   8. ,  15. ,  22. ,  60.8])

这篇关于用序列创建的 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆