Python2 语法随笔记录集
1.基本API部分
PRO1. python2生成矩阵Matrix语法
numpy包的zeros,ones,eye方法可以实现功能,看下边的例子:
它们分别是0矩阵,1矩阵 和单位阵
>>> np.zeros((5,6))
array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
>>> np.ones((5,6))
array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])
>>> np.eye(5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
PRO2. Matplotlib作图
一次生成多个子图,获取每个子图的对象,可以使用add_subplot。
axe4 = plt.add_subplot(234) 表示将图分成了2行3列(也就是一共6个子图了),同时,对象axe4表示是第4张图对应的对象。
详细参考:https://blog.csdn.net/zahuopuboss/article/details/54895890
PRO3. Numpy包的 tile函数,max函数,min函数,shape函数
numpy.tile(A, reps):表示将列表A,重复reps构建新的数组。
例如请看下边例子:
>>> a = np.array([0,1,2]) >>> np.tile(a,2) array([0, 1, 2, 0, 1, 2]) >>> np.tile(a,(2,2)) array([[0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2]]) >>> np.tile(a,(2,2,4)) array([[[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]], [[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]]])
ndarray.max([int axis])
函数功能:求ndarray中指定维度的最大值,默认求所有值的最大值。
axis=0:求各column的最大值
axis=1:求各row的最大值
同样地,min也是一个道理,下面是我自己键入的一个例子。
>>> a = np.array(([2,89,34],[43,23,134],[1,3434,332])) >>> a array([[ 2, 89, 34], [ 43, 23, 134], [ 1, 3434, 332]]) >>> a.min() 1 >>> a.max() 3434 >>> a.min(0) array([ 1, 23, 34]) >>> a.min(1) array([ 2, 23, 1])
shape是指:
数组的维度。这是一个整数的元组,元组中的每一个元素对应着每一维度的大小(size)。
numpy中数组的shape属性输出的元组,有以下两个结论:
- 元组的元素的个数等于维度数
- 元组中每一个元素又代表中每一维度元素的个数(从左到右,依次为第一维度中元素的个数,第二维度中元素的个数...第n维度元素的个数)
下边例子的a变量承接上一个例子。
>>> a.shape (3, 3) >>> a.shape[0] 3

浙公网安备 33010602011771号