python - Numpy 形状相同,均值返回不同的形状

标签 python arrays numpy

这个问题在这里已经有了答案:





What are the differences between numpy arrays and matrices? Which one should I use?

(6 个回答)


去年关闭。




我有以下片段。

values = [[0.1, 0.7, 0.5], [0.6, 0.3, 0.2], [0.2, 0.8, 0.77]]
A = np.array(values).reshape(3,3)
print A.shape
print np.mean(A, axis=1)

B = np.mat(np.random.rand(3, 3));
print B.shape
print np.mean(B, axis=1)

打印语句的输出:
(3, 3)
[ 0.43333333  0.36666667  0.59      ]

(3, 3)
[[ 0.47252016]
 [ 0.44380355]
 [ 0.51070646]]

我有两个具有不同值的相同形状的 numpy 数组作为输入,一个是使用 rand 生成的另一个函数是用 array 创建的 python 列表。函数并在其上调用 reshape。

但是,即使输入形状相同,两者的平均 yield 形状也是不同的。有什么想法会导致这种情况吗?

最佳答案

numpy.matrix 的文档中所述,

A matrix is a specialized 2-D array that retains its 2-D nature through operations



此外,

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

关于python - Numpy 形状相同,均值返回不同的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59818048/

相关文章:

javascript - JSON.parse 给我对象而不是结果

python - 安装 numpy 从

python - 如何使用 ctypes 将数组从 Go [lang] 返回到 Python?

python - 如何使用 Appium 或命令行(无越狱)使用密码解锁 iOS 设备

javascript - apply 如何与采用逗号分隔参数的函数配合使用?

python - cv2.imshow() 给出黑屏

c++ - 覆盖数组索引运算符

python - Pandas 与 json 之间的时间戳

python - 无法使用 psycopg2 连接到远程 postgreSQL

python - 按整数排列元组列表