Fork me on GitHub

numpy入门25个案例

开始更新numpy相关的文章,本文介绍numpy中的25个小案例,主要内容是如何利用numpy来生成向量(一维数组),矩阵和高维数组等

numpy介绍

NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

NumPy 是一个运行速度非常快的数学库,主要用于数组计算,包含:

  • 一个强大的N维数组对象 ndarray
  • 广播功能函数
  • 整合 C/C++/Fortran 代码的工具
  • 线性代数、傅里叶变换、随机数生成等功能

导入numpy

1
import numpy as np

打印numpy的版本和配置信息

1
print(np.version)
<module 'numpy.version' from '/Applications/downloads/anaconda/anaconda3/lib/python3.7/site-packages/numpy/version.py'>
1
print(np.show_config)
<function show at 0x1060cc560>

查看函数帮助文档

1
# np.info(np.abs)

创建0向量

1
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
1
np.zeros((5,2))
array([[0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.]])
1
np.zeros((2,2,3))
array([[[0., 0., 0.],
        [0., 0., 0.]],

       [[0., 0., 0.],
        [0., 0., 0.]]])

改变0向量指定位置的值

1
2
z = np.zeros((3,4))
z
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
1
2
3
4
z[2,3] = 1
z[1,1] = 2

z
array([[0., 0., 0., 0.],
       [0., 2., 0., 0.],
       [0., 0., 0., 1.]])

找到非0的值

1
np.nonzero(z)
(array([1, 2]), array([1, 3]))
1
2


创建全1向量、数组

1
np.ones(6)
array([1., 1., 1., 1., 1., 1.])
1
np.ones((3,2))
array([[1., 1.],
       [1., 1.],
       [1., 1.]])
1
np.ones([2,3,2])
array([[[1., 1.],
        [1., 1.],
        [1., 1.]],

       [[1., 1.],
        [1., 1.],
        [1., 1.]]])

创建单位矩阵-eye

1
np.eye(4)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
1
np.eye(4,dtype=int)
array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]])
1
2


自定义数据类型

1
np.ones([2,3])  # 默认是浮点数
array([[1., 1., 1.],
       [1., 1., 1.]])
1
np.ones([2,3],dtype=int)  # 指定为int类型
array([[1, 1, 1],
       [1, 1, 1]])

列表转数组

1
2
3
lst = [1,2,3,4]

np.array(lst)
array([1, 2, 3, 4])
1
2
3
4
# 指定数组类型

lst = [1,2,3,4]
np.array(lst, dtype=float)
array([1., 2., 3., 4.])

嵌套列表转数组

1
2
3
lst1 = [[1,2,3],[4,5,6]]

np.array(lst1)
array([[1, 2, 3],
       [4, 5, 6]])
1
2
3
4
5
# 指定数据类型

lst1 = [[1,2,3],[4,5,6]]

np.array(lst1, dtype=float)
array([[1., 2., 3.],
       [4., 5., 6.]])

元组转数组

1
2
t1 = (9,8,7)
np.array(t1)
array([9, 8, 7])

嵌套元组转数组

1
2
t2 = ((9,8,7),(6,5,4))
np.array(t2)
array([[9, 8, 7],
       [6, 5, 4]])

列表和元组混合

1
2
3
lt = [(1,2,3),(7,8,9)]

np.array(lt)
array([[1, 2, 3],
       [7, 8, 9]])

迭代器转数组

1
2
3
range_number = range(3,8)

np.array(range_number)
array([3, 4, 5, 6, 7])
1
2
3
4
# 指定类型

range_number = range(3, 8)
np.array(range_number, dtype=float)
array([3., 4., 5., 6., 7.])

特殊矩阵1

边界值为1,其他为0

1
2
b = np.ones([6,6])
b
array([[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., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1.]])
1
2
3
b[1:-1,1:-1] = 0

b
array([[1., 1., 1., 1., 1., 1.],
       [1., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 1.],
       [1., 1., 1., 1., 1., 1.]])

特殊矩阵2

用0填充矩阵的边界

1
2
c = np.ones((6,6))
c
array([[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., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1.]])
1
np.pad(c,pad_width=1,mode="constant",constant_values=0)
array([[0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.]])

特殊矩阵3

6*6的矩阵,对角线下方的值为1,2,3,4,5

1
np.diag(1 + np.arange(5), k=-1)
array([[0, 0, 0, 0, 0, 0],
       [1, 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]])

np.arange函数

numpy 包中的使用 arange 函数创建数值范围并返回 ndarray 对象,函数使用方法为:

1
numpy.arange(start, stop, step, dtype)
  • start:起始值,默认为0
  • stop:终止值,不包含
  • step:步长,默认为1
  • dtype:返回数组的数据类型
1
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1
2
# 指定步长
np.arange(0,10,2)
array([0, 2, 4, 6, 8])
1
2
# 指定类型
np.arange(0,10,2,dtype=float)
array([0., 2., 4., 6., 8.])

创建随机数组

1
np.random.random((2,3,2))
array([[[0.56045087, 0.15566786],
        [0.34963774, 0.51837142],
        [0.68895046, 0.04980068]],

       [[0.98352437, 0.47189043],
        [0.30430488, 0.49057744],
        [0.20020709, 0.90466043]]])

Pandas数据转数组

1
2
3
4
import pandas as pd
s = pd.Series([1,2,3,4])

np.array(s)
array([1, 2, 3, 4])
1
2
3
d = pd.DataFrame([[1,2,3,4],[9,8,7,6]])

np.array(d)
array([[1, 2, 3, 4],
       [9, 8, 7, 6]])

反转数组

1
2
ten = np.arange(10)
ten
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1
ten[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

reshape函数

主要是用来改变数组的形状

1
2
arr = np.arange(16)
arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
1
arr.shape
(16,)
1
arr.reshape((4,4))
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
1
arr.reshape((2,8))
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15]])
1
arr.reshape((8,2))
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11],
       [12, 13],
       [14, 15]])
1
arr.reshape((1,16))
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15]])

我们需要特别注意-1的使用,numpy会自动生成相应的shape值

1
arr.reshape((8,-1))
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11],
       [12, 13],
       [14, 15]])
1
arr.reshape((-1,8))
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15]])

np.linspace函数

用于构建一个等差数列的数组,使用方法为:

1
2
3
4
5
6
7
8
np.linspace(
start, # 起始值
stop, # 终止值,如果endpoint为true,该值包含于数列中
num=50, # 生成的样本量,默认为50
endpoint=True, #是否包含末尾的值;默认为True
retstep=False, # 为True时,生成的数组中会显示间距,反之不
dtype=None # 数据类型
)
1
np.linspace(1,10,5)
array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ])

可以不包含末尾的数值:

1
np.linspace(1,10,5,endpoint=False)
array([1. , 2.8, 4.6, 6.4, 8.2])

全部是1的等差数列:

1
2
# 全部是1的等差数列
np.linspace(1,1,10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

加上restep参数,则会显示步长:

1
np.linspace(1,10,5,retstep=True)
(array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ]), 2.25)

np.logspace函数

主要是用于生成等比数列,使用方法为:

1
2
3
4
5
6
7
8
np.logspace(
start, # base ** start(指数)
stop, # base ** stop;如果endpoint为true,该值包含于数列中
num=50,
endpoint=True,
base=10.0, # 默认底数为10
dtype=None
)
1
np.logspace(1,5,num=10)
array([1.00000000e+01, 2.78255940e+01, 7.74263683e+01, 2.15443469e+02,
       5.99484250e+02, 1.66810054e+03, 4.64158883e+03, 1.29154967e+04,
       3.59381366e+04, 1.00000000e+05])

指定不同的底数;第一个数为2的0次方,为1:

1
np.logspace(0,8,num=10,base=2)
array([  1.        ,   1.85174942,   3.42897593,   6.34960421,
        11.75787594,  21.77264   ,  40.3174736 ,  74.65785853,
       138.24764658, 256.        ])

本文标题:numpy入门25个案例

发布时间:2022年01月28日 - 15:01

原始链接:http://www.renpeter.cn/2022/01/28/numpy%E5%85%A5%E9%97%A825%E4%B8%AA%E6%A1%88%E4%BE%8B.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea