【问题标题】:`TypeError: invalid type promotion` when concatenating two numpy arrays连接两个numpy数组时的`TypeError:无效类型提升`
【发布时间】:2021-01-14 19:24:45
【问题描述】:

我想我对 NumPy 还不是很熟悉。我有一个问题,我想创建类似矩阵的东西。为此,我使用np.datetime64 创建我的时间向量,它工作得很好。我得到一个形状为(300001,), dtype('<M8[us]') 的数组。所以我想将它与我的另一个形状为(300001, 12), dtype('float64') 的矩阵连接起来。我想主要问题是 dtype 因为如果我尝试连接我会在标题中得到错误。要连接我使用以下内容:

com_interpolated_matrix = np.concatenate((time_vector, interpolated_matrix), axis=1)

我该如何解决这个问题?

【问题讨论】:

  • 数组是同构的。它们不能包含两种不同的数据类型。听起来 Pandas 会更符合您的需求?
  • 您希望制作什么dtypeshape
  • 我希望得到形状 (300001, 13)。只要有效,dtype 并不重要
  • @alkasm 实际上解决了我的问题。非常感谢我切换到熊猫。

标签: python python-3.x numpy matrix


【解决方案1】:

你可以使用 numpy.column_stack:

np.column_stack((array1,array2))

【讨论】:

  • 这将如何帮助解决此错误? column_stack 将纠正他的形状不匹配,但不处理 dtype 错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 2021-04-11
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
相关资源
最近更新 更多