如何创建仅包含特定形状的inf的数组?

时间:2019-07-28 21:59:54

标签: python-3.x matlab numpy pycharm

这是Matlab向量:a = [inf(m,1);ones(m,1)]

我试图以python方式创建类似的对象。

我尝试过:

import numpy as np

a = np.stack((np.tile(np.array([np.inf]), (m, 0))),np.ones((m,0)))

通过在控制台中对此进行测试,例如将m更改为5,然后尝试使用PyCharm进行查看,我得到了以下消息:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydev_comm\server.py", line 34, in handle
    self.processor.process(iprot, oprot)
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 266, in process
    self.handle_exception(e, result)
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 254, in handle_exception
    raise e
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 263, in process
    result.success = call()
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 228, in call
    return f(*(args.__dict__[k] for k in api_args))
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydev_bundle\pydev_console_utils.py", line 359, in getArray
    return pydevd_thrift.table_like_struct_to_thrift_struct(array, name, roffset, coffset, rows, cols, format)
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydevd_bundle\pydevd_thrift.py", line 602, in table_like_struct_to_thrift_struct
    return TYPE_TO_THRIFT_STRUCT_CONVERTERS[type_name](array, name, roffset, coffset, rows, cols, format)
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydevd_bundle\pydevd_thrift.py", line 377, in array_to_thrift_struct
    array, array_chunk, r, c, f = array_to_meta_thrift_struct(array, name, format)
  File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydevd_bundle\pydevd_thrift.py", line 476, in array_to_meta_thrift_struct
    bounds = (array.min(), array.max())
  File "C:\Users\Azerty\PycharmProjects\OptionsHedgeFund\venv37\lib\site-packages\numpy\core\_methods.py", line 32, in _amin
    return umr_minimum(a, axis, None, out, keepdims, initial)
ValueError: zero-size array to reduction operation minimum which has no identity

怎么了?

3 个答案:

答案 0 :(得分:2)

因此,如果我正确理解了您的预期结果,您可能会这样做以生成矩阵。

m = 5 #or whatever

a = np.ones((2, m)) #create a matrix 2 x m of ones
a[0] = np.inf #replace first row with infinites

a是:

array([[inf, inf, inf, inf, inf],
       [ 1.,  1.,  1.,  1.,  1.]])

答案 1 :(得分:2)

在八度会话中:

>> m = 5;
>> a = [inf(m,1); ones(m,1)];
>> size(a)
ans =

   10    1

>> a
a =

   Inf
   Inf
   Inf
   Inf
   Inf
     1
     1
     1
     1
     1

ipython会话中:

In [21]: m=5                                                                                                 
In [22]: np.vstack([np.ones((m,1))*np.inf, np.ones((m,1))])                                                  
Out[22]: 
array([[inf],
       [inf],
       [inf],
       [inf],
       [inf],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.]])
In [23]: _.shape                                                                                             
Out[23]: (10, 1)

具有相同功能的变体:`

np.concatenate([np.full((m,1), np.inf), np.ones((m,1))], axis=0)

===

对于两行,以(1,m)形状开始:

>> a = [inf(1,m); ones(1,m)];
>> size(a)
ans =

   2   5

>> a
a =

   Inf   Inf   Inf   Inf   Inf
     1     1     1     1     1

In [26]: np.concatenate([np.full((1,m), np.inf), np.ones((1,m))], axis=0)                                    
Out[26]: 
array([[inf, inf, inf, inf, inf],
       [ 1.,  1.,  1.,  1.,  1.]])

===

对于您的错误,np.stack在连接两个(5,0)形状的数组时遇到问题(我不确定为什么)。

In [27]: a = np.stack((np.tile(np.array([np.inf]), (m, 0))),np.ones((m,0)))                                  
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-c6f8ec8462c9> in <module>
----> 1 a = np.stack((np.tile(np.array([np.inf]), (m, 0))),np.ones((m,0)))

/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py in stack(arrays, axis, out)
    417 
    418     result_ndim = arrays[0].ndim + 1
--> 419     axis = normalize_axis_index(axis, result_ndim)
    420 
    421     sl = (slice(None),) * axis + (_nx.newaxis,)

TypeError: only size-1 arrays can be converted to Python scalars

您的错误有所不同;看起来它是由pydev而不是Python本身产生的。

但是检查碎片:

In [28]: np.tile(np.array([np.inf]), (m, 0))                                                                 
Out[28]: array([], shape=(5, 0), dtype=float64)
In [29]: np.ones((m,0))                                                                                      
Out[29]: array([], shape=(5, 0), dtype=float64)

In [31]: a = np.vstack([(np.tile(np.array([np.inf]), (m, 0))),np.ones((m,0))])                               
In [32]: a                                                                                                   
Out[32]: array([], shape=(10, 0), dtype=float64)

将(m,0)替换为(m,1),我们得到所需的(10,1)数组:

In [33]: a = np.vstack([(np.tile(np.array([np.inf]), (m, 1))),np.ones((m,1))]) 

stack添加一个尺寸-这不是您想要的尺寸:

In [35]: a = np.stack([(np.tile(np.array([np.inf]), (m, 1))),np.ones((m,1))])                                
In [36]: a.shape                                                                                             
Out[36]: (2, 5, 1)

尽管起始形状很简单,但我们得到了2行:

In [37]: a = np.stack([(np.tile(np.array([np.inf]), (m,))),np.ones((m))])                                    
In [38]: a.shape                                                                                             
Out[38]: (2, 5)

实际上,您的stack的问题是放错位置的)。这是正确的:

In [50]: np.stack((np.tile(np.array([np.inf]), (m, 0)),np.ones((m,0))))                                      
Out[50]: array([], shape=(2, 5, 0), dtype=float64)

您的结束)位于tile之后,因此np.ones()表达式位于axis参数位置。

答案 2 :(得分:0)

Numpy具有专用于填充数组的功能。参见“ full()”,“ fill()”。

a = np.full((2, 2), np.inf)
array([[inf, inf],
       [inf, inf]])