scipy讲义,创建数组示例

时间:2019-05-12 05:38:06

标签: python numpy scipy

indexing的科学演讲笔记中

创建此数组存在一个示例问题。

[[0., 0., 0., 0., 0.],
 [2., 0., 0., 0., 0.],
 [0., 3., 0., 0., 0.],
 [0., 0., 4., 0., 0.],
 [0., 0., 0., 5., 0.],
 [0., 0., 0., 0., 6.]]

对我来说,问题在于顶部多余的空白行。如何实现示例?

这是我当前的代码。

d =np.zeros([5,],dtype=int) + np.diag(arange(2,7,1))

d
Out[66]: 
array([[2, 0, 0, 0, 0],
       [0, 3, 0, 0, 0],
       [0, 0, 4, 0, 0],
       [0, 0, 0, 5, 0],
       [0, 0, 0, 0, 6]])

5 个答案:

答案 0 :(得分:4)

您可以使用diag进行此操作,并使用k的{​​{1}}参数来单独建立索引:

diag

给予:

np.diag(np.arange(2,7), k = -1)

那几乎是正确的。您只需要丢失可以用切片完成的最后一列:

array([[0, 0, 0, 0, 0, 0],
       [2, 0, 0, 0, 0, 0],
       [0, 3, 0, 0, 0, 0],
       [0, 0, 4, 0, 0, 0],
       [0, 0, 0, 5, 0, 0],
       [0, 0, 0, 0, 6, 0]])

给出所需的结果:

np.diag(np.arange(2,7), k = -1)[:, :-1]

答案 1 :(得分:2)

使用np.append

>>> zero_row = np.zeros((1,5))
>>> matrix   = np.diag(np.arange(2,7,1))
>>> np.append(zero_row, matrix, axis=0)
<<< array([[0., 0., 0., 0., 0.],
           [2., 0., 0., 0., 0.],
           [0., 3., 0., 0., 0.],
           [0., 0., 4., 0., 0.],
           [0., 0., 0., 5., 0.],
           [0., 0., 0., 0., 6.]])

答案 2 :(得分:2)

这是不使用diag的情况下生成数组的一种方法。相反,我在zeros数组中索引了对角线的元素集:

In [167]: x = np.zeros((6,5))                                                     
In [168]: x[np.arange(1,6), np.arange(5)] = np.arange(2,7)                        
In [169]: x                                                                     
Out[169]: 
array([[0., 0., 0., 0., 0.],
       [2., 0., 0., 0., 0.],
       [0., 3., 0., 0., 0.],
       [0., 0., 4., 0., 0.],
       [0., 0., 0., 5., 0.],
       [0., 0., 0., 0., 6.]])

答案 3 :(得分:1)

Failed to resolve: androidx.databinding:databinding-runtime:3.2.1

Failed to resolve: androidx.databinding:databinding-adapters:3.2.1

Failed to resolve: androidx.appcompat:appcompat:1.0.0

Failed to resolve: androidx.legacy:legacy-support-v4:1.0.0

Failed to resolve: com.google.android.material:material:1.0.0
Install Repository and sync project

Failed to resolve: androidx.cardview:cardview:1.0.0

Failed to resolve: androidx.lifecycle:lifecycle-runtime:2.0.0

Failed to resolve: androidx.lifecycle:lifecycle-extensions:2.0.0

Failed to resolve: androidx.room:room-runtime:2.1.0-alpha06

Failed to resolve: androidx.test:runner:1.1.1

Failed to resolve: androidx.test:rules:1.1.1

Failed to resolve: androidx.room:room-testing:2.1.0-alpha06

Failed to resolve: androidx.arch.core:core-testing:2.0.1

Failed to resolve: androidx.test.espresso:espresso-core:3.1.1

Failed to resolve: androidx.test.espresso:espresso-contrib:3.1.1

Failed to resolve: androidx.test.espresso:espresso-intents:3.1.1

Failed to resolve: androidx.annotation:annotation:1.0.0

我们可以使用import numpy as np m = np.zeros([5,]) n = np.diag(np.arange(2,7,1)) m = np.vstack((m,n)) print(m)

答案 4 :(得分:1)

您也可以像这样使用reshape

out = np.zeros((6, 5))                                                              
out.reshape(5, 6)[:, 5] = np.arange(2, 7)                                            
out                                                                                                         
# array([[0., 0., 0., 0., 0.],
#        [2., 0., 0., 0., 0.],
#        [0., 3., 0., 0., 0.],
#        [0., 0., 4., 0., 0.],
#        [0., 0., 0., 5., 0.],
#        [0., 0., 0., 0., 6.]])

或非常相似:

out = np.zeros((6, 5))
out.reshape(-1)[5::6] = np.arange(2, 7)
out
# array([[0., 0., 0., 0., 0.],
#        [2., 0., 0., 0., 0.],
#        [0., 3., 0., 0., 0.],
#        [0., 0., 4., 0., 0.],
#        [0., 0., 0., 5., 0.],
#        [0., 0., 0., 0., 6.]])

这两种方法都比迄今为止发布的所有方法都快:

import numpy as np
from timeit import timeit

def od_hpj():
    out = np.zeros((6, 5))
    out[np.arange(1,6), np.arange(5)] = np.arange(2,7)                        
    return out

def od_mm():
    return np.diag(np.arange(2,7), k = -1)[:, :-1]

def od_ks():
    m = np.zeros([5,])
    n = np.diag(np.arange(2,7,1))
    return np.vstack((m,n))

def od_as():
    zero_row = np.zeros((1,5))
    matrix   = np.diag(np.arange(2,7,1))
    return np.append(zero_row, matrix, axis=0)

def od_pp1():
    out = np.zeros((6, 5))
    out.reshape(5, 6)[:, 5] = np.arange(2, 7)
    return out

def od_pp2():
    out = np.zeros((6, 5))
    out.reshape(-1)[5::6] = np.arange(2, 7)
    return out

for n, o in list(globals().items()):
    if n.startswith("od_"):
        print(f"{n.replace('od_', ''):3s}: {timeit(o):.3f} us")

样品运行:

hpj: 3.379 us
mm : 2.952 us
ks : 7.804 us
as : 5.222 us
pp1: 1.735 us
pp2: 2.418 us