• 01_Numpy基本使用


    1.Numpy读取txt/csv文件

    读取数据

    import numpy as np
    # numpy打开本地txt文件
    world_alcohol = np.genfromtxt("D:\数据分析\01_Numpy\numpy\world_alcohol.txt", delimiter=",", dtype=str, skip_header=1)
    print(world_alcohol)
    # 第一个参数为文件存放路径
    # delimiter  分隔符
    # dtype 以哪种数据类型打开
    # skip_header=1  跳过头信息,不打印第一行

    结果输出:

    [['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
     ['1986' 'Americas' 'Uruguay' 'Other' '0.5']
     ['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62']
     ...
     ['1987' 'Africa' 'Malawi' 'Other' '0.75']
     ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
     ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
    # numpy打开数据后,以矩阵的形式展示,索引值从0开始
    vector = world_alcohol[2, 4] # 取第二行第四列的值
    print(vector)

    结果输出:

    1.62

    帮助文档

    #  打印帮助文档
    print(help(np.genfromtxt))

    使用help命令查看帮助文档

    结果输出:

    Help on function genfromtxt in module numpy.lib.npyio:
    
    genfromtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None, encoding='bytes')
        Load data from a text file, with missing values handled as specified.
        
        Each line past the first `skip_header` lines is split at the `delimiter`
        character, and characters following the `comments` character are discarded.
        
        Parameters
        ----------
    ...

    2.Numpy的矩阵操作

    构造矩阵

    # 导入
    import numpy as np
    
    vector = np.array([5, 10, 15, 20])  # 构造一个numpy的向量
    
    matrix = np.array([[5, 10, 15], [20, 25, 30]])  # 构造一个numpy的矩阵
    print(vector)
    print(matrix)

    结果输出:

    [ 5 10 15 20]
    [[ 5 10 15]
     [20 25 30]]
    vector = np.array([5, 10, 15, 20])
    print("矩阵维度:", vector.shape)  # shape属性打印矩阵(向量)的维度
    print("矩阵数据类型:", vector.dtype)  # 打印矩阵中的数据类型

    结果输出:

    矩阵维度: (4,)
    矩阵数据类型: int32

    类型转换

    # numpy 要求array中是数据为同一类型,且自动实现类型转换
    
    vector = np.array([5.0, 10, 15])
    print(vector)
    print(vector.dtype)

    结果输出:

    [ 5. 10. 15.]
    float64

    切片操作

    vector = np.array([5, 10, 15, 20])
    # 切片操作,取前三个元素
    print(vector[0:3])

    结果输出:

    [ 5 10 15]
    matrix = np.array([
        [1,2,3],
        [4,5,6],
        [7,8,9]
    ])
    # 矩阵取元素
    # 取矩阵第二列,所有值
    vector = matrix[:, 1]  # 冒号表示所有样本,1表述列数(前面表示行,后面表示列)
    print(vector)
    print("---------")
    print(matrix[:, 0:2]) # 取前两列所有元素
    print("---------")
    print(matrix[:2, :2]) # 取前两列的前两行元素

    结果输出:

    [2 5 8]
    ---------
    [[1 2]
     [4 5]
     [7 8]]
    ---------
    [[1 2]
     [4 5]]

    逻辑运算

    vector = np.array([5, 10, 15, 20])
    equal = (vector == 10)  # 判断array中的元素是否为10, 对array的每一个元素进行判断
    print(equal)
    
    print(vector[equal])  # 从向量中取返回True的值
    
    and_ = (vector == 10) & (vector == 5)  # 逻辑运算 与 其他同理
    print(and_)

    结果输出:

    [False  True False False]
    [10]
    [False False False False]
    matrix = np.array([
        [1,2,3],
        [4,5,6],
        [7,8,9]
    ])
    matrix == 5  # 矩阵操作同理,判断array中所有元素是否为5

    结果输出:

    array([[False, False, False],
           [False,  True, False],
           [False, False, False]])
    # array数据类型转换换
    vector = np.array(['1', '2', '3'])
    print(vector.dtype)  # 打印数据类型 str类型显示未U1
    vector = vector.astype(float)  # 使用astype()实现类型转换,转换为float类型
    print(vector.dtype)

    结果输出:

    <U1
    float64

    一般函数

    vector = np.array([5, 10, 15, 20])
    # 求最小值
    print(vector.min())
    # 求最大值
    print(vector.max())

    结果输出:

    5
    20
    matrix = np.array([
        [1,2,3],
        [4,5,6],
        [7,8,9]
    ])
    # 按行求和
    matrix.sum(axis=1)  # axis=0表示按列求和

    结果输出:

    array([ 6, 15, 24])

    3.Numpy矩阵属性

    常用属性

    import numpy as np
    
    print(np.arange(15))  # 生成一个列表(索引)
    print('--'*10)
    a = np.arange(15).reshape(3, 5)  # reshape函数修改矩阵为3行5列
    print(a)

    结果输出:

    [ 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]]
    print(a.shape)  # shape属性打印矩阵的行列数
    print(a.ndim)  # ndim查看当前矩阵的维度
    print(a.dtype)  # 查看矩阵数据类型
    print(a.size)  # 查看矩阵中的元素

    结果输出:

    (3, 5)
    2
    int32
    15

    4.Numpy矩阵的初始化

    zeros与ones

    np.zeros((3, 4))  # 初始化一个3行4列的0矩阵,参数以元组的形式传入,默认为float类型

    结果输出:

    array([[0., 0., 0., 0.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.]])
    np.ones((2, 3, 4),dtype=np.int32) # 初始化一个三维1矩阵, 指定数据类型为int,元组长度决定了矩阵维度

    结果输出:

    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]]])
    np.arange(5, 10, 0.3)  # 创建一个等差数列,公差为0.3

    结果输出:

    array([5. , 5.3, 5.6, 5.9, 6.2, 6.5, 6.8, 7.1, 7.4, 7.7, 8. , 8.3, 8.6,
           8.9, 9.2, 9.5, 9.8])

    随机矩阵

    np.random.random((2, 3))  # 随机矩阵,创建一个2行3列的随机矩阵,默认为0到1之间

    结果输出:

    array([[0.10536991, 0.49359947, 0.56369024],
           [0.39357893, 0.78362851, 0.75576749]])
    from numpy import pi  # 数学中的π
    np.linspace(0, 2*pi, 100)  # 从0到2π中平均生成100个数

    结果输出:

    array([0.        , 0.06346652, 0.12693304, 0.19039955, 0.25386607,
           0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866,
           0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126,
           0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385,
           1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644,
           1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903,
           1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162,
           2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421,
           2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 ,
           2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939,
           3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199,
    ...

    基础运算

    a = np.array([20, 30, 40])
    b = np.array([2, 3, 4])
    print("a:",a)
    print("b:",b)
    c = a - b
    print("c:",c)  # numpy是对应位置相减
    c = c - 1
    print("c-1:",c)
    print("b^2:", b ** 2)  # 平方
    print("a<30:",a < 30)  # 判断每个元素是否小于30

    结果输出:

    a: [20 30 40]
    b: [2 3 4]
    c: [18 27 36]
    c-1: [17 26 35]
    b^2: [ 4  9 16]
    a<30: [ True False False]
    
    a = np.array([
        [1, 1],
        [0, 1]
    ])
    b = np.array([
        [2, 0],
        [3, 4]
    ])
    print( a * b) # 点积相乘,对应位置相乘
    print("------")
    print(a.dot(b))  # 矩阵相乘
    print("------")
    print(np.dot(a, b))  # 同样的矩阵相乘

    结果输出:

    [[2 0]
     [0 4]]
    ------
    [[5 4]
     [3 4]]
    ------
    [[5 4]
     [3 4]]

    5.Numpy中的常用函数

    平方与幂

    B = np.arange(3)
    print(np.exp(B))  # e的幂运算
    print(np.sqrt(B))  # 开平方

    结果输出:

    [1.         2.71828183 7.3890561 ]
    [0.         1.         1.41421356]

    取整函数

    a = np.floor(10 * np.random.random((3, 4)))  # floor 向下取整
    print(a)

    结果输出:

    [[8. 0. 7. 6.]
     [3. 1. 3. 1.]
     [3. 0. 1. 4.]]

    拆分矩阵

    aa = a.ravel()  # ravel 拆分矩阵,将矩阵拆分为向量
    print(aa)

    结果输出:

    [8. 0. 7. 6. 3. 1. 3. 1. 3. 0. 1. 4.]

    reshape函数

    aa.shape = (6, 2)  # 改变矩阵样式, 效果和reshape((6, 2))相同
    aa

    结果输出:

    array([[8., 0.],
           [7., 6.],
           [3., 1.],
           [3., 1.],
           [3., 0.],
           [1., 4.]])

    矩阵转置

    aa.T  # 转置

    结果输出:

    array([[8., 7., 3., 3., 3., 1.],
           [0., 6., 1., 1., 0., 4.]])

    矩阵拼接

    a = np.floor(10 * np.random.random((2, 3)))
    b = np.floor(10 * np.random.random((2, 3)))
    
    print(a)
    print(b)
    print('============')
    print(np.vstack((a, b)))  # 按行拼接
    print('============')
    print(np.hstack((a, b)))  # 按列拼接

    结果输出:

    [[5. 6. 7.]
     [0. 1. 7.]]
    [[6. 2. 3.]
     [5. 4. 7.]]
    ============
    [[5. 6. 7.]
     [0. 1. 7.]
     [6. 2. 3.]
     [5. 4. 7.]]
    ============
    [[5. 6. 7. 6. 2. 3.]
     [0. 1. 7. 5. 4. 7.]]
    a = np.floor(10 * np.random.random((2, 12)))
    print(a)
    print("===="*5)
    print(np.hsplit(a, 3))  # 将a(2行12列)平均切分为3份,按行切分
    print("===="*5)
    print(np.hsplit(a, (3, 4)))  # 在角标为3的地方切分开一次,角标为4的地方再切分开一次,参数为元组
    print("===="*5)
    a = a.T
    print(np.vsplit(a, 3))  # 同理操作,按列切分

    结果输出:

    [[6. 0. 3. 8. 4. 2. 6. 7. 2. 9. 7. 9.]
     [2. 3. 5. 5. 3. 2. 5. 6. 8. 2. 6. 4.]]
    ====================
    [array([[6., 0., 3., 8.],
           [2., 3., 5., 5.]]), array([[4., 2., 6., 7.],
           [3., 2., 5., 6.]]), array([[2., 9., 7., 9.],
           [8., 2., 6., 4.]])]
    ====================
    [array([[6., 0., 3.],
           [2., 3., 5.]]), array([[8.],
           [5.]]), array([[4., 2., 6., 7., 2., 9., 7., 9.],
           [3., 2., 5., 6., 8., 2., 6., 4.]])]
    ====================
    [array([[6., 2.],
           [0., 3.],
           [3., 5.],
           [8., 5.]]), array([[4., 3.],
           [2., 2.],
           [6., 5.],
           [7., 6.]]), array([[2., 8.],
           [9., 2.],
           [7., 6.],
           [9., 4.]])]
    import numpy as np
    
    data = np.sin(np.arange(20)).reshape(5, 4)  # 生成一组数据
    print(data)
    ind = data.argmax(axis=0)  # 按列查找,返回每列最大值的索引
    print(ind)
    data_max = data[ind, range(data.shape[1])]  # 取出每一列的最大值
    print(data_max)

    结果输出:

    [[ 0.          0.84147098  0.90929743  0.14112001]
     [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
     [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
     [-0.53657292  0.42016704  0.99060736  0.65028784]
     [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
    [2 0 3 1]
    [0.98935825 0.84147098 0.99060736 0.6569866 ]
    a = np.arange(0, 20, 3)
    print(a)
    b = np.tile(a, (4, 2))  # 平铺,行变为原来的多少倍(4),列变为原来的多少倍(2)
    print(b)

    结果输出:

    [ 0  3  6  9 12 15 18]
    [[ 0  3  6  9 12 15 18  0  3  6  9 12 15 18]
     [ 0  3  6  9 12 15 18  0  3  6  9 12 15 18]
     [ 0  3  6  9 12 15 18  0  3  6  9 12 15 18]
     [ 0  3  6  9 12 15 18  0  3  6  9 12 15 18]]

    矩阵排序

    a = np.array([
        [4, 3, 5],
        [1, 2, 1]
    ])
    print(a)
    print("---"*5)
    b = np.sort(a, axis=0)  # 按行从小到大排序
    print(b)
    print("---"*5)
    print(np.sort(a, axis=1)) # 按列从小到大排序
    print("---"*5)
    index = np.argsort(a)  # 获取从小到大排序的索引值
    print(index)

    结果输出:

    [[4 3 5]
     [1 2 1]]
    ---------------
    [[1 2 1]
     [4 3 5]]
    ---------------
    [[3 4 5]
     [1 1 2]]
    ---------------
    [[1 0 2]
     [0 2 1]]
  • 相关阅读:
    給COMBOBOX增加VALUE和名称(完全解决)
    api控制系统服务
    .net 函数注析
    电容降压式电源
    光电耦合器的应用
    javascrip判断null和undefined
    光电耦合器
    Windows 系统错误代码
    Request.ServerVariables
    获得操作系统版本
  • 原文地址:https://www.cnblogs.com/pythoner6833/p/9265032.html
Copyright © 2020-2023  润新知