Basic use of numpy

#-*- coding:utf-8 -*-
import numpy as np
'''
Numpy: The bottom layer is implemented by C language, so it is faster.
'''

#1.numpy常用函数
l1=np.array([1,2,3,4,5,6])
print(l1,type(l1),l1.shape)
print(l1.sum())
print(l1.max())
print(l1.min())
print(np.average(l1))

l1_ =l1[1:3] #Slice print (l1_ )




l2=np.array([[1,2,3],[4,5,6]])
print(l2,type(l2),l2.shape)
l2=l1.reshape(2,3)
print(l2,type(l2),l2.shape)

l2_ =l2[:1,:2] #Slice print (l2_ )


# 2. Broadcast 
l3=l2+3 #Same for other operations 
print (l3)
l3 = l3+ l2
 print (l3)

l3 = np.exp(l2)
 print (l3)

# 3. Vectorized calculation 
'''
1 Try not to use for loops
2 Try to use numpy's own functions
3 Use radio more
'''
l4=np.array([[1,2,3],[4,5,6]])
l5 =np.array([[10,11,12,13],[14,15,16,17],[ 18,19,20,21 ]])
 print (np.dot(l4,l5))
 
# The calculation of the above formula 
yhat=np.array([0.9,0.2,0.1,0.4,0.9 ])
y =np.array([1,0,0,1,1 ])
l6 = np.sum (np.abs (y- yhat))
l7=np.dot((y-yhat),(y-yhat))
print(l6,l7)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325284918&siteId=291194637