无法让 numpy.transpose 对表面上看起来是数组的对象进行操作?
can't get numpy.transpose to operate on what outwardly seems to be an array?
以下代码采用排列为 3 行 14 列的列表列表并将其转换为数组。然而,numpy.flip 和 numpy.transpose 的应用似乎对新数组没有任何作用,尽管可以识别形状......尽管 'shape' 说?
fitarray = numpy.array(listoflists)
print 'original'
print fitarray
print 'shape:', fitarray.shape
#reverse order in array
numpy.flipud(numpy.fliplr(fitarray))
print 'after flipping'
print fitarray
print 'shape:' fitarray.shape
numpy.transpose(fitarray)
print 'after transpose'
print fitarray
print 'shape:' fitarray.shape
以上代码的输出如下:
原创
[[ 1. 1. 0.75922332 0.72510804 0.60655371 0.61518896
0.43338281 0.31000672 0.36051202 0.29079866 0.28775219 0.41336631
0.53799258 0.52036007]
[ 0.46761031 0.9629559 1. 1. 1. 1.
0.95181012 0.90766551 0.88126941 0.88357832 0.90511121 0.95506566
0.99609776 1. ]
[ 0.55385467 0.91574368 0.78931241 0.83173184 0.87563584 0.9592057
1. 1. 1. 1. 1. 1. 1.
0.99809394]]
shape: (3, 14)
翻转后
[[ 1. 1. 0.75922332 0.72510804 0.60655371 0.61518896
0.43338281 0.31000672 0.36051202 0.29079866 0.28775219 0.41336631
0.53799258 0.52036007]
[ 0.46761031 0.9629559 1. 1. 1. 1.
0.95181012 0.90766551 0.88126941 0.88357832 0.90511121 0.95506566
0.99609776 1. ]
[ 0.55385467 0.91574368 0.78931241 0.83173184 0.87563584 0.9592057
1. 1. 1. 1. 1. 1. 1.
0.99809394]]
shape: (3, 14)
转置后
[[ 1. 1. 0.75922332 0.72510804 0.60655371 0.61518896
0.43338281 0.31000672 0.36051202 0.29079866 0.28775219 0.41336631
0.53799258 0.52036007]
[ 0.46761031 0.9629559 1. 1. 1. 1.
0.95181012 0.90766551 0.88126941 0.88357832 0.90511121 0.95506566
0.99609776 1. ]
[ 0.55385467 0.91574368 0.78931241 0.83173184 0.87563584 0.9592057
1. 1. 1. 1. 1. 1. 1.
0.99809394]]
shape: (3, 14)
您需要根据 docs
分配运算结果
示例:
In [182]:
a = np.random.randn(3*14).reshape(3,14)
a
Out[182]:
array([[-1.09047556, -0.03717911, 1.56770073, -0.44577998, 1.89357885,
-0.53837911, 0.74492976, 0.55248619, 1.19553176, -0.90725472,
1.33540847, -0.36895309, -0.1392841 , 0.21324307],
[ 0.95891412, 0.0810524 , -0.29181996, 0.2275121 , 1.06491463,
-0.94156398, 0.04786322, 0.15859003, 0.06085846, -0.8113653 ,
-0.64495641, 0.03964511, -0.36936448, -1.21301754],
[-0.1486475 , 0.61538289, -0.42302942, 0.46333247, 1.65154601,
-0.16310981, 0.09809179, -0.37837943, 0.98993927, 0.1095821 ,
-0.69703357, -0.62647274, 1.05738354, 0.01542917]])
In [185]:
b = np.transpose(a)
print(a.shape, b.shape)
(3, 14) (14, 3)
In [189]:
a_copy = a.copy()
b = np.flipud(np.fliplr(a))
print((a_copy == a).all())
print((b == a).all())
True
False
以下代码采用排列为 3 行 14 列的列表列表并将其转换为数组。然而,numpy.flip 和 numpy.transpose 的应用似乎对新数组没有任何作用,尽管可以识别形状......尽管 'shape' 说?
fitarray = numpy.array(listoflists)
print 'original'
print fitarray
print 'shape:', fitarray.shape
#reverse order in array
numpy.flipud(numpy.fliplr(fitarray))
print 'after flipping'
print fitarray
print 'shape:' fitarray.shape
numpy.transpose(fitarray)
print 'after transpose'
print fitarray
print 'shape:' fitarray.shape
以上代码的输出如下:
原创
[[ 1. 1. 0.75922332 0.72510804 0.60655371 0.61518896
0.43338281 0.31000672 0.36051202 0.29079866 0.28775219 0.41336631
0.53799258 0.52036007]
[ 0.46761031 0.9629559 1. 1. 1. 1.
0.95181012 0.90766551 0.88126941 0.88357832 0.90511121 0.95506566
0.99609776 1. ]
[ 0.55385467 0.91574368 0.78931241 0.83173184 0.87563584 0.9592057
1. 1. 1. 1. 1. 1. 1.
0.99809394]]
shape: (3, 14)
翻转后
[[ 1. 1. 0.75922332 0.72510804 0.60655371 0.61518896
0.43338281 0.31000672 0.36051202 0.29079866 0.28775219 0.41336631
0.53799258 0.52036007]
[ 0.46761031 0.9629559 1. 1. 1. 1.
0.95181012 0.90766551 0.88126941 0.88357832 0.90511121 0.95506566
0.99609776 1. ]
[ 0.55385467 0.91574368 0.78931241 0.83173184 0.87563584 0.9592057
1. 1. 1. 1. 1. 1. 1.
0.99809394]]
shape: (3, 14)
转置后
[[ 1. 1. 0.75922332 0.72510804 0.60655371 0.61518896
0.43338281 0.31000672 0.36051202 0.29079866 0.28775219 0.41336631
0.53799258 0.52036007]
[ 0.46761031 0.9629559 1. 1. 1. 1.
0.95181012 0.90766551 0.88126941 0.88357832 0.90511121 0.95506566
0.99609776 1. ]
[ 0.55385467 0.91574368 0.78931241 0.83173184 0.87563584 0.9592057
1. 1. 1. 1. 1. 1. 1.
0.99809394]]
shape: (3, 14)
您需要根据 docs
分配运算结果示例:
In [182]:
a = np.random.randn(3*14).reshape(3,14)
a
Out[182]:
array([[-1.09047556, -0.03717911, 1.56770073, -0.44577998, 1.89357885,
-0.53837911, 0.74492976, 0.55248619, 1.19553176, -0.90725472,
1.33540847, -0.36895309, -0.1392841 , 0.21324307],
[ 0.95891412, 0.0810524 , -0.29181996, 0.2275121 , 1.06491463,
-0.94156398, 0.04786322, 0.15859003, 0.06085846, -0.8113653 ,
-0.64495641, 0.03964511, -0.36936448, -1.21301754],
[-0.1486475 , 0.61538289, -0.42302942, 0.46333247, 1.65154601,
-0.16310981, 0.09809179, -0.37837943, 0.98993927, 0.1095821 ,
-0.69703357, -0.62647274, 1.05738354, 0.01542917]])
In [185]:
b = np.transpose(a)
print(a.shape, b.shape)
(3, 14) (14, 3)
In [189]:
a_copy = a.copy()
b = np.flipud(np.fliplr(a))
print((a_copy == a).all())
print((b == a).all())
True
False