连接两个一维 NumPy 数组 [英] Concatenating two one-dimensional NumPy arrays

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

问题描述

我在 NumPy 中有两个简单的一维数组.我应该能够使用 numpy.concatenate 连接它们.但我收到以下代码的错误:

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: 只有长度为 1 的数组可以转换为 Python 标量

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

代码

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

为什么?

推荐答案

该行应该是:

numpy.concatenate([a,b])

要连接的数组需要作为序列传入,而不是作为单独的参数传入.

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

来自 NumPy 文档:

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

将一系列数组连接在一起.

Join a sequence of arrays together.

它试图将您的 b 解释为轴参数,这就是它抱怨无法将其转换为标量的原因.

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天全站免登陆