• 2-8 四则运算


    In [2]:
    import numpy as np
    x=np.array([5,5])
    y=np.array([2,2])
    
     

    乘法

    In [3]:
    np.multiply(x,y)
    
    Out[3]:
    array([10, 10])
    In [4]:
    np.dot(x,y)#内积
    
    Out[4]:
    20
    In [5]:
    x.shape
    
    Out[5]:
    (2,)
    In [6]:
    y.shape
    
    Out[6]:
    (2,)
    In [7]:
    x.shape=2,1
    x
    
    Out[7]:
    array([[5],
           [5]])
    In [8]:
    np.dot(x,y)#维度不一样
    
     
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-8-6849a5f7ad6c> in <module>()
    ----> 1np.dot(x,y)
    
    ValueError: shapes (2,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)
    In [9]:
    y.shape=1,2
    y
    
    Out[9]:
    array([[2, 2]])
    In [10]:
    x
    
    Out[10]:
    array([[5],
           [5]])
    In [12]:
    print(x.shape)
    print(y.shape)
    
     
    (2, 1)
    (1, 2)
    
    In [14]:
    np.dot(x,y)
    
    Out[14]:
    array([[10, 10],
           [10, 10]])
    In [15]:
    np.dot(y,x)
    
    Out[15]:
    array([[20]])
    In [17]:
    x=np.array([1,1,1])
    y=np.array([[1,2,3],[4,5,6]])
    print(x*y)#自动加维数转换
    
     
    [[1 2 3]
     [4 5 6]]
    
    In [18]:
    x=np.array([1,1,1])
    y=np.array([1,1,1])
    x==y#必须维度一样,逐一进行比较
    
    Out[18]:
    array([ True,  True,  True])
    In [19]:
    np.logical_and(x,y)#逻辑与操作
    
    Out[19]:
    array([ True,  True,  True])
    In [20]:
    np.logical_or(x,y)#或
    
    Out[20]:
    array([ True,  True,  True])
    In [21]:
    np.logical_not(x,y)#非
    
    Out[21]:
    array([0, 0, 0])
  • 相关阅读:
    vijos p1782——借教室(noip2012提高组第2题)
    vijos p1781——同余方程(noip2012提高组第1题)
    vijos p1905——生活大爆炸版 石头剪刀布(noip2014提高组第一题)
    URAL_1018 二叉苹果树
    b_lc_统计同构子字符串的数目(找规律 / dp)
    a_lc_完成所有工作的最短时间(暴搜 / 状压)
    lc_b_栈和队列设计(都需要不断踢出非法元素的过程)
    a_lc_缺失的第一个整数 I~II(暴力 / 不断放到正确位置)
    b_lc_最短无序连续子数组(暴力 / )
    b_lc_把二叉搜索树转换为累加树(逆中序遍历 / 迭代)
  • 原文地址:https://www.cnblogs.com/AI-robort/p/11627176.html
Copyright © 2020-2023  润新知