无法在python中打印矩阵内的数组

时间:2016-10-21 01:36:43

标签: arrays numpy matrix

我正在使用np.linalg.lstsq来执行线性最小二乘问题,该问题输出矩阵。我成功地获得了一个矩阵,但我无法将其中的一部分拉出来。 我的代码正在输出:

b = (matrix([[ -4.86924934e-03],
            [  5.74493035e-03],
            [ -4.54975137e-09],
            [  1.61423505e-09],
            [ -3.62215286e-08],
            [ -1.61423449e-09],
            [ -5.49795983e-05]]), matrix([], shape=(1, 0), dtype=float64), 6, array([  1.79115437e+05,   9.78958908e+04,   6.75604715e+04,
             3.21255052e+01,   3.00000000e+00,   2.99773807e+00,
             3.74438915e-12])) 

我已将矩阵命名为b,如果我要求



print b[0][1]




我收到5.74493035e-03的打印声明。但是我希望在b的末尾获得数组。我正在尝试使用b [1] [4]来获取整个数组,但到目前为止我收到的错误是



index 1 is out of bounds for axis 0 with size 1




如何进入第二个阵列打印它?我究竟做错了什么?为了清楚起见,我希望数组的第一个值为1.79115437e,最后一个值为3.74438915e-12。

1 个答案:

答案 0 :(得分:0)

使用以下代码:

Visibility.Collapsed

我明白了:

from numpy import *

b = (matrix([[ -4.86924934e-03],
            [  5.74493035e-03],
            [ -4.54975137e-09],
            [  1.61423505e-09],
            [ -3.62215286e-08],
            [ -1.61423449e-09],
            [ -5.49795983e-05]]),
     matrix([], shape=(1, 0),  dtype=float64),
     6,
     array([  1.79115437e+05,   9.78958908e+04,   6.75604715e+04,3.21255052e+01,   3.00000000e+00,   2.99773807e+00,3.74438915e-12]))

删除形状():

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\pydevd.py", line 1580, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\pydevd.py", line 964, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:/Users/vafaa/Documents/Pycharm_Projects/test/imsho.py", line 11, in <module>
    matrix([], shape=(1, 0),  dtype=float64),
TypeError: __new__() got an unexpected keyword argument 'shape'

结果:

from numpy import *

b = (matrix([[ -4.86924934e-03],
            [  5.74493035e-03],
            [ -4.54975137e-09],
            [  1.61423505e-09],
            [ -3.62215286e-08],
            [ -1.61423449e-09],
            [ -5.49795983e-05]]),
     matrix([],  dtype=float64),
     6,
     array([  1.79115437e+05,   9.78958908e+04,   6.75604715e+04,3.21255052e+01,   3.00000000e+00,   2.99773807e+00,3.74438915e-12]))

print b[3][0], b[3][-1]

看起来问题在于您正在处理的线性最小二乘问题的解决方案。