python - numpy.concatenate float64(101,1) 和 float64(101,)

标签 python numpy concatenation

我是一位 MatLab 用户,最近转换为 python。我正在运行一个 for 循环,将较长的信号切割成单独的试验,将它们标准化为 100% 试验,然后希望将试验水平列在单个变量中。我的代码是

RHipFE=np.empty([101, 1])

newlength = 101

for i in range(0,len(R0X)-1,2):
    iHipFE=redataf.RHipFE[R0X[i]:R0X[i+1]]
    x=np.arange(0,len(iHipFE),1)
    new_x = np.linspace(x.min(), x.max(), newlength)
    iHipFEn = interpolate.interp1d(x, iHipFE)(new_x)
    RHipFE=np.concatenate((RHipFE,iHipFEn),axis=1)

当我运行此命令时,出现错误“ValueError:所有输入数组必须具有相同的维数”。我认为这是因为 RHipFE 是 (101,1) 而 iHipFEn 是 (101,)。是制作 iHipFEn (101,1) 的最佳解决方案吗?如果是这样,如何在上面的 for 循环中执行此操作?

最佳答案

通常,在列表中收集数组并使用某种形式的连接一次会更快。列表appendconcatenate更快:

In [51]: alist = []
In [52]: for i in range(3):
    ...:     alist.append(np.arange(i,i+5))
    ...:     
In [53]: alist
Out[53]: [array([0, 1, 2, 3, 4]), array([1, 2, 3, 4, 5]), array([2, 3, 4, 5, 6])]

多种加入方式

In [54]: np.vstack(alist)  
Out[54]: 
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6]])
In [55]: np.column_stack(alist)  
Out[55]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])
In [56]: np.stack(alist, axis=1)
Out[56]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])
In [57]: np.array(alist)
Out[57]: 
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6]])

在内部,vstackcolumn_stackstack扩展组件的维度,并在适当的轴上连接:

In [58]: np.concatenate([l[:,None] for l in alist],axis=1)
Out[58]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])

关于python - numpy.concatenate float64(101,1) 和 float64(101,),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50457003/

相关文章:

python - 如何填充嵌套列表

python - 在 Python 中读取原始二进制图像

在 C 中使用 ## 运算符连接字符串

python 3 : how to use the press Ctrl+X (cut) and Ctrl+V using pynput?

python - 无法使用 zip() 从列表列表中获取平均颜色(红色、绿色、蓝色)

Python:根据某些列过滤 numpy 值

python - np.concatenate ND张量/数组与一维数组

c - 如何连接两个 Lua lua L_Buffer 对象?

python - 将大型 DataFrame 从 Pandas 加载到 Postgresql

python - Python : ImportError: No module named . 问题 .. [Linux]