Numpy通用函数

数组形状
.T / .reshape() / .resize()

.T

转置

import numpy as np
ar1 = np.arange(10)
ar2 = np.zeros((2,5))
print(ar1)
print(ar2)
print(ar1.T)
print(ar2.T)

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

.reshape()

直接将已有数组的形状改变

import numpy as np
ar1 = np.arange(16)
print(ar1.reshape(2,8))

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

生成数组后直接改变形状
import numpy as np
ar1 = np.zeros((4,6)).reshape(3,8)
print(ar1)

[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]

参数内添加数组,目标形状
import numpy as np
ar1 = np.reshape(np.arange(12), (3,4))
print(ar1)

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

.resize()

返回具有指定形状的新数组,如有必要可重复填充所需数量的元素

numpy.resize(a, new)

import numpy as np
print(np.resize(np.arange(5),(3,4)))

[[0 1 2 3]
[4 0 1 2]
[3 4 0 1]]

resize的使用过程中需注意避免的坑,如果s.resize,改变的是s本身
import numpy as np
s = np.arange(10)
print(s.resize(2,5))
print(np.resize(s,(2,5)))

None
[[0 1 2 3 4]
[5 6 7 8 9]]

import numpy as np
s = np.arange(10)
print(s)
l = s.resize(2,5)
print(s,l)

[0 1 2 3 4 5 6 7 8 9]
[[0 1 2 3 4]
[5 6 7 8 9]] None

数组的复制
import numpy as np
ar1 = np.arange(10)
ar2 = ar1
print(ar1 is ar2)
print("__________________")
ar1[2] = 100
print(ar1, ar2)
print("__________________")
ar3 = ar1.copy()
ar1[3] = 1000
print(ar1, ar3)

True


[ 0 1 100 3 4 5 6 7 8 9] [ 0 1 100 3 4 5 6 7 8 9]


[ 0 1 100 1000 4 5 6 7 8 9] [ 0 1 100 3 4 5 6 7 8 9]

数组类型转换

.astype()

import numpy as np
ar1 = np.arange(10, dtype = float)
ar2 = ar1.astype(np.int64)
print(ar1, ar1.dtype)
print(ar2, ar2.dtype)

[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] float64
[0 1 2 3 4 5 6 7 8 9] int64

数组的堆叠

.hstack()

数组间的横向连接
import numpy as np
a = np.arange(5)
b = np.arange(5,9)
print(a,"______",b)
print(np.hstack((a,b)))

[0 1 2 3 4] ______ [5 6 7 8]
[0 1 2 3 4 5 6 7 8]

.vstack()

数组间的竖向连接
import numpy as np
a = np.array([1,2,3])
b = np.array(["a","b","c"])
print(a)
print(b)
print("________")
print(np.vstack((a,b)))

[1 2 3]
['a' 'b' 'c']


[['1' '2' '3']
['a' 'b' 'c']]

.stack()

通过参数确认连接方式
import numpy as np
a = np.array([1,2,3])
b = np.array(["a","b","c"])
print(a)
print(b)
print("________")
print(np.stack((a,b)))
print("________")
print(np.stack((a,b), axis = 1))

[1 2 3]
['a' 'b' 'c']


[['1' '2' '3']
['a' 'b' 'c']]


[['1' 'a']
['2' 'b']
['3' 'c']]

数组的拆分

.hsplit()

数组间的横向拆分
import numpy as np
ar = np.arange(16).reshape(4,4)
print(ar)
print("___________")
print(np.hsplit(ar, 2))

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


[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]

.vsplit()

数组间的纵向拆分
import numpy as np
ar = np.arange(16).reshape(4,4)
print(ar)
print("___________")
print(np.vsplit(ar, 2))

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


[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]

数组的简单运算

与标量的计算

(1)、加法:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar + 10)

[[10 11 12]
[13 14 15]]

(2)、乘法:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar * 2)

[[ 0 2 4]
[ 6 8 10]]

(3)、除法:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(1 / (ar + 1))

[1 2 3]
['a' 'b' 'c']
________
[['1' '2' '3']
['a' 'b' 'c']]


#.stack()
通过参数确认连接方式
import numpy as np
a = np.array([1,2,3])
b = np.array(["a","b","c"])
print(a)
print(b)
print("________")
print(np.stack((a,b)))
print("________")
print(np.stack((a,b), axis = 1))
>>>
[1 2 3]
['a' 'b' 'c']
________
[['1' '2' '3']
['a' 'b' 'c']]
________
[['1' 'a']
['2' 'b']
['3' 'c']]


数组的拆分
#.hsplit()
数组间的横向拆分
import numpy as np
ar = np.arange(16).reshape(4,4)
print(ar)
print("___________")
print(np.hsplit(ar, 2))
>>>
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
___________
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]


#.vsplit()
数组间的纵向拆分
import numpy as np
ar = np.arange(16).reshape(4,4)
print(ar)
print("___________")
print(np.vsplit(ar, 2))
>>>
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
___________
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]


数组的简单运算
#与标量的计算
(1)、加法:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar + 10)
>>>
[[10 11 12]
[13 14 15]]


(2)、乘法:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar * 2)
>>>
[[ 0 2 4]
[ 6 8 10]]


(3)、除法:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(1 / (ar + 1))
>>>
[[ 1. 0.5 0.33333333]
[ 0.25 0.2 0.16666667]]

(4)、幂:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar ** 5)
arprint()

[[ 0 1 32]
[ 243 1024 3125]]

常用函数

(1)、求平均值:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar.mean())

2.5

(2)、求最大值:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar.max())

5

(3)、求最小值:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar.min())

0

(4)、求标准差:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar.std())

1.70782512766

(5)、求方差:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar.var())

2.91666666667

(6)、求和,np.sum() → axis为0,按列求和;axis为1,按行求和:
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar.sum())
print(np.sum(ar, axis = 0))
print(np.sum(ar, axis = 1))

15
[3 5 7]
[ 3 12]

(7)、排序:
import numpy as np
print(np.sort(np.array([1,4,2,5,6,3])))

[1 2 3 4 5 6]

你可能感兴趣的:(Numpy通用函数)