[转载] Python学习之Numpy

参考链接: Python中的numpy.triu

a1=[1,2,3,4]

a1+1 

TypeError                                 Traceback (most recent call last)

<ipython-input-11-a66fed7b98b2> in <module>()

      1 a1=[1,2,3,4]

----> 2 a1+1

TypeError: can only concatenate list (not "int") to list 

import numpy as np

a=np.array([1,2,3,4])

a+1

a+[1,2,3,4]

a+[1]

a*2

b=np.array([[1,2],[3,4]])

b+2

a+[1,2] 

array([2, 3, 4, 5])

array([2, 4, 6, 8])

array([2, 3, 4, 5])

array([2, 4, 6, 8])

array([[3, 4],

       [5, 6]])

ValueError                                Traceback (most recent call last)

<ipython-input-15-e36b81d20138> in <module>()

      1 import numpy as np

      2 a=np.array([1,2,3,4])

----> 3 a+[1,2]

ValueError: operands could not be broadcast together with shapes (4,) (2,)  

2**a

a**2 

array([ 2,  4,  8, 16])

array([ 1,  4,  9, 16]) 

'''

ones(shape[,dtype,order])

根据形状和类型返回一个新的元素全部为1的数组

shape:int或者int元组  定义返回数组的形状,形如:(2,3)或者2

dtype:数据类型,可选  返回数组的数据类型,例如:numpy:int8,默认numpy:float64

'''

b=np.ones(4)+1

b

a-b

np.ones(4,dtype=np.int)

np.ones((2,3))

a*b 

array([ 2.,  2.,  2.,  2.])

array([-1.,  0.,  1.,  2.])

array([1, 1, 1, 1])

array([[ 1.,  1.,  1.],

       [ 1.,  1.,  1.]])

array([ 2.,  4.,  6.,  8.])        

j=np.arange(5)

j

2**(j+1)-j 

array([0, 1, 2, 3, 4])

array([ 2,  3,  6, 13, 28]) 

a=np.arange(10000)

'''

%取模运算  格式化输出

'''

%timeit a+1

np.arange(1,5)

#第一个值为起始点,第二个值为终止点,第三个值为步长

np.arange(1,10,2) 

The slowest run took 5.33 times longer than the fastest. This could mean that an intermediate result is being cached.

100000 loops, best of 3: 8.08 µs per loop

array([1, 2, 3, 4])

array([1, 3, 5, 7, 9]) 

l=range(10000)

%timeit[i+1 for i in l] 

1000 loops, best of 3: 360 µs per loop 

range(5)

range(1,5)

range(1,10,2) 

[0, 1, 2, 3, 4]

[1, 2, 3, 4]

[1, 3, 5, 7, 9] 

c=np.ones((3,3))

c*c#不是矩阵乘法 

array([[ 1.,  1.,  1.],

       [ 1.,  1.,  1.],

       [ 1.,  1.,  1.]]) 

c.dot(c) 

array([[ 3.,  3.,  3.],

       [ 3.,  3.,  3.],

       [ 3.,  3.,  3.]]) 

a=np.array([1,2,3,4])

b=np.array([4,2,2,4])

a==b

a>b

a=np.array([1,1,0,0],dtype=bool)

b=np.array([1,0,1,0],dtype=bool)

np.logical_or(a,b)

np.logical_and(a,b)

a=np.arange(10)

np.sin(a)

np.log(a)

np.log10(a)

np.exp(a) 

array([False,  True, False,  True], dtype=bool)

array([False, False,  True, False], dtype=bool)

array([ True,  True,  True, False], dtype=bool)

array([ True, False, False, False], dtype=bool)

array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,

       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])

array([       -inf,  0.        ,  0.69314718,  1.09861229,  1.38629436,

        1.60943791,  1.79175947,  1.94591015,  2.07944154,  2.19722458])    

array([       -inf,  0.        ,  0.30103   ,  0.47712125,  0.60205999,

        0.69897   ,  0.77815125,  0.84509804,  0.90308999,  0.95424251])         

array([  1.00000000e+00,   2.71828183e+00,   7.38905610e+00,

         2.00855369e+01,   5.45981500e+01,   1.48413159e+02,

         4.03428793e+02,   1.09663316e+03,   2.98095799e+03,

         8.10308393e+03])           

a=np.arange(4)

print a

a.reshape(4,1)+np.array([2,3,5]) 

[0 1 2 3]

Out[81]:

array([[2, 3, 5],

       [3, 4, 6],

       [4, 5, 7],

       [5, 6, 8]]) 

a=np.triu(np.ones((3,3)),-1)

a

a=np.triu(np.ones((3,3)),0)

a

a=np.triu(np.ones((3,3)),1)

a

a.T 

array([[ 1.,  1.,  1.],

       [ 1.,  1.,  1.],

       [ 0.,  1.,  1.]])

array([[ 1.,  1.,  1.],

       [ 0.,  1.,  1.],

       [ 0.,  0.,  1.]])  

array([[ 0.,  1.,  1.],

       [ 0.,  0.,  1.],

       [ 0.,  0.,  0.]])      

array([[ 0.,  0.,  0.],

       [ 1.,  0.,  0.],

       [ 1.,  1.,  0.]])              

a=np.array([1,2,3,4])

b=np.array([2,3,4,5])

c=np.array([1,2,3,4])

#数组间的比较

np.array_equal(a,b)

np.array_equal(a,c) 

False

True 

Linear algebra  The sub-module numpy.linalg implements basic linear algebra, such as solving linear systems, singular value decomposition, etc. However, it is not guaranteed to be compiled using efficient routines, and thus we recommend the use of scipy.linalg, as detailed in section scipy_linalg 

a=np.array([1,2,3,4])

np.sum(a)

a.sum()

a.sum(axis=0)

a.sum(axis=1)

a[:,0].sum(),a[:,1].sum()

a[0,:].sum(),a[1,:].sum() 

10

10

array([3, 3])

array([2, 4])

(3, 3)

(2, 4) 

x=np.random.rand(2,2,2)

x

print x.sum(axis=2)

x.sum(axis=2)[0,1]

x[0,1,:].sum() 

array([[[ 0.18897886,  0.3597148 ],

        [ 0.45634364,  0.97641483]],

       [[ 0.3529697 ,  0.83570539],

        [ 0.0629451 ,  0.96301844]]])

[[ 1.70568465  1.36214561]

 [ 0.50200916  0.98907998]]

1.3621456131271461

1.3621456131271461       

x=np.array([1,2,3,1])

x.mean()

np.median(x)

y=np.array([[1,2,3],[5,6,1]])

np.median(y,axis=-1)#最后一个axis

x.std()

x.min()

x.max()

x.argmin()#the index of min

x.argmax()#the index of max 

1.75

1.5

array([ 2.,  5.])

0.82915619758884995

1

3

0

np.all([True,True,True,False])#是否都为True

np.any([True,False,True]) 

False

True 

可以使用在数组比较 

a=np.zeros((100,100))

np.any(a!=0)

np.all(a==a)

a=np.array([1,2,3,4])

b=np.array([2,3,4,5])

np.all(a>b) 

False

True

False

猜你喜欢

转载自blog.csdn.net/u013946150/article/details/113057797