连接两个一维numpy的阵列 [英] Concatenating two one-dimensional NumPy arrays

查看:267
本文介绍了连接两个一维numpy的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 numpy的两个简单的一维数组。我应该能够使用将它们串联<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html\">numpy.concatenate.但我得到这个错误低于code:


  

类型错误:仅长度-1阵列可以转换到Python标量


code

 进口numpy的
一个= numpy.array([1,2,3])
B = numpy.array([5,6])
numpy.concatenate(A,B)

为什么?


解决方案

行应该是:

  numpy.concatenate([A,B])

要连接的阵列需要传过来的顺序,而不是作为单独的参数。

numpy的文档


  

numpy.concatenate((A1,A2,...),轴= 0)


  
  

加入阵列的序列在一起。


这是试图间preT你的 B 为轴心的参数,这就是为什么它抱怨它不能将它转换成一个标量。

I have two simple one-dimensional arrays in NumPy. I should be able to concatenate them using numpy.concatenate. But I get this error for the code below:

TypeError: only length-1 arrays can be converted to Python scalars

Code

import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([5, 6])
numpy.concatenate(a, b)

Why?

解决方案

The line should be:

numpy.concatenate([a,b])

The arrays you want to concatenate need to passed in as a sequence, not as separate arguments.

From the NumPy documentation:

numpy.concatenate((a1, a2, ...), axis=0)

Join a sequence of arrays together.

It was trying to interpret your b as the axis parameter, which is why it complained it couldn't convert it into a scalar.

这篇关于连接两个一维numpy的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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