0

IN the scipy lecture notes for indexing

There is an example problem, to create this 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.]]

The problem for me is getting the extra blank row at the top. How do I acheive the example?

This is my current code.

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]])
4

5 回答 5

4

您可以使用以下参数diag单独使用和索引来执行此操作:kdiag

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]

这给出了预期的结果:

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]])
于 2019-05-12T06:17:04.177 回答
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.]])
于 2019-05-12T05:54:23.677 回答
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.]])
于 2019-05-12T06:36:50.887 回答
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
于 2019-05-12T09:51:48.713 回答
1
import numpy as np

m = np.zeros([5,])
n = np.diag(np.arange(2,7,1))

m = np.vstack((m,n))
print(m)

我们可以用vstack

于 2019-05-12T05:56:39.123 回答