Python学习笔记#6:numpy的使用

本文详细介绍了Python的numpy库,包括Numpy的概念、数据创建(如随机数、矩阵和副本)、数据转换、抽样、查找与筛选、修改、切片、计算及字符串操作等。通过实例演示了各种功能的使用,如随机数生成、数组去重、矩阵运算、线性方程组求解等,是学习和进阶numpy的实用教程。
摘要由CSDN通过智能技术生成

Numpy 概念

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

NumPy 的前身 Numeric 最早是由 Jim Hugunin 与其它协作者共同开发,2005 年,Travis Oliphant 在 Numeric 中结合了另一个同性质的程序库 Numarray 的特色,并加入了其它扩展而开发了 NumPy。NumPy 为开放源代码并且由许多协作者共同维护开发。

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

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

安装

pip install numpy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

导入包

import numpy as np

数据创建

1. 随机数

在numpy中,随机函数是一个很大的类,我们会经常使用随机函数初始化数据模型。

numpy.random.normal()

numpy.random.normal(loc=0,scale=1e-2,size=shape)

  • 参数loc(float):正态分布的均值,对应着这个分布的中心。loc=0说明这一个以Y轴为对称轴的正态分布,
  • 参数scale(float):正态分布的标准差,对应分布的宽度,scale越大,正态分布的曲线越矮胖,scale越小,曲线越高瘦。
  • 参数size(int 或者整数元组):输出的值赋在shape里,默认为None。
>>> np.random.normal(loc = 0, scale = 1, size = (2, 3))
array([[ 1.1253141 , -0.35899555,  1.2206081 ],
       [-1.33949555,  0.42837337, -0.12346315]])

numpy.random.rand()

numpy.random.rand(d0,d1,…,dn)

  • rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1
  • dn表格每个维度
  • 返回值为指定维度的array
>>> np.random.rand()
0.5741992576511386

>>> np.random.rand(4)
array([0.46293136, 0.67650639, 0.4965211 , 0.41193224])

>>> np.random.rand(3,4)
array([[0.5874274 , 0.74255452, 0.532332  , 0.20922426],
       [0.60061671, 0.57630622, 0.5325137 , 0.42761424],
       [0.75694314, 0.04299292, 0.86352272, 0.0804793 ]])
       
>>> np.random.rand(2,3,4)
array([[[0.4610567 , 0.62417219, 0.42756664, 0.96705009],
        [0.21420153, 0.78014512, 0.86688418, 0.49624361],
        [0.69868323, 0.05157287, 0.75464171, 0.3637569 ]],

       [[0.37934486, 0.2571473 , 0.56984629, 0.55191529],
        [0.98903933, 0.7968863 , 0.78496612, 0.49052729],
        [0.53844221, 0.91275757, 0.89148343, 0.78016788]]])

numpy.random.randn()

numpy.random.randn(d0,d1,…,dn)

  • randn函数返回一个或一组样本,具有标准正态分布。
  • dn表格每个维度
  • 返回值为指定维度的array
>>> numpy.random.randn()
0.8538518153827508

>>> numpy.random.randn(4)
array([ 0.18676025, -0.96798301, -1.55092326,  0.15827384])

>>> numpy.random.randn(3,4)
array([[ 0.46254689, -0.76170678,  0.83751158, -1.09405029],
       [ 0.23794122,  0.75556361, -0.98164949,  2.35257227],
       [ 0.52405716,  0.49690142,  0.02190174,  0.52204988]])
       
>>> numpy.random.randn(2,3,4)
array([[[ 1.53811637,  1.03104813, -0.07696404, -0.48572504],
        [ 0.15683787, -1.23805156,  2.75345817,  1.14479291],
        [ 1.77812274,  1.56753809,  0.6222866 , -0.04480746]],

       [[ 0.23821553, -2.22083911,  0.13508668, -1.3745254 ],
        [ 0.83610244,  0.08711017,  1.46519802, -0.65621799],

numpy.random.randint()

numpy.random.randint(low, high=None, size=None, dtype=‘I’)

  • 返回随机整数,范围区间为[low,high),包含low,不包含high
  • 参数:low为最小值,high为最大值,size为数组维度大小,dtype为数据类型,默认的数据类型是np.int
  • high没有填写时,默认生成随机数的范围是[0,low)
>>> np.random.randint(1, 5)
2

>>> np.random.randint(1, 5, size=(2))
array([3, 1])

>>> np.random.randint(1, 5, size=(4))
array([4, 4, 4, 2])

>>> np.random.randint(1, 5, size=(3,4))
array([[2, 1, 1, 3],
       [4, 3, 3, 2],
       [1, 1, 1, 4]])
       
>>> np.random.randint(1, 5, size=(2,3,4))
array([[[2, 1, 4, 1],
        [4, 3, 3, 3],
        [1, 3, 2, 2]],

       [[2, 2, 1, 2],
        [3, 2, 4, 3],
        [2, 2, 3, 3]]])

2. 矩阵

使用numpy,可以非常方便的对矩阵进行各种运算。
随机矩阵可以使用随机函数创建,而一些特殊矩阵也可以是用特定的函数

# 0 矩阵
>>> np.zeros([3, 3])
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

# 1 矩阵
>>> np.ones([3, 3])
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

# 单位矩阵
>>> np.identity(6)
array([[1., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0., 1.]])

# 对角矩阵
>>> np.diag(np.random.randint(1, 10, size = 6))
array([[6, 0, 0, 0, 0, 0],
       [0, 3, 0, 0, 0, 0],
       [0, 0, 5, 0, 0, 0],
       [0, 0, 0, 8, 0, 0],
       [0, 0, 0, 0, 7, 0],
       [0, 0, 0, 0, 0, 9]])


# 重塑
>>> np.arange(1, 10)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> np.arange(1, 10).reshape(3,-1)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

>>> np.arange(40).reshape(-1,10)
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]])

3. 创建副本

>>> ar = np.random.randint(1,100,size=(10,10))
>>> ar
array([[37, 62, 79, 43, 71, 92, 86, 59, 26, 22],
       [73, 97, 35, 70, 48, 82, 57, 42, 89, 79],
       [40, 29, 76, 64, 15, 26, 44, 57,  8, 89],
       [64, 40,  4, 32, 87, 96, 42, 32, 70, 33],
       [17, 56, 37, 55,  9, 50, 63,  6, 79, 97],
       [14, 97, 77, 49, 94,  4, 87, 22, 52, 13],
       [47, 25, 33, 59, 93, 96, 94, 77, 47, 12],
       [ 7, 93,  8, 94, 40, 38, 51,  7, 76, 70],
       [19, 69, 44, 87, 60, 18, 17, 26, 91, 53],
       [40, 59, 50, 53, 77, 58, 47, 17, 48, 38]])
>>> ab = ar.copy()
>>> ab
array([[37, 62, 79, 43, 71, 92, 86, 59, 26, 22],
       [73, 97, 35, 70, 48, 82, 57, 42, 89, 79],
       [40, 29, 76, 64, 15, 26, 44, 57,  8, 89],
       [64, 40,  4, 32, 87, 96, 42, 32, 70, 33],
       [17, 56, 37, 55,  9, 50, 63,  6, 79, 97],
       [14, 97, 77, 49, 94,  4, 87, 22, 52, 13],
       [47, 25, 33, 59, 93, 96, 94, 77, 47, 12],
       [ 7, 93,  8, 94, 40, 38, 51,  7, 76, 70],
       [19, 69, 44, 87, 60, 18, 17, 26, 91, 53],
       [40, 59, 50, 53, 77, 58, 47, 17, 48, 38]])

数据转换

1. list 转换 numpy

>>> a = [i for i in range(1, 15)]
>>> ar = np.array(a)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> ar
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

2. DataFrame 转换 numpy

>>> df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
>>> df
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
>>> df.values
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]], dtype=int64)

数据抽样

1. 随机抽样

numpy.random.choice()

numpy.random.choice(a, size=None, replace=True, p=None)

  • 从给定的一维数组中生成随机数
  • 参数:
  • a为一维数组类似数据或整数;size为数组维度;p为数组中的数据出现的概率
  • a为整数时,对应的一维数组为np.arange(a)
# 当replace为False时,生成的随机数不能有重复的数值
>>> np.random.choice(5, size = 5, replace=False)
array([0, 1, 3, 4, 2])

# 从[10,...,19]当中随机选取数字,再组成(3,4)的矩阵。
>>> np.random.choice([i for i in range(10, 20)], size=(3, 4))
array([[14, 13, 10, 14],
       [12, 16, 15, 11],
       [19, 10, 12, 19]])

# 从[10,...,19]当中随机选取数字,再组成(3,4)的矩阵,然后重塑为(6,2)的矩阵
>>> np.random.choice([i for i in range(10, 20)], size=(3, 4)).reshape(6,-1)
array([[11, 19],
       [11, 10],
       [17, 18],
       [14, 18],
       [18, 18],
       [17, 15]])

2. 概率抽样

# 按照概率筛选数值
>>> np.random.choice(5, size=3, p=[0.2, 0.3, 0.1, 0.2, 0.2])
array([3, 1, 1])

numpy.random.seed()

numpy.random.seed()

  • np.random.seed()的作用:使得随机数据可预测。
  • 当我们设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数
# seed = 0
>>> np.random.seed(0)
>>> np.random.rand(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

# seed 随机改变,随机结果改变
>>> np.random.rand(5)
array([0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152])

# seed = 22,结果与 seed = 0 不同
>>> np.random.seed(22)
>>> np.random.rand(5)
array([0.20846054, 0.48168106, 0.42053804, 0.859182  , 0.17116155])

# seed = 0,结果和第一次相同
>>> np.random.seed(0)
>>> np.random.rand(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

数据查找&筛选

1. 去重

np.unique()

>>> ar = np.random.randint(1, 100, size=100)
>>> ar
array([87, 89, 80, 91, 11,  8, 20, 84, 11, 68, 83,  5, 15, 16, 83, 72, 63,
       71, 11, 57, 95,  2, 59, 39, 96, 51, 81, 74, 40, 76, 57, 47, 39, 14,
       80, 57, 52, 82, 31, 33, 99, 85, 31, 47, 29, 27,  4, 37, 51, 80,  6,
        6,  6, 67, 58, 36, 25, 80, 89, 61, 14, 33, 30, 42, 16, 82, 98, 17,
       81, 86, 41, 42, 58,  5, 18, 66, 82, 91, 57, 94,  3, 73, 77, 44, 42,
       12, 16, 12,  7, 36, 23, 61, 47, 19, 24, 73, 72, 18, 64, 99])
>>> np.unique(ar)
array([ 2,  3,  4,  5,  6,  7,  8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 23,
       24, 25, 27, 29, 30, 31, 33, 36, 37, 39, 40, 41, 42, 44, 47, 51, 52,
       57, 58, 59, 61, 63, 64, 66, 67, 68, 71, 72, 73, 74, 76, 77, 80, 81,
       82, 83, 84, 85, 86, 87, 89, 91, 94, 95, 96, 98, 99])

2. 交集

np.intersect1d()

>>> ar1 = np.random.randint(1, 10, size=(10))
>>> ar2 = np.random.randint(1, 10, size=(10))
>>> ar1
array([4, 8, 4, 8, 8, 8, 1, 2, 7, 6])
>>> ar2
array([2, 5, 6, 5, 8, 5, 7, 3, 9, 7])
>>> np.intersect1d(ar1, ar2)
array([2, 6, 7, 8])

3. 位置查找

>>> ar = np.random.randint(1, 20, 10)
>>> index = np.array([1, 3, 5, 6, 8])
>>> ar
array([18, 14,  4, 10, 14, 15,  4,  8,  2, 18])
>>> index
array([1, 3, 5, 6, 8])
>>> np.take(ar, index)
array([14, 10, 15,  4,  2])

4. 条件筛选

>>> ar = np.random.randint(1, 100, size=100)
>>> ar
array([90,  4, 66, 97,  3, 84, 37, 86, 52, 45, 14, 83, 19,  9, 42, 13, 13,
       25, 94, 88, 30, 85, 96, 49, 62, 91, 65, 11, 40, 57, 88, 85, 99, 64,
       48, 44, 97, 95, 67, 61, 52, 34, 81, 34, 88, 55, 89, 76, 21, 66,  6,
       37, 44, 43, 38, 21, 44, 79,  5, 95, 24, 16, 46, 65,  9, 54,  4, 57,
       45, 40, 69, 59, 88, 12,  1, 44, 48, 53, 72, 55, 54, 97, 52, 82, 37,
       35, 12, 90, 59, 76, 60, 72, 50, 92, 28, 58, 41, 44, 92, 47])
  1. 40 < x < 80 40<x<80 40<x<80
  2. 偶 数 偶数
>>> ar[(ar > 40) & (ar < 80) & (ar % 2 == 0)]
array([66, 52, 42, 62, 64, 48, 44, 52, 76, 66, 44, 44, 46, 54, 44, 48, 72,
       54, 52, 76, 60, 72, 50, 58, 44])
  1. 奇 数 奇数
>>> ar[np.where(ar % 2 == 1)]
array([97,  3, 37, 45, 83, 19,  9, 13, 13, 25, 85, 49, 91, 65, 11, 57, 85,
       99, 97, 95, 67, 61, 81, 55, 89, 21, 37, 43, 21, 79,  5, 95, 65,  9,
       57, 45, 69, 59,  1, 53, 55, 97, 37, 35, 59, 41, 47])

数据修改

1. 删除

>>> ar1 = np.random.randint(1, 10, size=(10))
>>> ar2 = np.random.randint(1, 10, size=(10))
>>> ar1
array([6, 8, 5, 1, 4, 9, 3, 7, 5, 9])
>>> ar2
array([8, 5, 3, 6, 4, 4, 4, 3, 5, 6])
>>> np.setdiff1d(ar1, ar2)
array([1, 7, 9])

2. 向上、下取整

>>> ar3 = np.random.uniform(1, 10, size=10)
>>> ar3
array([2.1951119 , 1.89026099, 6.12395198, 8.4558816 , 5.04633365,
       6.99120923, 4.08988088, 3.32174106, 7.63073125, 3.41559037])
>>> np.ceil(ar3)
array([3., 2., 7., 9., 6., 7., 5., 4., 8., 4.])
>>> np.floor(ar3)
array([2., 1., 6., 8., 5., 6., 4., 3., 7., 3.])

3. 显示

# 矩阵
>>> m = np.arange(40).reshape(-1,8)
>>> m
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39]])

# 矩阵的形状
>>> m.shape
(5, 8)

# 矩阵大小
>>> m.size
40

# 第 2 行
>>> m[2]
array([16, 17, 18, 19, 20, 21, 22, 23])

# 第 2 列
>>> m[:, 2]
array([ 2, 10, 18, 26, 34])

# 获取矩阵的某个元素
>>> m[1, 2]
10

# 0 到 2(4-2 = 2)行
>>> m[:-2,:]
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23]])

# 间隔 -2 行,4,2,0
>>> m[::-2,:]
array([[32, 33, 34, 35, 36, 37, 38, 39],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [ 0,  1,  2,  3,  4,  5,  6,  7]])

# 间隔 -1 行
>>> m[::-1]
array([[32, 33, 34, 35, 36, 37, 38, 39],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [ 0,  1,  2,  3,  4,  5,  6,  7]])

# 间隔 -1 列
>>> m[:,::-1]
array([[ 7,  6,  5,  4,  3,  2,  1,  0],
       [15, 14, 13, 12, 11, 10,  9,  8],
       [23, 22, 21, 20, 19, 18, 17, 16],
       [31, 30, 29, 28, 27, 26, 25, 24],
       [39, 38, 37, 36, 35, 34, 33, 32]])

4. 分组

np.piecewise()
将大于等于7标记为1,或小于3的元素标记为-1,其余为0

>>> np.piecewise(ar, [ar < 3, ar >= 7], [-1, 1])
array([ 1,  1,  0,  0,  1,  1,  1,  0, -1,  0])
>>> ar = np.random.randint(1, 20, 10)
>>> ar
array([15, 14, 16, 19,  1, 19,  6, 16, 11, 17])
>>> np.piecewise(ar, [ar < 3, ar >= 7], [-1, 1])
array([ 1,  1,  1,  1, -1,  1,  0,  1,  1,  1])

5. 压缩

np.squeeze()
从数组的形状中删除单维度条目,即把shape中为1的维度去掉

>>> m = np.random.randint(1, 10, size=(3, 1))
>>> m
array([[3],
       [7],
       [2]])
>>> np.squeeze(m)
array([3, 7, 2])

6. 矩阵堆叠

>>> a = np.arange(9).reshape(3,-1)
>>> b = np.repeat(1,3)

>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

>>> b
array([1, 1, 1])

# 垂直堆叠
>>> np.vstack([a,b])
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [1, 1, 1]])

>>> np.vstack([b,a])
array([[1, 1, 1],
       [0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

# 水平堆叠
>>> c = np.repeat(1,6).reshape(3,-1)
>>> c
array([[1, 1],
       [1, 1],
       [1, 1]])

>>> np.hstack([a, c])
array([[0, 1, 2, 1, 1],
       [3, 4, 5, 1, 1],
       [6, 7, 8, 1, 1]])

>>> np.hstack([c, a])
array([[1, 1, 0, 1, 2],
       [1, 1, 3, 4, 5],
       [1, 1, 6, 7, 8]])

# 插入行
>>> np.insert(a, 1, values = b, axis = 0)
array([[0, 1, 2],
       [1, 1, 1],
       [3, 4, 5],
       [6, 7, 8]])

# 插入列
>>> np.insert(a, 1, values = b, axis = 1)
array([[0, 1, 1, 2],
       [3, 1, 4, 5],
       [6, 1, 7, 8]])

7. 矩阵旋转

# 矩阵转置
>>> m
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]])
       
>>> m.T
array([[ 0, 10, 20, 30],
       [ 1, 11, 21, 31],
       [ 2, 12, 22, 32],
       [ 3, 13, 23, 33],
       [ 4, 14, 24, 34],
       [ 5, 15, 25, 35],
       [ 6, 16, 26, 36],
       [ 7, 17, 27, 37],
       [ 8, 18, 28, 38],
       [ 9, 19, 29, 39]])

# 矩阵翻转(上下)
>>> m[::-1]
array([[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9]])

# 矩阵翻转(水平)
>>> m[:,::-1]
array([[ 9,  8,  7,  6,  5,  4,  3,  2,  1,  0],
       [19, 18, 17, 16, 15, 14, 13, 12, 11, 10],
       [29, 28, 27, 26, 25, 24, 23, 22, 21, 20],
       [39, 38, 37, 36, 35, 34, 33, 32, 31, 30]])

# 矩阵顺时针旋转 90°
>>> m[::-1].T
array([[30, 20, 10,  0],
       [31, 21, 11,  1],
       [32, 22, 12,  2],
       [33, 23, 13,  3],
       [34, 24, 14,  4],
       [35, 25, 15,  5],
       [36, 26, 16,  6],
       [37, 27, 17,  7],
       [38, 28, 18,  8],
       [39, 29, 19,  9]])

# 矩阵逆时针旋转 90°
>>> m.T[::-1]
array([[ 9, 19, 29, 39],
       [ 8, 18, 28, 38],
       [ 7, 17, 27, 37],
       [ 6, 16, 26, 36],
       [ 5, 15, 25, 35],
       [ 4, 14, 24, 34],
       [ 3, 13, 23, 33],
       [ 2, 12, 22, 32],
       [ 1, 11, 21, 31],
       [ 0, 10, 20, 30]])

数据切片

1. 矩阵切片

>>> m = np.arange(40).reshape(5,-1)
>>> m
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39]])

# 矩阵切片
>>> m[[2,3],:]
array([[16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31]])

# 2-3 行 + 2-5 列
>>> m[[2,3],:][:,[2,3,4,5]]
array([[18, 19, 20, 21],
       [26, 27, 28, 29]])

# 行列自由组合,由上到下分别是第4行,2行,2行,0行。
>>> m[[4,2,2,0],:]
array([[32, 33, 34, 35, 36, 37, 38, 39],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [ 0,  1,  2,  3,  4,  5,  6,  7]])

>>> m[[0,0,0,0],:]
array([[0, 1, 2, 3, 4, 5, 6, 7],
       [0, 1, 2, 3, 4, 5, 6, 7],
       [0, 1, 2, 3, 4, 5, 6, 7],
       [0, 1, 2, 3, 4, 5, 6, 7]])

# 上面矩阵当中的1、3、5、7列
>>> m[[0,0,0,0],:][:,[1,3,5,7]]
array([[1, 3, 5, 7],
       [1, 3, 5, 7],
       [1, 3, 5, 7],
       [1, 3, 5, 7]])

数据计算

1. 求余数

>>> a = 10
>>> a = 132454523
>>> b = 32
>>> np.mod(a, b)
27

2. svd分解

>>> m = np.random.randint(1, 10, size=(3, 3))
>>> np.linalg.svd(m)
(array([[-0.4014294 ,  0.04088355, -0.91497703],
       [-0.71853942, -0.63353588,  0.28693797],
       [-0.56793973,  0.7726324 ,  0.28369638]]), array([14.3287306 ,  4.94460329,  1.49612092]), array([[-0.38714846, -0.7580505 , -0.52485761],
       [ 0.12079223,  0.52264288, -0.84395122],
       [ 0.91407073, -0.39013313, -0.11077378]]))

3. 求解线性方程组

[ 1 2 3 2 − 1 0 3 − 1 − 1 ] ∗ [ a b c ]   = [ 9 8 3 ] \begin{bmatrix} 1 & 2 & 3 \\ 2 & -1 & 0 \\ 3 & -1 & -1 \\ \end{bmatrix} * \begin{bmatrix} a \\ b \\ c \\ \end{bmatrix} \ = \begin{bmatrix} 9\\ 8\\ 3\\ \end{bmatrix} 123211301abc =983
{   1 ∗ a + 2 ∗ b + 3 ∗ c = 0   2 ∗ a − 1 ∗ b + 0 ∗ c = 8   3 ∗ a − 1 ∗ b − 1 ∗ c = 3 \begin{cases} \ 1*a + 2*b + 3*c = 0 \\ \ 2*a - 1*b + 0*c = 8 \\ \ 3*a - 1*b - 1*c = 3 \\ \end{cases}  1a+2b+3c=0 2a1b+0c=8 3a1b1c=3

>>> A = np.array([[1, 2, 3], [2, -1, -1], [3, 0, -1]])
>>> B = np.array([9, 8, 3])
>>> np.linalg.solve(A, B)
array([ 3.5, -8.5,  7.5])

4. 矩阵运算

>>> a = np.arange(1,10).reshape(3,-1)
>>> p = np.arange(4, 1, -1)

>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

>>> p
array([4, 3, 2])

矩阵加法
[ 1 2 3 4 5 6 7 8 9 ] + [ 1 2 3 4 5 6 7 8 9 ]   = [ 1 + 1 2 + 2 3 + 3 4 + 4 5 + 5 6 + 6 7 + 7 8 + 8 9 + 9 ]   = [ 2 4 6 8 10 12 14 16 18 ] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} + \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \ = \begin{bmatrix} 1+1& 2+2& 3+3 \\ 4+4 & 5+5 & 6+6 \\ 7+7 & 8+8 & 9+9 \\ \end{bmatrix} \ = \begin{bmatrix} 2& 4& 6 \\ 8 & 10 & 12 \\ 14 & 16 & 18 \\ \end{bmatrix} 147258369+147258369 =1+14+47+72+25+58+83+36+69+9 =28144101661218

# 矩阵相加
>>> a + a
array([[ 2,  4,  6],
       [ 8, 10, 12],
       [14, 16, 18]])

矩阵与向量相加
[ 1 2 3 4 5 6 7 8 9 ] + [ 4 3 2 ]   = [ 1 + 4 2 + 3 3 + 2 4 + 4 5 + 3 6 + 2 7 + 4 8 + 3 9 + 2 ]   = [ 5 5 5 8 8 8 11 11 11 ] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} + \begin{bmatrix} 4 & 3 & 2 \\ \end{bmatrix} \ = \begin{bmatrix} 1+4 & 2+3 & 3+2 \\ 4+4 & 5+3 & 6+2 \\ 7+4 & 8+3 & 9+2 \\ \end{bmatrix} \ = \begin{bmatrix} 5 & 5 & 5 \\ 8 & 8 & 8 \\ 11 & 11 & 11 \\ \end{bmatrix} 147258369+[432] =1+44+47+42+35+38+33+26+29+2 =581158115811

# 矩阵与向量相加
>>> a + p
array([[ 5,  5,  5],
       [ 8,  8,  8],
       [11, 11, 11]])

矩阵乘法
[ 1 2 3 4 5 6 7 8 9 ] ∗ 10   = [ 10 20 30 40 50 60 70 80 90 ] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} * 10 \ = \begin{bmatrix} 10 & 20 & 30 \\ 40 & 50 & 60 \\ 70 & 80 & 90 \\ \end{bmatrix} 14725836910 =104070205080306090

# 矩阵乘法
>>> a * 10
array([[10, 20, 30],
       [40, 50, 60],
       [70, 80, 90]])

矩阵乘法—点乘
[ 1 2 3 4 5 6 7 8 9 ] ⋅ [ 1 2 3 4 5 6 7 8 9 ]   = [ 1 ∗ 1 2 ∗ 2 3 ∗ 3 4 ∗ 4 5 ∗ 5 6 ∗ 6 7 ∗ 7 8 ∗ 8 9 ∗ 9 ]   = [ 1 4 9 16 25 36 49 64 81 ] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \cdot \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \ = \begin{bmatrix} 1*1& 2*2& 3*3 \\ 4*4 & 5*5 & 6*6 \\ 7*7 & 8*8 & 9*9 \\ \end{bmatrix} \ = \begin{bmatrix} 1& 4 & 9 \\ 16 & 25 & 36 \\ 49 & 64 & 81 \\ \end{bmatrix} 147258369147258369 =114477225588336699 =116494256493681

# 矩阵乘法(逐元素相乘)
>>> a * a
array([[ 1,  4,  9],
       [16, 25, 36],
       [49, 64, 81]])

矩阵乘法—叉乘
[ 1 2 3 4 5 6 7 8 9 ] × [ 1 2 3 4 5 6 7 8 9 ]   = [ 1 ∗ 1 + 2 ∗ 4 + 3 ∗ 7 … … ⋮ ⋱ ⋮ ⋮ … 150 ]   = [ 30 36 42 66 81 96 102 126 150 ] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \times \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \ = \begin{bmatrix} 1*1+2*4+3*7 & \dots & \dots \\ \vdots & \ddots & \vdots \\ \vdots & \dots & 150 \\ \end{bmatrix} \ = \begin{bmatrix} 30 & 36 & 42 \\ 66 & 81 & 96 \\ 102 & 126 & 150 \\ \end{bmatrix} 147258369×147258369 =11+24+37150 =306610236811264296150

# 矩阵乘法(标准定义)
>>> a.dot(a)
array([[ 30,  36,  42],
       [ 66,  81,  96],
       [102, 126, 150]])

矩阵与向量—点乘
[ 1 2 3 4 5 6 7 8 9 ] ⋅ [ 4 3 2 ]   = [ 1 ∗ 4 2 ∗ 3 3 ∗ 2 4 ∗ 4 5 ∗ 3 6 ∗ 2 7 ∗ 4 8 ∗ 3 9 ∗ 2 ]   = [ 4 6 6 16 15 12 28 24 18 ] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \cdot \begin{bmatrix} 4 & 3 & 2 \\ \end{bmatrix} \ = \begin{bmatrix} 1*4 & 2*3 & 3*2 \\ 4*4 & 5*3 & 6*2 \\ 7*4 & 8*3 & 9*2 \\ \end{bmatrix} \ = \begin{bmatrix} 4 & 6 & 6 \\ 16 & 15 & 12 \\ 28 & 24 & 18 \\ \end{bmatrix} 147258369[432] =144474235383326292 =416286152461218

# 矩阵与向量点乘
>>> a * p
array([[ 4,  6,  6],
       [16, 15, 12],
       [28, 24, 18]])

矩阵与向量—叉乘
[ 1 2 3 4 5 6 7 8 9 ] × [ 4 3 2 ]   = [ 4 ∗ 1 + 2 ∗ 3 + 3 ∗ 2 4 ∗ 4 + 5 ∗ 3 + 6 ∗ 2 7 ∗ 4 + 8 ∗ 3 + 9 ∗ 2 ]   = [ 16 43 70 ] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \times \begin{bmatrix} 4 & 3 & 2\\ \end{bmatrix} \ = \begin{bmatrix} 4*1 + 2*3 + 3*2 & 4*4 + 5*3 + 6*2 & 7*4 + 8*3 + 9*2 \\ \end{bmatrix} \ = \begin{bmatrix} 16 & 43 & 70\\ \end{bmatrix} 147258369×[432] =[41+23+3244+53+6274+83+92] =[164370]

# 矩阵与向量相乘
>>> a.dot(p)
array([16, 43, 70])

[ 4 3 2 ] × [ 1 2 3 4 5 6 7 8 9 ]   = [ 4 ∗ 1 + 3 ∗ 4 + 2 ∗ 7 … … ]   = [ 30 39 48 ] \begin{bmatrix} 4 & 3 & 2 \\ \end{bmatrix} \times \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \ = \begin{bmatrix} 4*1 + 3*4 + 2*7 & \dots & \dots \\ \end{bmatrix} \ = \begin{bmatrix} 30 & 39 & 48 \\ \end{bmatrix} [432]×147258369 =[41+34+27] =[303948]

>>> p.dot(a)
array([30, 39, 48])

字符串操作

1. 组合

>>> str1 = "I love"
>>> str2 = " Python"
>>> np.char.add(str1, str2)
array('I love Python', dtype='<U13')

2. 首字母大写

>>> np.char.title(str1)
array('I Love', dtype='<U6')

数据分析

>>> ar1 = np.random.randint(1, 10, size=(10))
>>> ar2 = np.random.randint(1, 10, size=(10))
>>> ar1
array([8, 3, 7, 3, 7, 5, 2, 2, 3, 1])
>>> ar2
array([8, 1, 9, 3, 1, 8, 8, 1, 6, 2])

平均数

>>> np.mean(ar1)
4.1

中位数

>>> np.median(ar1)
3.0

方差

>>> np.var(ar1)
5.49

标准差

>>> np.std(ar1)
2.3430749027719964

相关性矩阵

>>> np.cov(ar1, ar2)
array([[ 6.1       ,  3.47777778],
       [ 3.47777778, 11.56666667]])

协方差矩阵

>>> np.corrcoef(ar1, ar2)
array([[1.        , 0.41403094],
       [0.41403094, 1.        ]])

常用练习题:

>>> a = np.array([1,2,3,2,3,4,3,4,5,6])
>>> b = np.array([7,2,10,2,7,4,9,4,9,8])

>>> a
array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])

>>> b
array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])

# 数组中共同的项
>>> np.intersect1d(a,b)
array([2, 4])

# 从一个数组中移除与另一个数组重复的项
>>> np.setdiff1d(a,b)
array([1, 3, 5, 6])
>>> np.setdiff1d(b,a)
array([7, 8, 9, 10])

# 求交集
>>> np.where(a == b)
(array([1, 3, 5, 7], dtype=int32),)
>>> a[np.where(a == b)]
array([2, 2, 4, 4])
>>> a[a==b]
array([2, 2, 4, 4])

# 求并集
>>> c = np.hstack([a,b])
array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6, 7, 2, 10, 2, 7, 4, 9, 4, 9, 8])

# 多条件筛选数组
>>> c[np.where((c > 3) & (c < 8))]
array([4, 4, 5, 6, 7, 7, 4, 4])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值