技巧

Numpy

  1. import numpy as np
  2. A = np.random.randn(3,3)
  3. B = np.random.randn(3,3)
  4. print('A:\n{0}\n B:\n{1}'.format(A,B))
  1. A:
  2. [[-0.16819223 -0.65005007 -1.45306835]
  3. [ 0.14667478 -1.40195255 -0.12871305]
  4. [-0.86260073 0.25187163 -2.58701945]]
  5. B:
  6. [[-0.36228023 -1.5495955 -0.43543578]
  7. [-0.44032362 0.68811886 -0.40343672]
  8. [ 0.07358677 -0.64011174 -1.66832965]]
  • 最大/最小值
  1. C = np.maximum(A,B)
  2. D = np.minimum(A,B)
  3. print('取A/B对应位置最大值:',C,sep='\n')
  4. print('取A/B组对应位置最小值:',D,sep='\n')
  1. A/B对应位置最大值:
  2. [[-0.16819223 -0.65005007 -0.43543578]
  3. [ 0.14667478 0.68811886 -0.12871305]
  4. [ 0.07358677 0.25187163 -1.66832965]]
  5. A/B组对应位置最小值:
  6. [[-0.36228023 -1.5495955 -1.45306835]
  7. [-0.44032362 -1.40195255 -0.40343672]
  8. [-0.86260073 -0.64011174 -2.58701945]]
  • relu(A)
  1. C = np.maximum(A,0)
  2. print('relu(A):\n{}'.format(C))
  1. relu(A):
  2. [[0. 0. 0. ]
  3. [0.14667478 0. 0. ]
  4. [0. 0.25187163 0. ]]
  • 高斯分布
  1. C = np.random.normal(0,1,(3,3))
  2. print('高斯分布:\n{}'.format(C))
  1. 高斯分布:
  2. [[ 2.38548299 -0.66467181 -0.23646556]
  3. [-0.13876203 -0.35762595 -0.44623119]
  4. [-0.78050996 0.01327971 0.20492358]]
  • 字典排序
  1. d={'a':1,'c':3,'b':2}
  2. d_order=sorted(d.items(),key=lambda x:x[1],reverse=False)
  3. print(d_order)
  1. [('a', 1), ('b', 2), ('c', 3)]
  • 字典列表排序
  1. d=[{'a':1},{'a':3},{'a':2}]
  2. d_order = sorted(d,key=lambda x:x['a'],reverse=True)
  3. print(d_order)
  1. [{'a': 3}, {'a': 2}, {'a': 1}]

BUG

  1. a = A
  2. b = A,
  3. print('type of (A):{}\ntype of (A,):{}'.format(type(a),type(b)))
  1. type of (A):<class 'numpy.ndarray'>
  2. type of (A,):<class 'tuple'>
  1. ## 默认并不是间隔为1进行划分,而是点数为50进行划分
  2. len_lin = len(np.linspace(-6,-1))
  3. len_log = len(np.logspace(-6,-1))
  4. print('len(np.linspace(-6,-1)):',len_lin,'\nlen(np.logspace(-6,-1)):',len_log)
  1. len(np.linspace(-6,-1)): 50
  2. len(np.logspace(-6,-1)): 50