• numpy学习笔记


    In [4]:
     
     
     
     
     
     
     
     
     
    import numpy as np
    my_list = [1,2,3]
    x = np.array(my_list)
    print('列表',my_list)
    print('Array:',x)
     
     
     
    列表 [1, 2, 3]
    Array: [1 2 3]
    
    In [5]:
     
     
     
     
     
     
     
     
     
     
    np.array([1,2,3])-np.array([4,5,6])
     
     
    Out[5]:
    array([-3, -3, -3])
    In [9]:
     
     
     
     
     
     
     
     
     
     
    my_list = [[1,2,3],[4,5,6]]
    x = np.array(my_list)
    print(x)
    print("x's shape is : ",x.shape)
     
     
     
    [[1 2 3]
     [4 5 6]]
    x's shape is :  (2, 3)
    
    In [10]:
     
     
     
     
     
     
     
     
     
     list  = np.arange(0,30,2)
     print(list)
    
    
     
     
     
    [ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28]
    
    In [12]:
     
     
     
     
     
     
     
     
    new_array = list.reshape(3,5)
    print(new_array)
     
     
     
    [[ 0  2  4  6  8]
     [10 12 14 16 18]
     [20 22 24 26 28]]
    
    In [18]:
     
     
     
     
     
    x
     
     
    print('ones:
    ',np.ones((3,2)))
    print('zeros:
    ',np.zeros((3,2)))
    print('eye:
    ',np.eye((3)))
    print('diag:
    ',np.diag((1,2,3)))
    print('diag:
    ',np.diag(my_list))
     
     
     
    ones:
     [[ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]]
    zeros:
     [[ 0.  0.]
     [ 0.  0.]
     [ 0.  0.]]
    eye:
     [[ 1.  0.  0.]
     [ 0.  1.  0.]
     [ 0.  0.  1.]]
    diag:
     [[1 0 0]
     [0 2 0]
     [0 0 3]]
    diag:
     [1 5]
    
    In [20]:
     
     
     
     
     
     
     
     
     
    x=np.arange(16).reshape((4,4))
    print(x)
     
     
     
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]
     [12 13 14 15]]
    
    In [21]:
     
     
     
     
     
    x
     
     
     
    print('diag:
    ',np.diag(x,1))
    print('diag:
    ',np.diag(x,-1))
     
     
     
    diag:
     [ 1  6 11]
    diag:
     [ 4  9 14]
    
    In [22]:
     
     
     
     
     
     
     
     
    print('diag:
    ',np.diag(x,2))
    print('diag:
    ',np.diag(x,-2))
     
     
     
    diag:
     [2 7]
    diag:
     [ 8 13]
    
    In [23]:
     
     
     
     
     
    x
     
     
    print("*操作:
    ",np.array([1,2,3]*3))
    print("*操作:
    ",np.repeat([1,2,3],3))
     
     
     
    *操作:
     [1 2 3 1 2 3 1 2 3]
    *操作:
     [1 1 1 2 2 2 3 3 3]
    
    In [25]:
     
     
     
     
     
     
     
     
     
    p1 = np.ones((3,3))
    p2 = np.arange(9).reshape(3,3)
    print("纵向+
    ",np.vstack((p1,p2)))
    print("横向+
    ",np.hstack((p1,p2)))
     
     
     
    纵向+
     [[ 1.  1.  1.]
     [ 1.  1.  1.]
     [ 1.  1.  1.]
     [ 0.  1.  2.]
     [ 3.  4.  5.]
     [ 6.  7.  8.]]
    横向+
     [[ 1.  1.  1.  0.  1.  2.]
     [ 1.  1.  1.  3.  4.  5.]
     [ 1.  1.  1.  6.  7.  8.]]
    
    In [27]:
     
     
     
     
     
     
     
     
     
     
      print('p1 + p2 
    ',p1+p2)
      print('p1 * p2 
    ',p1*p2)
      print('p2^2 
    ',p2**2)
      print('p1 . p2 
    ',p1.dot(p2))
    
    
     
     
     
    p1 + p2 
     [[ 1.  2.  3.]
     [ 4.  5.  6.]
     [ 7.  8.  9.]]
    p1 * p2 
     [[ 0.  1.  2.]
     [ 3.  4.  5.]
     [ 6.  7.  8.]]
    p2^2 
     [[ 0  1  4]
     [ 9 16 25]
     [36 49 64]]
    p1 . p2 
     [[  9.  12.  15.]
     [  9.  12.  15.]
     [  9.  12.  15.]]
    
    In [ ]:
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    获取文件扩展名(后缀)
    文件字节大小显示成M,G和K
    mybatis在Mapper的xml文件中的转义字符的处理
    Java反射中method.isBridge() 桥接方法
    MyBatis框架的使用及源码分析(十三) ResultSetHandler
    MyBatis框架的使用及源码分析(十二) ParameterHandler
    MyBatis框架的使用及源码分析(十一) StatementHandler
    MyBatis框架的使用及源码分析(十) CacheExecutor,SimpleExecutor,BatchExecutor ,ReuseExecutor
    MyBatis框架的使用及源码分析(九) Executor
    MyBatis框架的使用及源码分析(八) MapperMethod
  • 原文地址:https://www.cnblogs.com/crawer-1/p/7660800.html
Copyright © 2020-2023  润新知