Title:
1numpy
2Numpy objects
gtgtgt import numpy as np gtgtgt np.array(1,2,3)? arra
y(1, 2, 3)? gtgtgt np.array(1,2,3.0)? array(
1., 2., 3.)? gtgtgt np.array(1,2,3,np.float64)?
array( 1., 2., 3.)? gtgtgt np.array(range(3)
for x in range(4),np.float64)? array( 0., 1.,
2., 0., 1., 2., 0., 1.,
2., 0., 1., 2.)? gtgtgt
3Numpy objects
gtgtgt A np.array(range(3) for x in
range(4),np.float64)? gtgtgt np.matrix(range(3)
for x in range(4),np.float64)? matrix( 0.,
1., 2., 0., 1., 2., 0.,
1., 2., 0., 1., 2.)? gtgtgt M
np.matrix(range(3) for x in range(4),np.float64)
? gtgtgt type(A), type(M)? (lttype 'numpy.ndarray'gt,
ltclass 'numpy.core.defmatrix.matrix'gt)? gtgtgt
np.matrix(range(3),np.float64)? matrix( 0.,
1., 2.)? gtgtgt np.array(range(3),np.float64)? arr
ay( 0., 1., 2.)? gtgtgt Mnp.matrix(range(3),np.f
loat64)? gtgtgt Anp.array(range(3),np.float64)? gtgtgt
A.shape (3,)? gtgtgt M.shape (1, 3)?
4Numpy objects
gtgtgt Mnp.matrix(range(3),np.float64)? gtgtgt
Anp.array(range(3),np.float64)? gtgtgt A.T array(
0., 1., 2.)? gtgtgt M.T matrix( 0.,
1., 2.)? gtgtgt Anp.array(1,2,2,-2
,np.float64)? gtgtgt Mnp.matrix(1,2,2,-2,np.f
loat64)? gtgtgt M.T matrix( 1., 2.,
2., -2.)? gtgtgt A.T array( 1., 2.,
2., -2.)? gtgtgt A.I Traceback (most recent call
last) File "ltstdingt", line 1, in
ltmodulegt AttributeError 'numpy.ndarray' object
has no attribute 'I' gtgtgt M.I matrix(
0.33333333, 0.33333333, 0.33333333,
-0.16666667)? gtgtgt
5Numpy objects
gtgtgt M matrix( 1., 2., 2.,
-2.)? gtgtgt M.I matrix( 0.33333333,
0.33333333, 0.33333333,
-0.16666667)? gtgtgt np.linalg.inv(A)? array(
0.33333333, 0.33333333, 0.33333333,
-0.16666667)? gtgtgt gtgtgt M2np.matrix(1,1,0,0
,np.float64)? gtgtgt A2np.array(1,1,0,0,np.fl
oat64)? gtgtgt MM2 matrix( 1., 1.,
2., 2.)? gtgtgt AA2 array( 1., 2.,
0., -0.)? gtgtgt A2A array( 1., 2.,
0., -0.)? gtgtgt M2M matrix( 3., 0.,
0., 0.)?
6Numpy objects
gtgtgt A M matrix( True, True,
True, True, dtypebool)? gtgtgt
Ainp.linalg.inv(A)? gtgtgt np.dot(A,Ai)? array(
1.00000000e00, 5.55111512e-17,
0.00000000e00, 1.00000000e00)? gtgtgt
np.dot(Ai,A)? array( 1.00000000e00,
-1.11022302e-16, 0.00000000e00,
1.00000000e00)? gtgtgt MM.I matrix(
1.00000000e00, 5.55111512e-17,
0.00000000e00, 1.00000000e00)? gtgtgt
M.IM matrix( 1.00000000e00,
-1.11022302e-16, 0.00000000e00,
1.00000000e00)?
7Numpy objects
gtgtgt np.random.uniform()? 0.70420105296872415 gtgtgt
np.random.uniform(-0.1,0.1)? -0.059314492060574403
gtgtgt np.random.uniform(-0.1,0.1,4)? array(
0.06606591, 0.07766084, 0.08635536,
0.08010191)? gtgtgt np.random.uniform(-0.1,0.1,(2,2)
)? array(-0.06448909, 0.07906606,
-0.04752628, -0.02955906)? gtgtgt
np.random.normal(-1,1,(3,2))? array(-0.32922971,
-0.05700329, -2.81944081,
0.43708656, -1.4274894 ,
-0.61651697)? gtgtgt gtgtgt np.sort(1,5.9,2,-1)? ar
ray(-1. , 1. , 2. , 5.9)? gtgtgt
np.argsort(1,5.9,2,-1)? array(3, 0, 2,
1)? gtgtgt
8Numpy objects
gtgtgtnp.ones((4,4))? array( 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1.)? gtgtgt
np.zeros((4,4))? array( 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0.)? gtgtgt
Anp.zeros((4,4))? gtgtgt for i in
range(len(A)) ... Ainp.random.uniform(-
10,10,len(A))? ... gtgtgt 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)? gtgtgt for i in
range(len(A)) ... Ai,np.random.uniform(-1
0,10,len(A))? ...
9Numpy objects
gtgtgt B array( 0., 1., 1.,
0.)? gtgtgt evals,evecsnp.linalg.eig(B)? gtgtgt
evals array( 1., -1.)? gtgtgt for i in
range(len(B)) ... print evalsi,evecsi ...
1.0 0.70710678 -0.70710678 -1.0 0.70710678
0.70710678 gtgtgt
10Numpy objects
gtgtgt AA.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)? gtgtgt AAA.T gtgtgt
evals,evecsnp.linalg.eig(A)? gtgtgt
evals array(-19.38912856, -2.79317213,
27.87492659, 11.05525118)?
11Numpy objects
gtgtgt inputsnp.array( 1., 2., 3., 4.,
5.)? gtgtgt targetsnp.array( 3., 5.,
7., 9., 11.)? gtgtgt np.concatenate((inputs,ta
rgets))? array( 1., 2.,
3., 4., 5.,
3., 5., 7.,
9., 11.)? gtgtgt np.concatenate((inputs,t
argets),axis1)? array( 1., 3.,
2., 5., 3., 7., 4.,
9., 5., 11.)? gtgtgt
itnp.concatenate((inputs,targets),axis1)?
12Numpy objects
gtgtgt print np.sum(it), it.size 50.0 10 gtgtgt print
np.mean(it), np.sum(it)/it.size 5.0 5.0 gtgtgt
np.sum(it,axis0), np.sum(it,axis1)? (array(
15., 35.), array( 4., 7., 10., 13.,
16.))? gtgtgt len(it), len(it0)? (5, 2)? gtgtgt
np.mean(it,axis0),np.mean(it,axis1)? (array(
3., 7.), array( 2. , 3.5, 5. , 6.5, 8.
))? gtgtgt
13Numpy objects
gtgtgt Dnp.zeros((20,3))? gtgtgt for i in
range(len(D)) ... Di,np.random.unifor
m(-10,10,len(Di))? gtgtgt D.mean(axis0)? array(-
1.02909335, 2.77430392, -1.00094332)? gtgtgt gtgtgt
np.std(D)? 5.8341355973553766 gtgtgt
np.std(D,axis0)? array( 6.37529467,
5.64920856, 4.46994818)? gtgtgt np.var(D,axis0)? a
rray( 40.64438211, 31.91355732,
19.98043675)? gtgtgt np.sqrt(np.var(D,axis0))? arra
y( 6.37529467, 5.64920856, 4.46994818)? gtgtgt
14Numpy objects
gtgtgt r0.1 gtgtgt anp.array(range(4),np.float)np.ran
dom.uniform(-r,r)? gtgtgt bnp.array(range(4),np.floa
t)np.random.uniform(-r,r) -5 gtgtgt
cnp.array(range(4,0,-1),np.float)np.random.unifo
rm(-r,r) -5 gtgtgt dnp.matrix(np.random.uniform(-r,
r) for i in range(4),np.float)? gtgtgt
np.cov(a,a)? array( 1.66666667, 1.66666667,
1.66666667, 1.66666667)? gtgtgt
np.cov(a,b)? array( 1.66666667, 1.66666667,
1.66666667, 1.66666667)? gtgtgt
np.cov(a,c)? array( 1.66666667, -1.66666667,
-1.66666667, 1.66666667)? gtgtgt
np.cov(a,d)? array( 1.66666667, -0.01641726,
-0.01641726, 0.0033877 )?