'''
# coding=utf-8
Created on 2016-9-7
@author: paulsweet
'''
import numpy as np
a=np.arange(12).reshape(3,4)
a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
a.sum(axis=0)
array([12, 15, 18, 21])
for row in a:
print(row)
[0 1 2 3]
[4 5 6 7]
[ 8 9 10 11]
for element in a.flat:
print(element)
0
1
2
3
4
5
6
7
8
9
10
11
np.arange(0, 60, 10).reshape(-1, 1) + np.arange(0, 6)
array([[ 0, 1, 2, 3, 4, 5],
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
[40, 41, 42, 43, 44, 45],
[50, 51, 52, 53, 54, 55]])
组合函数
- hstack
- vstack
- dstack
- concatenate
x=np.arange(9).reshape(3,3)
y=x*3
print(x);print(y)
[[0 1 2]
[3 4 5]
[6 7 8]]
[[ 0 3 6]
[ 9 12 15]
[18 21 24]]
np.hstack((x,y))
array([[ 0, 1, 2, 0, 3, 6],
[ 3, 4, 5, 9, 12, 15],
[ 6, 7, 8, 18, 21, 24]])
np.vstack((x,y))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]])
np.dstack((x,y))
array([[[ 0, 0],
[ 1, 3],
[ 2, 6]],
[[ 3, 9],
[ 4, 12],
[ 5, 15]],
[[ 6, 18],
[ 7, 21],
[ 8, 24]]])
np.concatenate((x,y),axis=0)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]])
np.concatenate((x,y),axis=1)
array([[ 0, 1, 2, 0, 3, 6],
[ 3, 4, 5, 9, 12, 15],
[ 6, 7, 8, 18, 21, 24]])