Numpy 01

发布时间:2019-03-08 20:44:48编辑:auto阅读(1636)

     Infi-chu:

    http://www.cnblogs.com/Infi-chu/

    import numpy as np
    
    # 创建的数组
    stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
    
    # 基本属性
    count = stus_score.size
    print('该数组的元素有 --> ',count)
    shape = stus_score.shape
    print('该数组的形状是 --> ',shape) # shape结果的第一个元素是行,第二个元素是列
    ndim = stus_score.ndim
    print('该数组的维度 --> ',ndim)
    type = stus_score.dtype
    print('该数组元素类型是 --> ',type)
    
    
    # 快速创建n维数组的API函数
    # 创建10行10列的数值为浮点1的矩阵
    array_one = np.ones([10,10])
    print('array_one --> ',array_one)
    # 创建10行10列的数值为浮点1的矩阵
    array_zero = np.zeros([10,10])
    print('array_zero --> ',array_zero)
    
    # Numpy创建随机数组
    # 均值分布
    '''
    np.random.rand(10, 10)创建指定形状(示例为10行10列)的数组(范围在0至1之间)
    np.random.uniform(0, 100)创建指定范围内的一个数
    np.random.randint(0, 100) 创建指定范围内的一个整数
    '''
    
    # 正态分布
    '''
    给定均值/标准差/维度的正态分布np.random.normal(1.75, 0.1, (2, 3))
    '''
    
    # 数组索引、切片
    # 正态生成4行5列的二维数组
    arr = np.random.normal(1.75, 0.1, (4, 5))
    print(arr)
    # 截取第1至2行的第2至3列(从第0行算起)
    after_arr = arr[1:3, 2:4]
    print(after_arr)
    
    # 改变数组形状(要求前后元素个数匹配)
    print("reshape函数的使用!")
    one_20 = np.ones([20])
    print("-->1行20列<--")
    print (one_20)
    one_4_5 = one_20.reshape([4, 5])
    print("-->4行5列<--")
    print (one_4_5)
    
    # 数组的计算
    # 比较
    res = stus_score > 80
    print(res)
    res = np.where(stus_score > 80)
    print(res)
    res = np.where(stus_score > 80,'Yes','No')  # 大于80的重写为Yes,否则为No
    print(res)
    # 求最大值
    print('数组是:\n',stus_score)
    # 求每一列的最大值(0表示列)
    result = np.amax(stus_score, axis=0)
    print("每一列的最大值为:\n",result)
    # 求每一行的最大值(1表示列)
    result = np.amax(stus_score, axis=1)
    print("每一行的最大值为:\n",result)
    # 求最小值
    # 求每一行的最小值(0表示列)
    print("每一列的最小值为:")
    result = np.amin(stus_score, axis=0)
    print(result)
    # 求每一行的最小值(1表示行)
    print("每一行的最小值为:")
    result = np.amin(stus_score, axis=1)
    print(result)
    # 求平均值
    # 求每一行的平均值(0表示列)
    print("每一列的平均值:")
    result = np.mean(stus_score, axis=0)
    print(result)
    # 求每一行的平均值(1表示行)
    print("每一行的平均值:")
    result = np.mean(stus_score, axis=1)
    print(result)
    # 求方差
    # 求每一行的方差(0表示列)
    print("每一列的方差:")
    result = np.std(stus_score, axis=0)
    print(result)
    # 求每一行的方差(1表示行)
    print("每一行的方差:")
    result = np.std(stus_score, axis=1)
    print(result)
    
    # 数组的运算
    # 加法
    print("加分前:")
    print(stus_score)
    # 为所第一列成绩都加5分
    stus_score[:, 0] = stus_score[:, 0]+5
    stus_score_new = stus_score[:, 0]+5
    print("加分后:")
    print(stus_score)
    print('')
    print(stus_score_new)
    # 乘法
    print("减半前:")
    print(stus_score)
    # 平时成绩减半
    stus_score[:, 0] = stus_score[:, 0]*0.5
    print("减半后:")
    print(stus_score)
    # 数组间运算
    a = np.array([1, 2, 3, 4])
    b = np.array([10, 20, 30, 40])
    c = a + b
    d = a - b
    e = a * b
    f = a / b
    print("a+b为", c)
    print("a-b为", d)
    print("a*b为", e)
    print("a/b为", f)
    
    # np.dot()
    # (M行, N列) * (N行, Z列) = (M行, Z列)
    # 平时成绩占40% 期末成绩占60%, 计算结果
    q = np.array([[0.4], [0.6]])
    result = np.dot(stus_score, q)
    print("最终结果为:")
    print(result)
    
    # 矩阵拼接
    # 垂直拼接
    print("v1为:")
    v1 = [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
    print(v1)
    print("v2为:")
    v2 = [[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
    print(v2)
    result = np.vstack((v1, v2))
    print("v1和v2垂直拼接的结果为:")
    print(result)
    # 水平拼接
    print("v1为:")
    v1 = [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
    print(v1)
    print("v2为:")
    v2 = [[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
    print(v2)
    result = np.hstack((v1, v2))
    print("v1和v2水平拼接的结果为")
    print(result)
    

     

关键字