1 Star 0 Fork 0

赖lai / python_note

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

1.矩阵生成 # 生成一个3行5列值为566的矩阵 print(np.full((3,5),566))

2.随机种子的使用 # 随机种子的使用:使用随机种子,然后随机创造数据,之后再次调用随机种子,再次随机的数据会和之前一次相同 np.random.seed(666) print(np.random.randint(0,10,size = 10)) np.random.seed(666) print(np.random.randint(0,10,size = 10))

3.随机数 # 符合正太分布的随机数:符合均值为0,方差为1的随机数 print(np.random.normal())

# np.random.normal(loc,scale,size) loc:均值 scale:方差 size:大小
# 生成一个均值为10,方差为100的随机数
print(np.random.normal(10,100,(3,5)))

4.矩阵的基本属性 x = np.arange(15).reshape(3,5) 4.1 维度 ndim 查看x的维度 x.ndim 4.2 shape 返回元组 x.shape 4.3 元素个数 size x.size

5.切片 x = np.arange(10) X = np.arange(15).reshape(3,5)

[In]: x
[Out]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
[In]: X
[Out]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

5.1 x[-1] 取x中最后一个元素
    [Out]: 9

5.2 X[-1][-1] 或者 X[-1,-1] 取X中最后一行最后一列的元素
    [Out]: 14

5.3 X[0:3] 取前3行的元素
    [Out]:
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14]])

5.4 x[::2]  取x的元素,间距为2
    [Out]: array([0, 2, 4, 6, 8])

5.5 x[::-1] 倒序x中的元素顺序
    [Out]: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

5.6 X[:2,:3] 取X中前2行前3列的元素
    ⚠️千万注意: X[:2,:3] != X[:2][:3]
    [Out]:
    array([[0, 1, 2],
           [5, 6, 7]])

5.7 X[:2,::2] 取前两行,每行元素间隔为2的元素
    [Out]:
    array([[0, 2, 4],
           [5, 7, 9]])

5.8 X[::-1,::-1]  矩阵的反转
    [Out]:
    array([[14, 13, 12, 11, 10],
           [ 9,  8,  7,  6,  5],
           [ 4,  3,  2,  1,  0]])

5.9 X[0]  或者 X[0,:] 矩阵降维
    [Out]: array([0, 1, 2, 3, 4])

5.10 ⚠️numpy中对自矩阵进行操作,会影响父矩阵的数据
    [In]: subX = X[:2,:3]
    [In]: subX[0,0] = 10
    [In]: subX
    [Out]:
    array([[10,  1,  2],
           [ 5,  6,  7]])
    [In]: X
    [Out]
    array([[10,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14]])
5.11 创建一个与父矩阵无关联的自矩阵,这样创建的子矩阵不会影响父矩阵
    [In]: subX = X[:2,:3].copy()
    [In]: subX[0,0] = 10
    [In]: subX
    [Out]:
    array([[10,  1,  2],
           [ 5,  6,  7]])
    [In]: X
    [Out]
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],

5.12 reshape 重塑元素的维度(⚠️此操作不改变原素组的数据)
    [In]: x.shape
    [Out]: (10,)
    [In]: x.reshape(2,5)
    [Out]:
    array([[0, 1, 2, 3, 4],
           [5, 6, 7, 8, 9]])

    ⚠️ 只规定元素的行数,不指定元素个数
    [In]: x.reshape(10,-1)
    [Out]:
    array([[0],
           [1],
           [2],
           [3],
           [4],
           [5],
           [6],
           [7],
           [8],
           [9]])
    [In]: x.reshape(-1,10)
    [Out]: array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

6 数据的合并与分割 x = np.array([1,2,3]) y = np.array([3,2,1]) z = np.array([0,0,0])

a = np.array([[1,2,3],[4,5,6]])
b = np.full((2,2),100)

6.1 将x和y合并(concatenate只能处理维度相同的数据)
    [In]: np.concatenate([x,y])
    [Out]: array([1, 2, 3, 3, 2, 1])
    [In]: np.concatenate([x,y,z])
    [Out]: array([1, 2, 3, 3, 2, 1, 0, 0, 0])


    ⚠️ np.concatenate([a,a], axis=)  axis是轴,默认为0,沿着第0个维度进行拼接
    [In]: np.concatenate([a,a])
    [Out]:
    array([[1, 2, 3],
           [4, 5, 6],
           [1, 2, 3],
           [4, 5, 6]])
    [In]: np.concatenate([a,a], axis=1)
    [Out]:
    array([[1, 2, 3, 1, 2, 3],
           [4, 5, 6, 4, 5, 6]])

6.2 将a和x进行垂直方向的合并(vstack可以在垂直方法进行叠加,即使两个数组维度不同)

    [In]: np.vstack([a,z])
    [Out]:
    array([[1, 2, 3],
           [4, 5, 6],
           [0, 0, 0]])

    [In]: np.hstack([a,b])
    [Out]:
    array([[  1,   2,   3, 100, 100],
           [  4,   5,   6, 100, 100]])


6.3 分割操作(split)
    [In]: x = np.arange(10)
    [In]: x
    [Out]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    np.split(x,[3,7]) ⚠️第一个参数:要分个的数组 第二个参数:分割点(数组)第三个参数:axis 默认为0,沿着第0个维度吧进行分割
    [In]: x1, x2, x3 = np.split(x,[3,7])
    [In]: x1
    [Out]: array([0, 1, 2])
    [In]: x2
    [Out]: array([3, 4, 5, 6])
    [In]: x3
    [Out]: array([7, 8, 9])

    同样适用于多维数组

6.4 vsplit和hsplit
    纵向切割和横向切割
    [In]: data = np.arange(16).reshape(4,4)
    [In]: date
    [Out]:
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
    [In]: x, y = np.hsplit(data, [-1])
    [In]: x
    [Out]:
    array([[ 0,  1,  2],
           [ 4,  5,  6],
           [ 8,  9, 10],
           [12, 13, 14]])
    [In]: y
    [Out]:
    array([[ 3],
           [ 7],
           [11],
           [15]])

7.矩阵的运算 A = np.arange(4).reshape(2,2) B = np.full((2,2),10)

    v = np.array([1,2])
7.1 矩阵乘法(dot)
    [In]: A.dot(B)
    [Out]:
    array([[10, 10],
           [50, 50]])

7.2 矩阵的转置
    [In]: A.T
    [Out]:
    array([[0, 2],
           [1, 3]])

7.3 矩阵和向量的运算

    [In]: v+A
    [Out]:
    array([[1, 3],
           [3, 5]])

    将向量v变成和A矩阵一样维度的矩阵,在和A矩阵进行运算
    [In]: np.tile(v, (2,1)) #将向量v 行方向堆叠2次,列方向堆叠1次
    [Out]:
    array([[1, 2],
           [1, 2]])
    [In]: np.tile(v, (2,1)) + A
    [Out]:
    array([[1, 3],
           [3, 5]])

7.4 矩阵的逆
    7.4.1 方阵的逆
        [In]: np.linalg.inv(A)
        [Out]:
        aarray([[-1.5,  0.5],
                [ 1. ,  0. ]])

    7.4.2 矩阵的伪逆
        [In]: x = np.arange(16).reshape(2,8)
        [In]: x
        [Out]:
        array([[ 0,  1,  2,  3,  4,  5,  6,  7],
               [ 8,  9, 10, 11, 12, 13, 14, 15]])
        [In]: np.linalg.pinv(x)
        [Out]:
        array([[-1.35416667e-01,  5.20833333e-02],
               [-1.01190476e-01,  4.16666667e-02],
               [-6.69642857e-02,  3.12500000e-02],
               [-3.27380952e-02,  2.08333333e-02],
               [ 1.48809524e-03,  1.04166667e-02],
               [ 3.57142857e-02, -1.04083409e-17],
               [ 6.99404762e-02, -1.04166667e-02],
               [ 1.04166667e-01, -2.08333333e-02]])

8.聚合操作 L = np.random.random(100) 8.1 求和 L.sum() 8.2 求最大值 L.max() 8.3 求最小值 L.min()

8.4  求矩阵行或者列的和
    X = np.arange(16).reshape(4,-1)
    X:
        array([ 0, 1, 2, 3],
              [ 4, 5, 6, 7],
              [ 8, 9,10,11],
              [12,13,14,15])
    按照列方向求和:把每列加起来
        np.sum(X,axis=0)就相当于是
            array([ 0 + 4 + 8 + 12, 1 + 5 + 9 + 13, 2 + 6 + 10 + 14, 3 + 7 + 11 + 15])
        就是:
            array([24, 28, 32, 36])
    按照行方向求和:把每行加起来
        np.sum(X,axis=1)就相当于是
            array([0 + 1 + 2 +3, 4 + 5 + 6 + 7, 8 + 9 + 10 + 11, 12 + 13 + 14 + 15])
        就是:
            array([ 6, 22, 38, 54])

8.5 求矩阵中各个元素的乘积
    np.prod(L)

8.6 求平均值
    np.mean(L)

8.7 求中位数
    np.median(X)

8.8 求百分位
    [In]: np.percentile(L, q = 50)
    [Out]: 0.4472645432100124
    表示在L中,有50%的数都小于0.4472645432100124

    在分析一个样本数据的时候,大体了解数据分布是,只需要看5个点即可
        for percent in [0, 25, 50, 75, 100] :
            print(np.percentile(L, q = percent))
    输出结果就会显示初这批样本的大致分布情况


8.9 求方差
    np.var(L)

8.10 求标准差
    np.std(L)

空文件

简介

python笔记 展开 收起
Python
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/reborn-lai/python_note.git
git@gitee.com:reborn-lai/python_note.git
reborn-lai
python_note
python_note
master

搜索帮助