返回介绍

NDArray

发布于 2025-02-25 23:43:39 字数 3855 浏览 0 评论 0 收藏 0

The base structure in numpy is ndarray , used to represent vectors, matrices and higher-dimensional arrays. Each ndarray has the following attributes:

  • dtype = correspond to data types in C
  • shape = dimensionns of array
  • strides = number of bytes to step in each direction when traversing the array
x = np.array([1,2,3,4,5,6])
print x
print 'dytpe', x.dtype
print 'shape', x.shape
print 'strides', x.strides
[1 2 3 4 5 6]
dytpe int64
shape (6,)
strides (8,)
x.shape = (2,3)
print x
print 'dytpe', x.dtype
print 'shape', x.shape
print 'strides', x.strides
[[1 2 3]
 [4 5 6]]
dytpe int64
shape (2, 3)
strides (24, 8)
x = x.astype('complex')
print x
print 'dytpe', x.dtype
print 'shape', x.shape
print 'strides', x.strides
[[ 1.+0.j  2.+0.j  3.+0.j]
 [ 4.+0.j  5.+0.j  6.+0.j]]
dytpe complex128
shape (2, 3)
strides (48, 16)

Creating arrays

# from lists
x_list = [(i,j) for i in range(2) for j in range(3)]
print x_list, '\n'
x_array = np.array(x_list)
print x_array
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]

[[0 0]
 [0 1]
 [0 2]
 [1 0]
 [1 1]
 [1 2]]
# Using convenience functions

print np.ones((3,2)), '\n'
print np.zeros((3,2)), '\n'
print np.eye(3), '\n'
print np.diag([1,2,3]), '\n'
print np.fromfunction(lambda i, j: (i-2)**2+(j-2)**2, (5,5))
[[ 1.  1.]
 [ 1.  1.]
 [ 1.  1.]]

[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

[[1 0 0]
 [0 2 0]
 [0 0 3]]

[[ 8.  5.  4.  5.  8.]
 [ 5.  2.  1.  2.  5.]
 [ 4.  1.  0.  1.  4.]
 [ 5.  2.  1.  2.  5.]
 [ 8.  5.  4.  5.  8.]]

Array indexing

# Create a 10 by 6 array from normal deviates and convert to ints
n, nrows, ncols = 100, 10, 6
xs = np.random.normal(n, 15, size=(nrows, ncols)).astype('int')
xs
array([[ 84, 108,  96,  93,  82, 115],
       [ 87,  70,  96, 132, 111, 108],
       [ 96,  85, 120,  72,  62,  66],
       [112,  86,  98,  86,  74,  98],
       [ 75,  91, 116, 105,  82, 122],
       [ 95, 119,  84,  89,  93,  87],
       [118, 113,  94,  89,  67, 107],
       [120, 105,  85, 100, 131, 120],
       [ 91, 137, 103,  94, 115,  92],
       [ 73,  98,  81, 106, 128,  75]])
# Use slice notation
print(xs[0,0])
print(xs[-1,-1])
print(xs[3,:])
print(xs[:,0])
print(xs[::2,::2])
print(xs[2:5,2:5])
84
75
[112  86  98  86  74  98]
[ 84  87  96 112  75  95 118 120  91  73]
[[ 84  96  82]
 [ 96 120  62]
 [ 75 116  82]
 [118  94  67]
 [ 91 103 115]]
[[120  72  62]
 [ 98  86  74]
 [116 105  82]]
#  Indexing with list of integers
print(xs[0, [1,2,4,5]])
[108  96  82 115]
# Boolean indexing
print(xs[xs % 2 == 0])
xs[xs % 2 == 0] = 0 # set even entries to zero
print(xs)
[ 84 108  96  82  70  96 132 108  96 120  72  62  66 112  86  98  86  74
  98 116  82 122  84 118  94 120 100 120  94  92  98 106 128]
[[  0   0   0  93   0 115]
 [ 87   0   0   0 111   0]
 [  0  85   0   0   0   0]
 [  0   0   0   0   0   0]
 [ 75  91   0 105   0   0]
 [ 95 119   0  89  93  87]
 [  0 113   0  89  67 107]
 [  0 105  85   0 131   0]
 [ 91 137 103   0 115   0]
 [ 73   0  81   0   0  75]]
# Extracting lower triangular, diagonal and upper triangular matrices

a = np.arange(16).reshape(4,4)
print a, '\n'
print np.tril(a, -1), '\n'
print np.diag(np.diag(a)), '\n'
print np.triu(a, 1)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

[[ 0  0  0  0]
 [ 4  0  0  0]
 [ 8  9  0  0]
 [12 13 14  0]]

[[ 0  0  0  0]
 [ 0  5  0  0]
 [ 0  0 10  0]
 [ 0  0  0 15]]

[[ 0  1  2  3]
 [ 0  0  6  7]
 [ 0  0  0 11]
 [ 0  0  0  0]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文