import numpy as np a=np.array([1,2,3]) print a b=np.array([[1,2],[3,4]]) print b print np.arange(1,5,0.5)#取不到 5 print np.random.random((2,2))#2*2随机浮点数数组 print np.linspace(1,2,10,endpoint=False) print np.linspace(1,2,10) #默认2能够取到 print np.fromfunction(lambda i,j:(i+1)*(j+1),[2,3]) c=np.array([4,5,6]) print np.hstack((a,c))#横向连接 print np.vstack((a,c))#纵向连接 print b.T #转置 print a+c#对应元素相加
ans:
[1 2 3]
[[1 2]
[3 4]]
[ 1. 1.5 2. 2.5 3. 3.5 4. 4.5]
[[ 0.54937261 0.22419401]
[ 0.41047764 0.53122061]]
[ 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]
[ 1. 1.11111111 1.22222222 1.33333333 1.44444444 1.55555556
1.66666667 1.77777778 1.88888889 2. ]
[[ 1. 2. 3.]
[ 2. 4. 6.]]
[1 2 3 4 5 6]
[[1 2 3]
[4 5 6]]
[[1 3]
[2 4]]
通过list生成数组:
import numpy as np a=np.array([1,2,3]) x=[1,2,3] xx=np.array(x) print xx y=[[1,2],[3,4]] yy=np.array(y) print yy print yy.flatten()#展平,不能作用于列表 ans: [1 2 3] [[1 2] [3 4]] [1 2 3 4]
b=np.array([[1,2],[3,4]]) print b.sum() print b.sum(axis=0)#按列求和 print b.sum(axis=1)#按行求和 print b.min() print b.argmax()#max的下标 print b.mean()
print b.std() b[1] Out[117]: array([3, 4]) b[1][1] Out[118]: 4
ans:
10
[4 6]
[3 7]
1
3
2.5
1.11803398875
remark:数组的属性函数不需要末尾加()
b=np.array([[1,2],[3,4]]) print b.size#元素个数 print b.ndim#秩 print b.shape print b.dtype#元素类型 ans: 4 2 (2L, 2L) int32
numpy比math的计算速度更快:
import numpy as np import time import math x=np.arange(0,100,0.01) t1=time.clock() for i,t in enumerate(x): x[i]=math.pow((math.sin(t)),2) t2=time.clock() y=np.arange(0,100,0.01) t3=time.clock() y=np.power(np.sin(y),2) t4=time.clock() print(‘running time of math is:‘,t2-t1) print(‘running time of numpy is:‘,t4-t3) ans: (‘running time of math is:‘, 0.011655904997766612) (‘running time of numpy is:‘, 0.0023625201675097404)
numpy与scipy的结合使用:
import numpy as np from scipy import linalg a=np.array([[1,0],[1,2]]) print np.dot(a,a) print linalg.det(a) print linalg.inv(a) x,y=linalg.eig(a) print x #特征值 print y#特征向量??奇怪的值,随便给出的一个特征向量 ans: [[1 0] [3 4]] 2.0 [[ 1. -0. ] [-0.5 0.5]] [ 2.+0.j 1.+0.j] [[ 0. 0.70710678] [ 1. -0.70710678]]
import numpy as np from scipy.cluster.vq import vq,kmeans,whiten list1=[89,90,76,90] list2=[96,78,89,79] list3=[90,98,89,80] list4=[80,72,79,84] list5=[92,81,89,87] data=np.array([list1,list2,list3,list4,list5]) print data whiten=whiten(data) centroids,_=kmeans(whiten,2) result,_=vq(whiten,centroids) print(result) ans: [[89 90 76 90] [96 78 89 79] [90 98 89 80] [80 72 79 84] [92 81 89 87]] [1 0 0 1 0]
三维数组:
import numpy as np b=np.arange(24).reshape(2,3,4) print b print b[1]#访问第二层 print b[0,1,::2]#从头开始,间隔是2 print b[0,1,[1,2]] print b[::-1]#两层交换 print b.ravel()#展平变一维的数组 print b.reshape(6,4) x=np.array([[1,2,3],[4,5,6],[7,8,9]]) #分割数组 x1,x2,x3=np.hsplit(x,3) print x1 #得到[1,4,7] y1,y2,y3=np.vsplit(x,3) print y1#得到[1,2,3] z=np.array([1+2.j,3+4.j]) print z.real print z.tolist() print x.tolist() ans: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]] [4 6] [5 6] [[[12 13 14 15] [16 17 18 19] [20 21 22 23]] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15] [16 17 18 19] [20 21 22 23]] [[1] [4] [7]] [[1 2 3]] [ 1. 3.] [(1+2j), (3+4j)] [[1, 2, 3], [4, 5, 6], [7, 8, 9]]