1、tile 元素重复函数
第二个参数是一维
>>> a=[[1,2,3],[4,5,5]] >>> b=np.tile(a,3) >>> print(b) [[1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5]]
第二个参数是二维
>>> c=np.tile(a,[2,3]) >>> print(c) [[1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5] [1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5]]
第二个参数是三维
d=np.tile(a,[2,3,4]) >>> print(d) [[[1 2 3 1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5 4 5 5] [1 2 3 1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5 4 5 5] [1 2 3 1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5 4 5 5]] [[1 2 3 1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5 4 5 5] [1 2 3 1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5 4 5 5] [1 2 3 1 2 3 1 2 3 1 2 3] [4 5 5 4 5 5 4 5 5 4 5 5]]]
2、expand_dims 元素增加维度
>>> a=[[1,2,3],[4,5,5]] >>> b=np.expand_dims(a,0) >>> print(b) [[[1 2 3] [4 5 5]]] >>> c=np.expand_dims(a,1) >>> print(c) [[[1 2 3]] [[4 5 5]]] >>> d=np.expand_dims(a,2) >>> print(d) [[[1] [2] [3]] [[4] [5] [5]]]
3、求accuracy precision recall
import numpy as np from sklearn import metrics def get_precision(lable, pre): # ((lable == pre) & lable).sum() / (pre.sum() + 0.00001) return metrics.precision_score(lable, pre) def get_recall(lable, pre): # ((lable == pre) & lable).sum() / (lable.sum() + 0.00001) return metrics.recall_score(lable, pre) def get_accuracy(label, pre): return (label == pre).mean() def get_auc(label, pre): return metrics.roc_auc_score(label, pre) if __name__ == '__main__': print get_recall(np.array([True, False, False]), np.array([True, False, True]))
4、不使用科学计数法
np.set_printoptions(suppress=True)