English 中文(简体)
随序列创建的numpy 数组
原标题:numpy array creating with a sequence

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];

这导致62个位置的一维阵列。 我试图使用 scipy 来执行这个阵列。 我现在最接近的是:

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

然而, 这样可以创建一个列表, 而不是数组, 因此我无法将它重塑为矢量矩阵。 但是, 当我这样做时, 我有一个错误

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

我相信我的主要障碍是 我无法在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!

最佳回答

WellmPy 执行 MATLAB s 阵列生成函数, Victor , 使用 two 函数, 而不是单向暗含指定一个特定的轴, 并围绕该轴进行 concatenation 。 这些函数是:

  • <强>r/强>(滚式连接)和

  • <强>c/强>(按列)


例如,NumPy等值是:

>>> 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]

列中对应词是:

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

selice 批注工作如预期[start:stop:step ]:

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

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

虽然如果使用一个 imaginary number 来作为第三个参数, 剪切的音符表现得像 < engene{em>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])
问题回答

你可以尝试一些东西,比如:

a = np.hstack(([0.2],np.linspace(1,60,60),[60.8]))
np.concatenate([[.2], linspace(1,60,60), [60.8]])

arange(0.2,60.8,0.2) 是否做你想做的事?

http://docs.scipy.org/doc/numpy/reference/genized/numpy.arange.html

我有点喜欢你提到的构建这些分隔范围的想法。如果你使用它们,也许一个小的函数,比如:

import numpy as np

def segrange(*args):
    result = []
    for arg in args:
        if hasattr(arg, __iter__ ):
            result.append(range(*arg))
        else:
            result.append([arg])
    return np.concatenate(result)

给您一个

>>> segrange(1., (2,5), (5,10,2))
[ 1.  2.  3.  4.  5.  7.  9.]

不过,我可能会用混凝土/黑匣子来回答问题。

如果我正确理解这块垫子 你可以用这个来完成这个任务

a=np.array([0.2]+list(range(1,61))+[60.8])

但也许有一个更好的方法... list(range(1,61)) 可以是 range(1,61) ,如果你再使用python 2.X,则可以是 range(1,61)

此工作方式是创建 3 个列表, 然后使用 < code_ / code > 运算符将其配置 。

你最初的尝试失败的原因 是因为

a=[[0.2],np.linspace(1,60,60,60,60] 创建一个清单 -- -- 换句话说:

a[0] == [0.2] #another list (length 1)
a[1] == np.linspace(1,60,60) #an array (length 60)
a[2] == [60.8] #another list (length 1)

array 函数期望一个可循环的序列,或相同长度的序列序列。

只想为从 MATLAB 到 Numpy 的任何其他人指出, 您可以用冒号构建 np.r_ 阵列, 然后用它来索引

例如,如果您在马特拉布

arr_ones = ones(10,10)

或在纽菲

arr_ones = np.ones([10,10])

你可以在Matlab只取一至五栏和七栏 像这样:

arr_ones(:,[1:5 7])

Doing the same in Numpy is not (at least for me) intuitive. This will give you an "invalid syntax" error:

arr_ones[:,[1:5,7]]

然而,这种作用是:

inds = np.r[1:5,]
arr_ones[:,inds]

我知道这在技术上不是一个新的答案, 但是在Matlab使用结肠构建一个阵列时, 将矩阵指数化似乎很自然, 我敢打赌, 许多人来到这页会想知道这个消息。 (我来到这里, 而不是问一个新问题 ) 。

使用 numpy. repeat () numpy.tile () 的最简单方式

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

np.r_[np.repeat(a,3),np.tile(a,3)]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签