注意:
import numpy as np
l = [1,2,3,4,5,6,7]
print(type(l)) # list
nd = np.array(l)
print(type(nd)) # numpy.ndarray
求和
nd.sum()
求均方差
nd.var()
求标准差
nd.std()
随机数生成
x = np.arange(0,100000,1)
print(x) # array([ 0, 1, 2, ..., 99997, 99998, 99999])
np.ones(shape, dtype=None, order=‘C‘) # shape 形状 dtype 数据类型
x = np.ones(shape = (5,5),dtype=np.int8)
print(x)
# array([[1, 1, 1, 1, 1],
# [1, 1, 1, 1, 1],
# [1, 1, 1, 1, 1],
# [1, 1, 1, 1, 1],
# [1, 1, 1, 1, 1]], dtype=int8)
np.zeros(shape, dtype=float, order=‘C‘)
x = np.zeros(shape = (2,3,4),dtype=np.float16)
print(x)
#array([[[0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]],
#
# [[0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]]], dtype=float16)
np.full(shape, fill_value, dtype=None, order=‘C‘)
x = np.full(shape = (3,5),fill_value=3.14)
print(x)
#array([[3.14, 3.14, 3.14, 3.14, 3.14],
# [3.14, 3.14, 3.14, 3.14, 3.14],
# [3.14, 3.14, 3.14, 3.14, 3.14]])
np.eye(N, M=None, k=0, dtype=float)
# 对角线为1其他的位置为0
# 单位矩阵
x = np.eye(N = 5)
print(x)
#array([[1., 0., 0., 0., 0.],
# [0., 1., 0., 0., 0.],
# [0., 0., 1., 0., 0.],
# [0., 0., 0., 1., 0.],
# [0., 0., 0., 0., 1.]])
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
# 等差数列
np.linspace(0,100,num = 21)
#array([ 0., 5., 10., 15., 20., 25., 30., 35., 40., 45., 50.,
# 55., 60., 65., 70., 75., 80., 85., 90., 95., 100.])
np.arange([start, ]stop, [step, ]dtype=None)
np.arange(0,100,3)
#array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48,
# 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99])
np.random.randint(low, high=None, size=None, dtype=‘l‘)
np.random.randint(0,100,size = (5,5))
#array([[ 4, 45, 66, 55, 42],
# [28, 5, 75, 71, 44],
# [ 0, 17, 89, 81, 66],
# [95, 96, 89, 31, 6],
# [75, 31, 51, 81, 38]])
np.random.randn(d0, d1, ..., dn)
# 标准正态分布
# normal 正常,正太
# dimession 维度
# 平均值是0,方差是1
np.random.randn(4,5)
#array([[-1.26185332, 0.29715466, 0.52047771, -1.55183841, -0.83663771],
# [ 0.40776138, -0.7380327 , 0.22623508, 1.12275365, -0.38189704],
# [ 0.67816239, 0.91695635, 0.13487838, 0.13769114, 0.68426452],
# [ 0.00935704, 0.49087787, -0.34920945, 0.15688878, -0.98320155]])
np.random.normal(loc=0.0, scale=1.0, size=None)
np.random.normal(loc = 175,scale=10,size = 10000).round(2)
#array([180.02, 162.26, 184.18, ..., 179.19, 182.83, 174.51])
np.random.random(size=None)
# 生成0到1的随机数,左闭右开
np.random.random(10)
#array([0.80403643, 0.60631454, 0.22301424, 0.03813725, 0.14537585,
# 0.00946211, 0.39063408, 0.5558176 , 0.39426771, 0.74874309])
4个必记参数:
一维与列表完全一致,多维时同理
nd2 = np.random.randint(0,150,size = (4,5))
print(nd2)
#array([[124, 91, 52, 23, 16],
# [122, 106, 143, 88, 85],
# [100, 0, 141, 101, 72],
# [ 26, 93, 123, 4, 31]])
nd2[1,1] # 106
nd2[2] # array([100, 0, 141, 101, 72])
一维与列表完全一致,多维时同理
nd2
array([[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31]])
nd2[0:3]
array([[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72]])
nd2[-2:]
array([[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31]])
nd2[0:3,0:3]
array([[124, 91, 52],
[122, 106, 143],
[100, 0, 141]])
将数据反转,例如[1,2,3]---->[3,2,1]
nd3 = nd[:10]
nd3
array([189.04, 166.26, 172.39, 172.1 , 173. , 176.82, 176. , 177.74,
162.46, 176.13])
nd3[::-1]
array([176.13, 162.46, 177.74, 176. , 176.82, 173. , 172.1 , 172.39,
166.26, 189.04])
## 两个::进行切片
使用reshape函数,注意参数是一个tuple!
nd2
array([[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31]])
nd2.reshape(2,10)
array([[124, 91, 52, 23, 16, 122, 106, 143, 88, 85],
[100, 0, 141, 101, 72, 26, 93, 123, 4, 31]])
nd2.reshape(5,4)
array([[124, 91, 52, 23],
[ 16, 122, 106, 143],
[ 88, 85, 100, 0],
[141, 101, 72, 26],
[ 93, 123, 4, 31]])
原文:https://www.cnblogs.com/techoc/p/13428182.html