520 likes | 1.18k Vues
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) array([ 1., 2., 3.]) >>> np.array([range(3) for x in range(4)],np.float64) array([[ 0., 1., 2.],
E N D
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.], [ 0., 1., 2.]]) >>>
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.], [ 0., 1., 2.]]) >>> M= np.matrix([range(3) for x in range(4)],np.float64) >>> type(A), type(M) (<type 'numpy.ndarray'>, <class 'numpy.core.defmatrix.matrix'>) >>> 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)
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 "<stdin>", line 1, in <module> AttributeError: 'numpy.ndarray' object has no attribute 'I' >>> M.I matrix([[ 0.33333333, 0.33333333], [ 0.33333333, -0.16666667]]) >>>
Numpy objects >>> M matrix([[ 1., 2.], [ 2., -2.]]) >>> M.I matrix([[ 0.33333333, 0.33333333], [ 0.33333333, -0.16666667]]) >>>np.linalg.inv(A) array([[ 0.33333333, 0.33333333], [ 0.33333333, -0.16666667]]) >>> >>> 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.]])
Numpy objects >>> A == M matrix([[ True, True], [ True, True]], dtype=bool) >>> Ai=np.linalg.inv(A) >>> np.dot(A,Ai) array([[ 1.00000000e+00, 5.55111512e-17], [ 0.00000000e+00, 1.00000000e+00]]) >>> np.dot(Ai,A) array([[ 1.00000000e+00, -1.11022302e-16], [ 0.00000000e+00, 1.00000000e+00]]) >>> M*M.I matrix([[ 1.00000000e+00, 5.55111512e-17], [ 0.00000000e+00, 1.00000000e+00]]) >>> M.I*M matrix([[ 1.00000000e+00, -1.11022302e-16], [ 0.00000000e+00, 1.00000000e+00]])
Numpy objects >>> np.random.uniform() 0.70420105296872415 >>> np.random.uniform(-0.1,0.1) -0.059314492060574403 >>> np.random.uniform(-0.1,0.1,4) array([ 0.06606591, 0.07766084, 0.08635536, 0.08010191]) >>> np.random.uniform(-0.1,0.1,(2,2)) array([[-0.06448909, 0.07906606], [-0.04752628, -0.02955906]]) >>> np.random.normal(-1,1,(3,2)) array([[-0.32922971, -0.05700329], [-2.81944081, 0.43708656], [-1.4274894 , -0.61651697]]) >>> >>> 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]) >>>
Numpy objects >>>np.ones((4,4)) array([[ 1., 1., 1., 1.], [ 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.], [ 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([[-2.74761648, -3.81873152, 8.5612057 , -6.50432488], [ 4.04987279, -9.37231031, -7.96381121, 0.4987925 ], [-5.86088041, 7.90005728, 7.30370647, 8.57564479], [ 9.09765827, 2.85531062, 9.41611209, -8.07463238]]) >>> for i in range(len(A)): ... A[i,:]=np.random.uniform(-10,10,len(A)) ...
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 [ 0.70710678 -0.70710678] -1.0 [ 0.70710678 0.70710678] >>>
Numpy objects >>> A+A.T array([[ -9.87072296, -8.6819582 , 1.05438839, 0.77621665], [ -8.6819582 , -6.20131579, 12.1133606 , -4.29184743], [ 1.05438839, 12.1133606 , 13.24488407, -7.21060701], [ 0.77621665, -4.29184743, -7.21060701, 19.57503177]]) >>> A=A+A.T >>> evals,evecs=np.linalg.eig(A) >>> evals array([-19.38912856, -2.79317213, 27.87492659, 11.05525118])
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)
Numpy objects >>> print np.sum(it), it.size 50.0 10 >>> print np.mean(it), np.sum(it)/it.size 5.0 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. ])) >>>
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([-1.02909335, 2.77430392, -1.00094332]) >>> >>> np.std(D) 5.8341355973553766 >>> np.std(D,axis=0) array([ 6.37529467, 5.64920856, 4.46994818]) >>> np.var(D,axis=0) array([ 40.64438211, 31.91355732, 19.98043675]) >>> np.sqrt(np.var(D,axis=0)) array([ 6.37529467, 5.64920856, 4.46994818]) >>>
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([[ 1.66666667, 1.66666667], [ 1.66666667, 1.66666667]]) >>> np.cov(a,b) array([[ 1.66666667, 1.66666667], [ 1.66666667, 1.66666667]]) >>> np.cov(a,c) array([[ 1.66666667, -1.66666667], [-1.66666667, 1.66666667]]) >>> np.cov(a,d) array([[ 1.66666667, -0.01641726], [-0.01641726, 0.0033877 ]])