import numpy;
print '使用列表生成一维数组'
data = [1,2,3,4,5,6]
data2 = data + [7.0]
x = numpy.array(data)
y = numpy.array(data2)
print data #打印列表 [1, 2, 3, 4, 5, 6]
print x, y #打印数组 [1 2 3 4 5 6] [ 1. 2. 3. 4. 5. 6. 7.]
print x.dtype, y.dtype #打印数组元素的类型 int32 float64
print '使用列表生成多维数组'
data = [[1,2],[3,4],[5,6],[7,8]]
data2 = [[[1,2,3],[2,3,4]], [[1,4,3],[5,3,4]], [[5,2,3],[6,3,4]]]
x = numpy.array(data)
y = numpy.array(data2)
print x, y #打印数组
print x.ndim, y.ndim #打印数组的总维度数,即从外到内方括号的层数 2 3
print x.shape, y.shape #打印数组各个维度的长度,即从最外层方括号往内层数每个维度的长度。shape是一个元组。当数组为二维矩阵时,shape分别表示行数和列数 (4L, 2L) (3L, 2L, 3L)
print '使用zero/ones/empty创建数组:根据shape来创建'
x = numpy.zeros(6) #创建一维长度为6的,元素都是0一维数组
print x
x = numpy.zeros((2,3)) #创建一维长度为2,二维长度为3的二维0数组
print x
x = numpy.ones((2,3)) #创建一维长度为2,二维长度为3的二维1数组
print x
x = numpy.empty((4,3)) #创建一维长度为4,二维长度为3,未初始化的二维数组
print x, x.shape, x.shape[0] #(4L, 3L) 4
print '使用arrange生成连续元素'
print numpy.arange(6) # 开区间 [0 1 2 3 4 5]
print numpy.arange(0,6,2) # start,stop,step [0 2 4]
print '使用tile重复生成新数组'
data = [1,2]
x = numpy.array(data)
y = numpy.tile(data, (2, 3, 5))
print y, y.shape #注意y.shape和reps的关系 (2L, 3L, 10L)
需要查询某个方法,直接输入例如numpy.tile查找文档即可。