zoukankan      html  css  js  c++  java
  • 数组的算数运算

    除法

    Key_Function

    np.divide方法, 返回带小数位的结果

    np.true_divide方法, 返回带小数位的结果

    np.floor_divide方法, 返回只有整数位的结果

    / 运算符, 返回带小数位的结果

    // 运算符, 返回只有整数位的结果

    import numpy as np
    
    a = np.array([2, 6, 5])
    b = np.array([1, 2, 3])
    print(np.divide(b, a))
    # [ 0.5         0.33333333  0.6       ]
    print(np.divide(a, b))
    # [ 2.          3.          1.66666667]
    
    print(np.true_divide(b, a))
    # [ 0.5         0.33333333  0.6       ]
    print(np.true_divide(a, b))
    # [ 2.          3.          1.66666667]
    
    print(np.floor_divide(b, a))    # np.floor_divide总是返回整数部分
    # [0 0 0]
    print(np.floor_divide(a, b))    # np.floor_divide总是返回整数部分
    # [2 3 1]
    
    print(b / a)
    # [ 0.5         0.33333333  0.6       ]
    print(a / b)
    # [ 2.          3.          1.66666667]
    
    print(b // a)   # 对应floor_divide
    # [0 0 0]
    print(a // b)
    # [2 3 1]

    模运算

    模运算就是取余数

    Key_Function

    np.remainder函数, 逐个返回两个数组中元素相除后的余数, 如果第二个参数为0, 则直接返回0

    np.mod函数,  与remainder函数功能完全一致

    % 运算符, 是np.remainder的简写

    np.fmod函数, 所得余数的正负由被除数决定, 与除数无关  

    Code

    a = np.arange(-4, 4)
    print(a)
    # [-4 -3 -2 -1  0  1  2  3]
    
    print(np.remainder(a, 2))   # remainder函数逐个返回两个数组中元素相除后的余数
    # [0 1 0 1 0 1 0 1]
    
    print(np.mod(a, 2))     # mod函数与remainder函数功能完全一致
    # [0 1 0 1 0 1 0 1]
    
    print(a % 2)
    # [0 1 0 1 0 1 0 1]     # % 是remainder的简写
    
    print(np.fmod(a, 2))    # 所得余数的正负由被除数决定, 与除数的正负无关 
    # [ 0 -1  0 -1  0  1  0  1]
  • 相关阅读:
    Aibabelx-shop 大型微服务架构系列实战之技术选型
    龙应台:中年人的迷惘,比年轻人的更可怕!
    劝进篇
    高并发与多线程的关系、区别、高并发的技术方案
    redis常用知识
    Lucene全文检索入门使用
    大数据综合案例-网站日志分析
    python数据类型
    Tornado
    flask-sqlalchemy用法详解
  • 原文地址:https://www.cnblogs.com/draven123/p/11397560.html
Copyright © 2011-2022 走看看