脾脏换位没有给出预期的结果 [英] Numpy transpose not giving expected result

查看:91
本文介绍了脾脏换位没有给出预期的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为转置方法在Python scipy模块中使用一个非常基本的示例,但未给出预期的结果.我在pylab模式下使用Ipython.

I am trying a very basic example in Python scipy module for transpose method but is not giving expected result. I am using Ipython with pylab mode.

a = array([1,2,3]
print a.shape
>> (3,)
b = a.transpose()
print b.shape
>> (3,)

如果我打印数组"a"和"b"的内容,它们是相似的.

If i print the contents of arrays "a" and "b", they are similar.

期望是:(这将在Matlab中进行转置)

Expectation is : (which will be result in Matlab om transpose)

 [1,
  2,
  3]

推荐答案

NumPy的transpose()有效地反转了数组的形状.如果数组是一维的,则意味着它无效.

NumPy's transpose() effectively reverses the shape of an array. If the array is one-dimensional, this means it has no effect.

在NumPy中,数组

array([1, 2, 3])

array([1,
       2,
       3])

实际上是相同的–它们只是空白不同.您可能想要的是相应的二维数组,对于该数组,transpose()可以正常工作.还可以考虑使用NumPy的matrix类型:

are actually the same – they only differ in whitespace. What you probably want are the corresponding two-dimensional arrays, for which transpose() would work fine. Also consider using NumPy's matrix type:

In [1]: numpy.matrix([1, 2, 3])
Out[1]: matrix([[1, 2, 3]])

In [2]: numpy.matrix([1, 2, 3]).T
Out[2]: 
matrix([[1],
        [2],
        [3]])

请注意,对于大多数应用程序而言,简单的一维数组既可以作为行向量也可以作为列向量,但是从Matlab提取时,您可能更喜欢使用numpy.matrix.

Note that for most applications, the plain one-dimensional array would work fine as both a row or column vector, but when coming from Matlab, you might prefer using numpy.matrix.

这篇关于脾脏换位没有给出预期的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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