Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numpy. Numpy objects >>> import numpy as np >>> np.array([1,2,3]) array([1, 2, 3]) >>> np.array([1,2,3.0]) array([ 1., 2., 3.]) >>> np.array([1,2,3],np.float64)

Similar presentations


Presentation on theme: "Numpy. Numpy objects >>> import numpy as np >>> np.array([1,2,3]) array([1, 2, 3]) >>> np.array([1,2,3.0]) array([ 1., 2., 3.]) >>> np.array([1,2,3],np.float64)"— Presentation transcript:

1 numpy

2 Numpy objects >>> import numpy as np >>> np.array([1,2,3]) array([1, 2, 3]) >>> np.array([1,2,3.0]) array([ 1., 2., 3.]) >>> np.array([1,2,3],np.float64) array([ 1., 2., 3.]) >>> np.array([range(3) for x in range(4)],np.float64) array([[ 0., 1., 2.], [ 0., 1., 2.], [ 0., 1., 2.]]) >>>

3 Numpy objects >>> A= np.array([range(3) for x in range(4)],np.float64) >>> np.matrix([range(3) for x in range(4)],np.float64) matrix([[ 0., 1., 2.], [ 0., 1., 2.], [ 0., 1., 2.]]) >>> M= np.matrix([range(3) for x in range(4)],np.float64) >>> type(A), type(M) (, ) >>> np.matrix(range(3),np.float64) matrix([[ 0., 1., 2.]]) >>> np.array(range(3),np.float64) array([ 0., 1., 2.]) >>> M=np.matrix(range(3),np.float64) >>> A=np.array(range(3),np.float64) >>> A.shape (3,) >>> M.shape (1, 3)

4 Numpy objects >>> M=np.matrix(range(3),np.float64) >>> A=np.array(range(3),np.float64) >>> A.T array([ 0., 1., 2.]) >>> M.T matrix([[ 0.], [ 1.], [ 2.]]) >>> A=np.array([[1,2],[2,-2]],np.float64) >>> M=np.matrix([[1,2],[2,-2]],np.float64) >>> M.T matrix([[ 1., 2.], [ 2., -2.]]) >>> A.T array([[ 1., 2.], [ 2., -2.]]) >>> A.I Traceback (most recent call last): File " ", line 1, in AttributeError: 'numpy.ndarray' object has no attribute 'I' >>> M.I matrix([[ , ], [ , ]]) >>>

5 Numpy objects >>> M matrix([[ 1., 2.], [ 2., -2.]]) >>> M.I matrix([[ , ], [ , ]]) >>> np.linalg.inv(A) array([[ , ], [ , ]]) >>> >>> M2=np.matrix([[1,1],[0,0]],np.float64) >>> A2=np.array([[1,1],[0,0]],np.float64) >>> M*M2 matrix([[ 1., 1.], [ 2., 2.]]) >>> A*A2 array([[ 1., 2.], [ 0., -0.]]) >>> A2*A array([[ 1., 2.], [ 0., -0.]]) >>> M2*M matrix([[ 3., 0.], [ 0., 0.]])

6 Numpy objects >>> A == M matrix([[ True, True], [ True, True]], dtype=bool) >>> Ai=np.linalg.inv(A) >>> np.dot(A,Ai) array([[ e+00, e-17], [ e+00, e+00]]) >>> np.dot(Ai,A) array([[ e+00, e-16], [ e+00, e+00]]) >>> M*M.I matrix([[ e+00, e-17], [ e+00, e+00]]) >>> M.I*M matrix([[ e+00, e-16], [ e+00, e+00]])

7 Numpy objects >>> np.random.uniform() >>> np.random.uniform(-0.1,0.1) >>> np.random.uniform(-0.1,0.1,4) array([ , , , ]) >>> np.random.uniform(-0.1,0.1,(2,2)) array([[ , ], [ , ]]) >>> np.random.normal(-1,1,(3,2)) array([[ , ], [ , ], [ , ]]) >>> >>> np.sort([1,5.9,2,-1]) array([-1., 1., 2., 5.9]) >>> np.argsort([1,5.9,2,-1]) array([3, 0, 2, 1]) >>>

8 Numpy objects >>>np.ones((4,4)) array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]]) >>> np.zeros((4,4)) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> A=np.zeros((4,4)) >>> for i in range(len(A)):... A[i][:]=np.random.uniform(-10,10,len(A))... >>> A array([[ , , , ], [ , , , ], [ , , , ], [ , , , ]]) >>> for i in range(len(A)):... A[i,:]=np.random.uniform(-10,10,len(A))...

9 Numpy objects >>> B array([[ 0., 1.], [ 1., 0.]]) >>> evals,evecs=np.linalg.eig(B) >>> evals array([ 1., -1.]) >>> for i in range(len(B)):... print evals[i],evecs[i] [ ] -1.0 [ ] >>>

10 Numpy objects >>> A+A.T array([[ , , , ], [ , , , ], [ , , , ], [ , , , ]]) >>> A=A+A.T >>> evals,evecs=np.linalg.eig(A) >>> evals array([ , , , ])

11 Numpy objects >>> inputs=np.array([[ 1.],[ 2.],[ 3.],[ 4.],[ 5.]]) >>> targets=np.array([[ 3.],[ 5.],[ 7.],[ 9.],[ 11.]]) >>> np.concatenate((inputs,targets)) array([[ 1.], [ 2.], [ 3.], [ 4.], [ 5.], [ 3.], [ 5.], [ 7.], [ 9.], [ 11.]]) >>> np.concatenate((inputs,targets),axis=1) array([[ 1., 3.], [ 2., 5.], [ 3., 7.], [ 4., 9.], [ 5., 11.]]) >>> it=np.concatenate((inputs,targets),axis=1)

12 Numpy objects >>> print np.sum(it), it.size >>> print np.mean(it), np.sum(it)/it.size 5.0 >>> np.sum(it,axis=0), np.sum(it,axis=1) (array([ 15., 35.]), array([ 4., 7., 10., 13., 16.])) >>> len(it), len(it[0]) (5, 2) >>> np.mean(it,axis=0),np.mean(it,axis=1) (array([ 3., 7.]), array([ 2., 3.5, 5., 6.5, 8. ])) >>>

13 Numpy objects >>> D=np.zeros((20,3)) >>> for i in range(len(D)):... D[i,:]=np.random.uniform(-10,10,len(D[i])) >>> D.mean(axis=0) array([ , , ]) >>> >>> np.std(D) >>> np.std(D,axis=0) array([ , , ]) >>> np.var(D,axis=0) array([ , , ]) >>> np.sqrt(np.var(D,axis=0)) array([ , , ]) >>>

14 Numpy objects >>> r=0.1 >>> a=np.array(range(4),np.float)+np.random.uniform(-r,r) >>> b=np.array(range(4),np.float)+np.random.uniform(-r,r) -5 >>> c=np.array(range(4,0,-1),np.float)+np.random.uniform(-r,r) -5 >>> d=np.matrix([np.random.uniform(-r,r) for i in range(4)],np.float) >>> np.cov(a,a) array([[ , ], [ , ]]) >>> np.cov(a,b) array([[ , ], [ , ]]) >>> np.cov(a,c) array([[ , ], [ , ]]) >>> np.cov(a,d) array([[ , ], [ , ]])


Download ppt "Numpy. Numpy objects >>> import numpy as np >>> np.array([1,2,3]) array([1, 2, 3]) >>> np.array([1,2,3.0]) array([ 1., 2., 3.]) >>> np.array([1,2,3],np.float64)"

Similar presentations


Ads by Google