TypechoJoeTheme

IT技术分享

统计

Numpy 矩阵处理代码示例 ——Python中科学计算库

2017-11-24
/
0 评论
/
704 阅读
/
正在检测是否收录...
11/24

numpy 初始化

zeros—初始为0

matrix = numpy.zeros((3,4))
print(matrix)
print(matrix.dtype)

*[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
——————————————————————————————————————————
float64*

ones—初始化为1

matrix = numpy.ones((3,4),dtype = numpy.int32)
print(matrix)
print(matrix.dtype)

*[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
——————————————————————————————————————————
int32*

arange—按范围和间隔初始化

matrix = numpy.arange(10, 50, 3)
print(matrix)

[10 13 16 19 22 25 28 31 34 37 40 43 46 49]

linspace—按范围平均取值

vector = numpy.linspace(1,10,20)
print(vector)

*[ 1. 1.47368421 1.94736842
2.42105263 2.89473684 3.36842105
3.84210526 4.31578947 4.78947368
5.26315789 5.73684211 6.21052632
6.68421053 7.15789474 7.63157895
8.10526316 8.57894737 9.05263158
9.52631579 10. ]*

random—随机初始化

matrix = numpy.random.random((2,3))
print(matrix)

*[[ 0.97707052 0.320353 0.64360654]
[ 0.64548695 0.01896411 0.71735917]]*

numpy 属性

shape—查看(改变)行列值

vector = numpy.array([1,2,3,4,5])
print(vector.shape)
matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(matrix.shape)
matrix.shape=(2,6)
print(matrix)

*(5,)
——————————————————————————————————————————
(4, 3)
——————————————————————————————————————————
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]*

ndim—查看维度

matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix.ndim)

2

dtype—查看元素类型

matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix.dtype.name)

int32

size—查看元素个数

matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix.size)

9

numpy 方法

array—List转数组

List转Vector

vector = numpy.array([1,2,3,4,5])
print(vector)
print(type(vector))
print(vector.dtype)
print(vector[0:3])
print(vector == 4)
print(vector[vector==5])

*[1 2 3 4 5]

int32
[1 2 3]
[False False False True False]
[5]*

List转Matrix

matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)
print(type(matrix))
print(matrix.dtype)
print(matrix == 8)

*[[1 2 3]
[4 5 6]
[7 8 9]]
——————————————————————————————————————————

int32
——————————————————————————————————————————
[[False False False]
[False False False]
[False True False]]*

reshape—将向量变为矩阵

matrix = numpy.arange(18).reshape(3,6)
print(matrix)

*[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]]*

genfromtxt—文件操作

text = numpy.genfromtxt("test.txt", delimiter=",", dtype=str, skip_header=1)
print(text)
print(text.dtype)
print(text[:,1:3])
print(text[1,2])

*[['1' 'a' '111']
['2' 'b' '222']
['3' 'c' '333']
['4' 'd' '444']]
——————————————————————————————————————————
<U3
——————————————————————————————————————————
[['a' '111']
['b' '222']
['c' '333']
['d' '444']]
——————————————————————————————————————————
222*

astype—类型转换

vector = numpy.array([1,2,3,4,5])
print(vector)
print(vector.dtype)
vector = vector.astype(str)
print(vector)
print(vector.dtype)

*[1 2 3 4 5]
int32
['1' '2' '3' '4' '5']
<U11*

max,min—最大最小

vector = numpy.array([1,2,3,4,5])
print(vector.min())
print(vector.max())

*1
5*

sum—求和

matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix.sum(axis=1)) #按行求和
print(matrix.sum(axis=0)) #按列求和

*[ 6 15 24]
[12 15 18]*

dot—矩阵的乘法

A = numpy.array([[1,1],[0,1]])
B = numpy.array([[2,0],[3,4]])
print(A * B)
print(A.dot(B))
print(numpy.dot(A, B))

*[[2 0]
[0 4]]
——————————————————————————————————————————
[[5 4]
[3 4]]
——————————————————————————————————————————
[[5 4]
[3 4]]*

exp—求e的指数

B = numpy.arange(3)
print(B)
print(numpy.exp(B))

*[0 1 2]
[ 1. 2.71828183 7.3890561 ]*

sqrt—开平方

B = numpy.arange(3)
print(B)
print(numpy.sqrt(B))

*[0 1 2]
[ 0. 1. 1.41421356]*

floor—向下取整

a = numpy.floor(10*numpy.random.random((3,4)))
print(a)

*[[ 9. 8. 0. 1.]
[ 4. 6. 7. 5.]
[ 0. 1. 7. 2.]]*

ravel—把矩阵撑长为向量

a = numpy.floor(10*numpy.random.random((3,4)))
print(a)
b= a.ravel()
print(b)

*[[ 1. 3. 5. 6.]
[ 7. 1. 9. 8.]
[ 7. 4. 3. 8.]]
——————————————————————————————————————————
[ 1. 3. 5. 6. 7. 1. 9. 8. 7. 4. 3. 8.]*

T—转置

a = numpy.floor(10*numpy.random.random((3,4)))
print(a.T)

*[[ 6. 4. 7.]
[ 0. 8. 2.]
[ 5. 6. 0.]
[ 4. 1. 2.]]*

vstack—矩阵行连接

a = numpy.floor(10*numpy.random.random((3,4)))
b = numpy.floor(10*numpy.random.random((3,4)))
print(a)
print("====================================================")
print(b)
print("====================================================")
print(numpy.vstack((a,b)))

*[[ 3. 9. 1. 5.]
[ 5. 4. 6. 1.]
[ 6. 5. 2. 2.]]
——————————————————————————————————————————
[[ 8. 1. 3. 0.]
[ 3. 5. 9. 9.]
[ 1. 5. 9. 9.]]
——————————————————————————————————————————
[[ 3. 9. 1. 5.]
[ 5. 4. 6. 1.]
[ 6. 5. 2. 2.]
[ 8. 1. 3. 0.]
[ 3. 5. 9. 9.]
[ 1. 5. 9. 9.]]*

hstack—矩阵列连接

a = numpy.floor(10*numpy.random.random((3,4)))
b = numpy.floor(10*numpy.random.random((3,4)))
print(a)
print("====================================================")
print(b)
print("====================================================")
print(numpy.hstack((a,b)))

*[[ 7. 7. 1. 0.]
[ 3. 9. 4. 8.]
[ 6. 1. 7. 4.]]
——————————————————————————————————————————
[[ 4. 2. 7. 3.]
[ 1. 0. 9. 5.]
[ 6. 4. 6. 1.]]
——————————————————————————————————————————
[[ 7. 7. 1. 0. 4. 2. 7. 3.]
[ 3. 9. 4. 8. 1. 0. 9. 5.]
[ 6. 1. 7. 4. 6. 4. 6. 1.]]*

vsplit—按行进行切分

A = numpy.floor(10*numpy.random.random((4,12)))
print(A)
print("====================================================")
print(numpy.vsplit(A, 2))
print("====================================================")
print(numpy.vsplit(A, (2,3)))

*[[ 4. 2. 7. 9. 1. 7. 8. 6. 7. 3. 9. 0.]
[ 9. 7. 1. 0. 2. 0. 2. 3. 1. 8. 3. 7.]
[ 4. 1. 2. 5. 0. 2. 4. 1. 3. 6. 7. 2.]
[ 7. 4. 0. 2. 1. 7. 8. 2. 1. 1. 1. 0.]]
——————————————————————————————————————————
[array([[ 4., 2., 7., 9., 1., 7., 8., 6., 7., 3., 9., 0.],

   [ 9.,  7.,  1.,  0.,  2.,  0.,  2.,  3.,  1.,  8.,  3.,  7.]]),  
    array([[ 4.,  1.,  2.,  5.,  0.,  2.,  4.,  1.,  3.,  6.,  7.,  2.],  
   [ 7.,  4.,  0.,  2.,  1.,  7.,  8.,  2.,  1.,  1.,  1.,  0.]])]  

——————————————————————————————————————————
[array([[ 4., 2., 7., 9., 1., 7., 8., 6., 7., 3., 9., 0.],

   [ 9.,  7.,  1.,  0.,  2.,  0.,  2.,  3.,  1.,  8.,  3.,  7.]]),   
   array([[ 4.,  1.,  2.,  5.,  0.,  2.,  4.,  1.,  3.,  6.,  7.,  2.]]),   
   array([[ 7.,  4.,  0.,  2.,  1.,  7.,  8.,  2.,  1.,  1.,  1.,  0.]])]*  

hsplit—按列进行切分

A = numpy.floor(10*numpy.random.random((4,12)))
print(A)
print(numpy.hsplit(A, 2))
print("====================================================")
print(numpy.hsplit(A, (2,3,5)))

*[[ 9. 9. 6. 0. 6. 4. 6. 4. 7. 9. 9. 1.]
[ 6. 5. 7. 5. 6. 4. 1. 9. 4. 4. 4. 2.]
[ 0. 8. 0. 3. 5. 6. 1. 0. 6. 7. 8. 2.]
[ 7. 9. 8. 2. 7. 6. 8. 5. 1. 5. 9. 3.]]
——————————————————————————————————————————
[array([[ 9., 9., 6., 0., 6., 4.],

   [ 6.,  5.,  7.,  5.,  6.,  4.],  
   [ 0.,  8.,  0.,  3.,  5.,  6.],  
   [ 7.,  9.,  8.,  2.,  7.,  6.]]),   
   array([[ 6.,  4.,  7.,  9.,  9.,  1.],  
   [ 1.,  9.,  4.,  4.,  4.,  2.],  
   [ 1.,  0.,  6.,  7.,  8.,  2.],  
   [ 8.,  5.,  1.,  5.,  9.,  3.]])]  

——————————————————————————————————————————
[array([[ 9., 9.],

   [ 6.,  5.],  
   [ 0.,  8.],  
   [ 7.,  9.]]),   
   array([[ 6.],  
   [ 7.],  
   [ 0.],  
   [ 8.]]),   
   array([[ 0.,  6.],  
   [ 5.,  6.],  
   [ 3.,  5.],  
   [ 2.,  7.]]),   
   array([[ 4.,  6.,  4.,  7.,  9.,  9.,  1.],  
   [ 4.,  1.,  9.,  4.,  4.,  4.,  2.],  
   [ 6.,  1.,  0.,  6.,  7.,  8.,  2.],  
   [ 6.,  8.,  5.,  1.,  5.,  9.,  3.]])]*  

view—创建视图(数据共用)

A = numpy.arange(12)
print(A)
B = A.view()
print(B is A)
B.shape = (2,6)
print(A)
print("====================================================")
print(B)
print("====================================================")
B[0,4] = 22
print(A)
print("====================================================")
print(B)
print("====================================================")
print(id(A))
print(id(B))

*[ 0 1 2 3 4 5 6 7 8 9 10 11]
False
[ 0 1 2 3 4 5 6 7 8 9 10 11]
——————————————————————————————————————————
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
——————————————————————————————————————————
[ 0 1 2 3 22 5 6 7 8 9 10 11]
——————————————————————————————————————————
[[ 0 1 2 3 22 5]
[ 6 7 8 9 10 11]]
——————————————————————————————————————————
2136918279952
2136918228304*

copy—浅复制

A = numpy.arange(12)
print(A)
B = A.copy()
print(B is A)
B.shape = (2,6)
print(A)
print("====================================================")
print(B)
print("====================================================")
B[0,4] = 22
print(A)
print("====================================================")
print(B)
print("====================================================")
print(id(A))
print(id(B))

*[ 0 1 2 3 4 5 6 7 8 9 10 11]
False
[ 0 1 2 3 4 5 6 7 8 9 10 11]
——————————————————————————————————————————
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
——————————————————————————————————————————
[ 0 1 2 3 4 5 6 7 8 9 10 11]
——————————————————————————————————————————
[[ 0 1 2 3 22 5]
[ 6 7 8 9 10 11]]
——————————————————————————————————————————
2136918228304
2136918280512*

argmax—返回最大值的索引

A = numpy.sin(numpy.arange(20)).reshape(5,4)
print(A)
print("====================================================")
index = A.argmax(axis=0)
print(index)
print("====================================================")
max_data = A[index, range(A.shape[1])]
print(max_data)

*[[ 0. 0.84147098 0.90929743 0.14112001]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.28790332 -0.96139749 -0.75098725 0.14987721]]
——————————————————————————————————————————
[2 0 3 1]
——————————————————————————————————————————
[ 0.98935825 0.84147098 0.99060736 0.6569866 ]*

tile—将矩阵平铺

A = numpy.arange(0,50,10)
print(A)
B = numpy.tile(A,(5,2))
print(B)

*[ 0 10 20 30 40]
——————————————————————————————————————————
[[ 0 10 20 30 40 0 10 20 30 40]
[ 0 10 20 30 40 0 10 20 30 40]
[ 0 10 20 30 40 0 10 20 30 40]
[ 0 10 20 30 40 0 10 20 30 40]
[ 0 10 20 30 40 0 10 20 30 40]]*

sort—排序

A = numpy.array([[4,3,5],[1,2,1]])
B = numpy.sort(A, axis = 1)
print(B)

*[[3 4 5]
[1 1 2]]*

argsort—获取排序后原索引

A = numpy.array([4,2,5,6])
print(numpy.sort(A))
B = numpy.argsort(A)
print(B)

*[2 4 5 6]
[1 0 2 3]*

朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/2839/(转载时请注明本文出处及文章链接)