python——Numpy 模組學習
1.Numpy簡單建立陣列
import numpy as np
# 建立簡單的列表
a = [1, 2, 3, 4]
# 將列表轉換為陣列
b = np.array(b)
import numpy
a = numpy.array([1,2,3,4,5])
print a[1] #2
b = numpy.array([1,2,3,4,5],float)
print b[1] #2.0
2.Numpy 陣列的檢視
陣列元素個數
b.size
陣列形狀
b.shape的用法
(a). Using shape to get array dimensions
import numpy
my__1D_array = numpy.array([1, 2, 3, 4, 5])
print my_1D_array.shape #(5,) -> 5 rows and 0 columns
my__2D_array = numpy.array([[1, 2],[3, 4],[6,5]])
print my_2D_array.shape #(3, 2) -> 3 rows and 2 columns
(b). Using shape to change array dimensions
import numpy change_array = numpy.array([1,2,3,4,5,6]) change_array.shape = (3, 2) print change_array #Output [[1 2] [3 4] [5 6]]
陣列維度
b.ndim
陣列元素型別
b.dtype
3.numpy矩陣的轉置和鋪展操作
Transpose and Flatten
轉置:transpose 不會改變原來的陣列,會生成一個新的陣列
import numpy my_array = numpy.array([[1,2,3], [4,5,6]]) print numpy.transpose(my_array) #Output [[1 4] [2 5] [3 6]]
鋪展:flatten
import numpy
my_array = numpy.array([[1,2,3],
[4,5,6]])
print my_array.flatten()
#Output
[1 2 3 4 5 6]
4.concatenate 連線numpy陣列的用法
import numpy
n,m,t = map(int,input().split())
arr1 = numpy.array([input().split() for _ in range(n)],int)
arr2 = numpy.array([input().split() for _ in range(m)],int)
print (numpy.concatenate((arr1,arr2)))
5.zeros和ones生成numpy樣式陣列
The zeros tool returns a new array with a given shape and type filled with 0 's.
預設元素為float
import numpy
print numpy.zeros((1,2)) #Default type is float
#Output : [[ 0. 0.]]
print numpy.zeros((1,2), dtype = numpy.int) #Type changes to int
#Output : [[0 0]]
The ones tool returns a new array with a given shape and type filled with 1's.
預設元素為float
import numpy
print numpy.ones((1,2)) #Default type is float
#Output : [[ 1. 1.]]
print numpy.ones((1,2), dtype = numpy.int) #Type changes to int
#Output : [[1 1]]
6.eye的用法
The eye tool returns a 2-D array with 1's as the diagonal and 0's elsewhere. The diagonal can be main, upper or lower depending on the optional parameter k. A positive is for the upper diagonal, a negative k is for the lower, and a k (default) is for the main diagonal.
import numpy
print numpy.eye(8, 7, k = 1) # 8 X 7 Dimensional array with first upper diagonal 1.
#Output
[[ 0. 1. 0. 0. 0. 0. 0.]
[ 0. 0. 1. 0. 0. 0. 0.]
[ 0. 0. 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 0. 0. 1.]
[ 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0.]]
print numpy.eye(8, 7, k = -2) # 8 X 7 Dimensional array with second lower diagonal 1.
7.numpy 矩陣的算術運算
import numpy
n,m=map(int,input().split())
a,b= (numpy.array([input().split() for _ in range(n)],int) for i in range(2))
print(a+b,a-b,a*b,a//b,a%b,a**b,sep='\n')
import numpy
a = numpy.array([1,2,3,4], float)
b = numpy.array([5,6,7,8], float)
print a + b #[ 6. 8. 10. 12.]
print numpy.add(a, b) #[ 6. 8. 10. 12.]
print a - b #[-4. -4. -4. -4.]
print numpy.subtract(a, b) #[-4. -4. -4. -4.]
print a * b #[ 5. 12. 21. 32.]
print numpy.multiply(a, b) #[ 5. 12. 21. 32.]
print a / b #[ 0.2 0.33333333 0.42857143 0.5 ]
print numpy.divide(a, b) #[ 0.2 0.33333333 0.42857143 0.5 ]
print a % b #[ 1. 2. 3. 4.]
print numpy.mod(a, b) #[ 1. 2. 3. 4.]
print a**b #[ 1.00000000e+00 6.40000000e+01 2.18700000e+03 6.55360000e+04]
print numpy.power(a, b) #[ 1.00000000e+00 6.40000000e+01 2.18700000e+03 6.55360000e+04]
8.Floor, Ceil and Rint 操作。numpy陣列取上整,下整,最接近的數
import numpy
a =numpy.array(list(map(float,input().split())))
print(numpy.floor(a),numpy.ceil(a),numpy.rint(a),sep='\n')
輸入值:1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
輸出值:
[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 2. 3. 4. 5. 6. 7. 8. 9. 10.]
[ 1. 2. 3. 4. 6. 7. 8. 9. 10.]
9.Sum and Prod 陣列行和列的加乘
axis =0表示列維度, axis=1表示行維度
The sum tool returns the sum of array elements over a given axis.
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print numpy.sum(my_array, axis = 0) #Output : [4 6]
print numpy.sum(my_array, axis = 1) #Output : [3 7]
print numpy.sum(my_array, axis = None) #Output : 10
print numpy.sum(my_array) #Output : 10
The prod tool returns the product of array elements over a given axis.
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print numpy.prod(my_array, axis = 0) #Output : [3 8]
print numpy.prod(my_array, axis = 1) #Output : [ 2 12]
print numpy.prod(my_array, axis = None) #Output : 24
print numpy.prod(my_array) #Output : 24
10. min and max的用法, 返回一個numpy or 一個值
The tool min returns the minimum value along a given axis.
import numpy
my_array = numpy.array([[2, 5],
[3, 7],
[1, 3],
[4, 0]])
print numpy.min(my_array, axis = 0) #Output : [1 0]
print numpy.min(my_array, axis = 1) #Output : [2 3 1 0]
print numpy.min(my_array, axis = None) #Output : 0
print numpy.min(my_array) #Output : 0
The tool max returns the maximum value along a given axis.
import numpy
my_array = numpy.array([[2, 5],
[3, 7],
[1, 3],
[4, 0]])
print numpy.max(my_array, axis = 0) #Output : [4 7]
print numpy.max(my_array, axis = 1) #Output : [5 7 3 4]
print numpy.max(my_array, axis = None) #Output : 7
print numpy.max(my_array) #Output : 7
11.Mean, Var, and Std 平均值,方差,標準差的計算
The mean tool computes the arithmetic mean along the specified axis.
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print numpy.mean(my_array, axis = 0) #Output : [ 2. 3.]
print numpy.mean(my_array, axis = 1) #Output : [ 1.5 3.5]
print numpy.mean(my_array, axis = None) #Output : 2.5
print numpy.mean(my_array) #Output : 2.5
The var tool computes the arithmetic variance along the specified axis.
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print numpy.var(my_array, axis = 0) #Output : [ 1. 1.]
print numpy.var(my_array, axis = 1) #Output : [ 0.25 0.25]
print numpy.var(my_array, axis = None) #Output : 1.25
print numpy.var(my_array) #Output : 1.25
The std tool computes the arithmetic standard deviation along the specified axis.
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print numpy.std(my_array, axis = 0) #Output : [ 1. 1.]
print numpy.std(my_array, axis = 1) #Output : [ 0.5 0.5]
print numpy.std(my_array, axis = None) #Output : 1.11803398875
print numpy.std(my_array) #Output : 1.11803398875
12. Dot and cross numpy向量點積和差積的計算
The dot tool returns the dot product of two arrays.
import numpy
A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])
print numpy.dot(A, B) #Output : 11
The cross tool returns the cross product of two arrays.
import numpy
A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])
print numpy.cross(A, B) #Output : -2
13.inner product 和outter product
Outer Product是線性代數中的外積( WikiPedia: Outer Product )。也就是張量積 :
The inner tool returns the inner product of two arrays.
import numpy
A = numpy.array([0, 1])
B = numpy.array([3, 4])
print numpy.inner(A, B) #Output : 4
The outer tool returns the outer product of two arrays.
import numpy
A = numpy.array([0, 1])
B = numpy.array([3, 4])
print numpy.outer(A, B) #Output : [[0 0]
# [3 4]]