python - Numpy 连接给出错误 : axis 1 is out of bounds for array of dimension 1

标签 python

尺寸似乎正常,但它仍然提示(血压升高):

x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
m = np.array([9,10])
pointsx = np.concatenate((x,[m[0]]), axis=0)
pointsy = np.concatenate((y,[m[1]]), axis=0)
points = np.concatenate((pointsx.T,pointsy.T), axis=1)

最佳答案

可能有两种解决方案:

(1) 使用 reshape() 改变一维向量 在这里,如果 pointx 和 pointy 是一维向量,并转置它而不是使用 .T (适用于更高维度)

import numpy as np
x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
m = np.array([9,10])
pointsx = np.concatenate((x,[m[0]]), axis=0)
pointsy = np.concatenate((y,[m[1]]), axis=0)

points = np.concatenate((pointsx.reshape(-1,1),pointsy.reshape(-1,1)), axis=1)
print(points)

假设如果pointsx = [1,2,3,4],那么pointsx.reshape(-1,1)会将其转换为

[[1]
 [2]
 [3]
 [4]
 [9]]

(2) 将一维向量转换为矩阵,然后使用转置。

import numpy as np
x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
m = np.array([9,10])
pointsx = np.concatenate((x,[m[0]]), axis=0)
pointsy = np.concatenate((y,[m[1]]), axis=0)

points = np.concatenate((np.matrix(pointsx).T,np.matrix(pointsy).T), axis=1)
print(points)

关于python - Numpy 连接给出错误 : axis 1 is out of bounds for array of dimension 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66609605/

相关文章:

Python pandas 字符串处理来自 SQL 数据库的分类数据

python - 标量乘法字典值

python - 无法使用Python连接到MSSQL Server数据库

python - 将外部 python 函数作为 vim 命令运行

python 3.6 : Either I miss something either generic typing breaks super chaining for inheritance

python - 如何从点分隔的字符串列表重建 JSON?

python - 类型错误 : 'psycopg2._psycopg.Binary' object does not support indexing

python - 随机填空

python:如何从列表中打印单独的行?

python - 使用 scrapy 抓取雅虎组的问题