少量元素情况
打印数组中的元素
import numpy as np
a = np.array(6)
print a
程序结果为:
[0 1 2 3 4 5]大量元素情况
可以采用 set_printoptions(threshold='nan')
import numpy as np
np.set_printoptions(threshold=np.NaN)
print np.arange(100)
print np.arange(100).reshape(10, 10)
结果为:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99]
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
当array里面的存放的数据维度过大时,在控制台会出现不能将array完全输出的情况,中间部分的结果会用省略号打印出来。这时就需要用到numpy里面的set_printoptions()方法
我们来看一下 set_printoptions 方法的简单说明
set_printoptions(precision=None,threshold=None,
edgeitems=None,
linewidth=None,
suppress=None,
nanstr=None,
infstr=None,
formatter=None)
precision:输出结果保留精度的位数
threshold:array数量的个数在小于threshold的时候不会被折叠
edgeitems:在array已经被折叠后,开头和结尾都会显示edgeitems个数
formatter:这个很有意思,像python3里面str.format(),就是可以对你的输出进行自定义的格式化