《统计学习方法》学习之番外:Numpy常用函数汇总

1、np.zeros()和np.ones()
  这两个函数用于生成指定形状的矩阵,元素全部为0或者全部为1,参数如下:

参数
shapeint or sequence of ints. Shape of the new array, e.g., (2, 3) or 2.
一个整数或者一个int类型的序列,表示需要生成的矩阵的形状。

|dtype | data-type, optional. The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
生成矩阵的数据类型,可选。默认是numpy.float64。|

|order | {‘C’, ‘F’}, optional. Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory.|

import numpy as np

a = np.zeros(5)

b = np.zeros([3, 4])

c = np.ones(7)

d = np.ones([4, 5])

print(a)

print('#########')

print(b)

print('#########')

print(c)

print('#########')

print(d)

结果如下:

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

2、np.ravel()
  Return a flattened array.返回一个展平后的数组。

import numpy as np

a = np.arange(15).reshape([3, 5])

b = a.ravel()

print(b)

结果如下:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

3、np.outer()
  计算两个矩阵的outer product。

参数
a(M,) array_like. First input vector. Input is flattened if not already 1-dimensional.
矩阵1,如果不是1维矩阵,该矩阵将被展开成1维矩阵。
b(N,) array_like. Second input vector. Input is flattened if not already 1-dimensional.
矩阵2,如果不是1维矩阵,该矩阵将被展开成1维矩阵。
out(M, N) ndarray, optional. A location where the result is stored.
import numpy as np

a = np.arange(15).reshape([3, 5])

print('raw matrix:\n', a)

c = np.outer(a, a)

print('calculated by np.outer:\n', c)

'''outer 计算等价于以下代码'''

t = a.ravel()[:, None]

p = a.ravel()[None, :]

d = np.multiply(t, p)

print('another way to calculate:\n', d)

结果如下:


raw matrix:

 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
calculated by np.outer:
 [[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14]
 [  0   2   4   6   8  10  12  14  16  18  20  22  24  26  28]
 [  0   3   6   9  12  15  18  21  24  27  30  33  36  39  42]
 [  0   4   8  12  16  20  24  28  32  36  40  44  48  52  56]
 [  0   5  10  15  20  25  30  35  40  45  50  55  60  65  70]
 [  0   6  12  18  24  30  36  42  48  54  60  66  72  78  84]
 [  0   7  14  21  28  35  42  49  56  63  70  77  84  91  98]
 [  0   8  16  24  32  40  48  56  64  72  80  88  96 104 112]
 [  0   9  18  27  36  45  54  63  72  81  90  99 108 117 126]
 [  0  10  20  30  40  50  60  70  80  90 100 110 120 130 140]
 [  0  11  22  33  44  55  66  77  88  99 110 121 132 143 154]
 [  0  12  24  36  48  60  72  84  96 108 120 132 144 156 168]
 [  0  13  26  39  52  65  78  91 104 117 130 143 156 169 182]
 [  0  14  28  42  56  70  84  98 112 126 140 154 168 182 196]]
another way to calculate:
 [[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14]
 [  0   2   4   6   8  10  12  14  16  18  20  22  24  26  28]
 [  0   3   6   9  12  15  18  21  24  27  30  33  36  39  42]
 [  0   4   8  12  16  20  24  28  32  36  40  44  48  52  56]
 [  0   5  10  15  20  25  30  35  40  45  50  55  60  65  70]
 [  0   6  12  18  24  30  36  42  48  54  60  66  72  78  84]
 [  0   7  14  21  28  35  42  49  56  63  70  77  84  91  98]
 [  0   8  16  24  32  40  48  56  64  72  80  88  96 104 112]
 [  0   9  18  27  36  45  54  63  72  81  90  99 108 117 126]
 [  0  10  20  30  40  50  60  70  80  90 100 110 120 130 140]
 [  0  11  22  33  44  55  66  77  88  99 110 121 132 143 154]
 [  0  12  24  36  48  60  72  84  96 108 120 132 144 156 168]
 [  0  13  26  39  52  65  78  91 104 117 130 143 156 169 182]
 [  0  14  28  42  56  70  84  98 112 126 140 154 168 182 196]]

4、np.diag()
  Extract a diagonal or construct a diagonal array.抽取一个矩阵的对角线元素,或者构建一个具有指定对角线元素的矩阵。

参数
varray_like.
If v is a 2-D array, return a copy of its k-th diagonal.
If v is a 1-D array, return a 2-D array with v on the k-th diagonal.
如果v是一个2维的矩阵,返回该矩阵第k个对角线元素的拷贝。
如果v是一个1维的数组,返回一个以v中元素作为对角线元素的2维矩阵。
kint, optional.
Diagonal in question. The default is 0. Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal.
k是一个可选参数,默认为0。
当k>0时,返回主对角线上方的对应元素;
当k<0时,返回主对角线下方的对应元素。
import numpy as np

a = np.arange(15).reshape([3, 5])

print('raw matrix:\n', a)

b = np.diag(a)

c = np.diag(a, 1)

d = np.diag(a, 10)

print(b)

print('###')

print(c)

print('###')

print(d)

print('###')

e = np.diag([1, 2, 3])

print(e)

print('###')

f = np.diag([4, 5, 6], k=2)

print(f)

print('###')

结果如下:

raw matrix:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
[ 0  6 12]
###
[ 1  7 13]
###
[]
###
[[1 0 0]
 [0 2 0]
 [0 0 3]]
###
[[0 0 4 0 0]
 [0 0 0 5 0]
 [0 0 0 0 6]
 [0 0 0 0 0]
 [0 0 0 0 0]]
###

5、np.identity()
  返回指定大小的单位矩阵。

参数
nint. Number of rows (and columns) in n * n output.
一个整数,矩阵的行数和列数。
dtypedata-type, optional. Data-type of the output. Defaults to float.
矩阵元素的数据类型,默认为float。
import numpy as np

a = np.identity(4)

print(a)

结果如下:

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

6、np.vstack()和np.hstack()
  hatsck:Stack arrays in sequence horizontally (column wise).将输入的矩阵按照水平列方向进行组合连接。
  vstakc:Stack arrays in sequence vertically (row wise).将输入的矩阵按照竖直行方向进行组合连接。

np.hstack()参数如下:

参数
tupsequence of ndarrays. The arrays must have the same shape along all but the second axis, except 1-D arrays which can be any length.
矩阵序列,这些矩阵沿着第2个轴具有相同的shape,即具有相同的列数。1维数组可以为任意长度。

np.vstack()参数如下:

参数
tupsequence of ndarrays. The arrays must have the same shape along all but the first axis. 1-D arrays must have the same length.
矩阵序列,这些矩阵除了第一维之外,其他的维度必须相同,1维数据必须为相同的长度。
import numpy as np

a = np.arange(15).reshape([3, 5])

b = np.arange(16, 16+15).reshape([3, 5])

c = np.vstack((a, b))

print(c)

d = np.hstack((a, b))

print(d)

结果如下:

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [16 17 18 19 20]
 [21 22 23 24 25]
 [26 27 28 29 30]]
[[ 0  1  2  3  4 16 17 18 19 20]
 [ 5  6  7  8  9 21 22 23 24 25]
 [10 11 12 13 14 26 27 28 29 30]]
import numpy as np

a = np.arange(15)

b = np.arange(16)

d = np.hstack((a, b))

print(d)

结果如下:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

7、np.sign()
  返回输入元素的符号:如果输入是一个数,返回该数值的符号,正数返回1,负数返回-1,0返回0;如果是一个矩阵,返回对应每一个元素符号所组成的矩阵。

参数
x输入矩阵或者一个数值。
import numpy as np

print(np.sign(-1))

print(np.sign(2))

print(np.sign(0))

print(np.sign([1, 2, -3, -4, 8]))

结果如下:

-1
1
0
[ 1  1 -1 -1  1]

2018年6月8日更新

8、np.round()
   对输入的矩阵进行四舍五入操作。该函数等价于np.around()。

参数:

参数含义
aarray_like, Input data.
输入的矩阵数据。
decimalsint, optional. Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.
可选int类型数据。小数点的位数(默认为0)。如果小数为负数,则指定小数点左边的位置数。
outndarray, optional. Alternative output array in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary.
用于存储结果的替代输出数组。它必须具有与预期输出相同的形状,但是如果需要,输出值的类型将被强制转换。
import numpy as np

array = np.random.random([3, 3]) * 100

print(array)

array_b = np.round(array, 2)

array_c = np.round(array)

array_d = np.round(array, -2)

print(array_b)

print('*' * 8)

print(array_c)

print('*' * 8)

print(array_d)

   结果如下:

[[65.54722546 87.58858673 10.59354906]
 [ 2.93457561 57.8042861  52.1987257 ]
 [54.4634482  74.77848184 52.90866199]]
[[65.55 87.59 10.59]
 [ 2.93 57.8  52.2 ]
 [54.46 74.78 52.91]]
********
[[66. 88. 11.]
 [ 3. 58. 52.]
 [54. 75. 53.]]
********
[[100. 100.   0.]
 [  0. 100. 100.]
 [100. 100. 100.]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值