
正文
创建 numpy.array
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
# 导包
import numpy as np
numpy.array
nparr = np.array([i for i in range(10)])
nparr # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
zeros
nparr = np.array([i for i in range(10)])
nparr # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
.zeros(shape=(x,y), dtype=None, order='C'):返回来一个给定形状和类型的用 0 填充的数组;
ones
.ones(shape=(x,y), dtype=None, order='C'):返回来一个给定形状和类型的用 1 填充的数组;
full
.full(fill_value=数字, shape=(x,y)):返回来一个给定形状用 指定常数 填充的数组;
eye
.eye(N,M =无,k = 0,dtype = None,order ='C'):返回一个二维数组,其中对角线为1,零点为零,默认 单位矩阵
shape: 数据尺寸、形状
dtype: 数据类型,可选参数,默认numpy.float64
order: 可选参数,C代表与c语言类似,行优先;F代表列优先
fill_value: 填充的常数
N: int,输出中的行数
M: int,可选参数,输出中的列数。如果无,默认为Ñ。
k: int,可选,对角线的索引:0(默认值)指的是主对角线,正值指的是上对角线,负值指的是下对角线
import numpy as np
# 建立一维数组
np.zeros(10) # array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.ones(10) # array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.zeros(10, dtype=float) # array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) # 建立一个矩阵(二维数组)
np.zeros((3, 5)) # 参数全为0的浮点型
np.zeros(shape=(3, 5), dtype=int)
np.full((3, 5), 666)
np.eye(3, dtype=int)
np.eye(3, k=1)
arange
-
.arange(start, stop, step, dtype = None):生成数组
-
arange --> 数
组
-
range --> list
start: 开始位置,数字,可选项,默认起始值为0
stop: 停止位置,数字
step: 步长,数字,可选项, 默认步长为1,如果指定了step,则还必须给出start。
dtype: 输出数组的类型。 如果未给出dtype,则从其他输入参数推断数据类型
import numpy as np
[i for i in range(0, 20, 2)] # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
np.arange(0, 20, 2) # array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
np.arange(0, 1, 0.2) # array([0. , 0.2, 0.4, 0.6, 0.8])
np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
linspace
.linspace(start, stop, num=50,endpoint=True, retstep=True, dtype = None):构造等差数列数组
start:开始数值
stop:结束数值
num:默认是50个样本点(数据),为正整数。
endpoint:默认为True,如果改为Fasle取不到右端点。
retstep:步长,如第一个数据和第第二个数据之间的距离。
dtype:可以设置数值类型,默认numpy.float64
import numpy as np
np.linspace(2, 3, num=5) # array([2. , 2.25, 2.5 , 2.75, 3. ])
np.linspace(2, 3, 5, retstep=True) # (array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
np.linspace(2, 3, 5, endpoint=False, retstep=True) # (array([2. , 2.2, 2.4, 2.6, 2.8]), 0.2)
random模块
随机抽样
rand
.random.rand(d0, d1, ... , dn): 均匀分布, 生成随机浮点数或N维浮点数组 ∈[0, 1)
import numpy as np
# 无参数
np.random.rand() # 随机生成一个浮点数:0.5693260707056654 # 一个参数
np.random.rand(5) #生成一个形状为5的一维数组
"""
array([0.03033044, 0.77342689, 0.05433217, 0.09637677, 0.78388356])
""" #两个参数
np.random.rand(2,3) #生成2x3的二维数组
"""
array([[0.62207475, 0.76152107, 0.8322783 ],
[0.9625673 , 0.14159552, 0.04631563]])
""" # 三个参数
np.random.rand(2,3,4) # 生成2个3x4的二维数组的三维数组
"""
array([[[0.69515825, 0.18253848, 0.94369687, 0.75510563],
[0.96469044, 0.10572931, 0.57167311, 0.26631619],
[0.25474511, 0.93999817, 0.49682046, 0.34514234]], [[0.59947499, 0.72308947, 0.80828931, 0.23145396],
[0.21023288, 0.2578903 , 0.30873597, 0.57619749],
[0.60353256, 0.02622591, 0.6148662 , 0.26888455]]])
"""
# 注意:参数必须是整数,不能是元组
randn
.random.randn(d0, d1, ... , dn): 标准正态分布 , 生成随机浮点数或N维浮点数组 ∈(-∞, +∞)
np.random.randn(2,3)
"""
array([[ 1.20815015, -1.71343109, -0.09987136],
[-0.33742606, 0.77209376, 0.18604608]])
"""
randint
.random.randint(low, high=None, size=None, dtype='l'): 离散均匀分布, 生成整数或N维整数数组
high ≠ None时, ∈ [low,high)
high = None时, ∈ [0,low)
import numpy as np
# 生成一个[0,2)之间随机整数
np.random.randint(2) # # 生成形状为10的一维整数数组 --> [1,10)随机整数
np.random.randint(1,10, size=10) # array([2, 6, 1, 4, 2, 4, 5, 3, 9, 9]) #生成一个2x3整数数组 --> [2,6)随机整数
np.random.randint(2,6,(2,3)) # np.random.randint(2,6,size=(2,3))
"""
array([[4, 3, 3],
[3, 2, 5]])
"""
seed
.random.seed(num): 具有预见性: 如果使用相同的num,则每次生成的随机数都相同。当参数不同或者无参数时,作用与 numpy.random.rand() 函数相同,即多次生成随机数且每次生成的随机数都不同。
详情请参考:https://www.cnblogs.com/lliuye/p/8551660.html
random
.random.random(size=None):返回随机的浮点数 ∈[0, 1)
import numpy as np np.random.random(3)
"""
array([0.59252408, 0.37882179, 0.61683128])
""" np.random.random((3,4))
"""
array([[0.51443445, 0.26071378, 0.17125182, 0.67260413],
[0.11513209, 0.35772957, 0.79621782, 0.29822459],
[0.19795992, 0.76367099, 0.25980888, 0.22451909]])
"""
normal
.random.normal(loc=0.0, scale=1.0, size=None):均值为loc,标准差为scale的 正态分布 ∈(-∞, +∞)
import numpy as np
ran = np.random.normal(0, 1, (3, 5))
ran
"""
array([[ 1.85322786, 1.13089185, -0.55186512, -1.58926526, -0.00656168],
[ 0.44077735, -0.1599656 , -0.2450492 , 1.71447564, -0.1603678 ],
[-0.0495362 , 0.30244281, 1.62018096, 0.31465292, 1.99379836]])
""" """
均值:μ
方差:σ^2=(∑▒〖(X-μ)〗^2 )/N
标准差:s=√(σ^2 )
协方差:σ^2=(∑▒〖(X-μ)〗^2 )/N-1
"""
# 均值
ran.mean()
# 方差
ran.var()
# 标准差
ran.std(ddof=1)
# 协方差
ran.cov()
permutation
.random.permutation(x):随机打乱x中的元素。若x是整数,则打乱arange(x),若x是一个数组,则将copy(x)的第一位索引打乱,意思是先复制x,对副本进行打乱处理,打乱只针对数组的第一维
np.random.permutation(5) # array([2, 1, 4, 3, 0])
np.random.permutation(5) # array([4, 3, 0, 2, 1])
np.random.permutation([[1,2,3],[4,5,6]])
"""
array([[1, 2, 3],
[4, 5, 6]])
"""
np.random.permutation([[1,2,3],[4,5,6]])
"""
array([[4, 5, 6],
[1, 2, 3]])
"""
shuffle
.random.shuffle(x):与permutation类似,随机打乱x中的元素。若x是整数,则打乱arange(x),但是shuffle会对x进行修改
a = np.arange(5)
a # array([0, 1, 2, 3, 4])
np.random.shuffle(a)
a # array([2, 4, 3, 1, 0])
choice
.random.choice(a, size=None, replace=True, p=None): 从a(数组)中选取size(维度)大小的随机数,replace=True表示可重复抽取,p是a中每个数出现的概率,若a是整数,则a代表的数组是arange(a)
np.random.choice(5) #
# 随机在2或4中选一个
np.random.choice([2, 4]) #
# 选取2的概率为1,选取3的概率为0
np.random.choice([2, 3], p=[1, 0]) #
# 选取3的概率为0,选取5的概率为1
np.random.choice([3, 5], p=[0, 1]) #
# 从0-4随机抽取5个数,可重复抽取
np.random.choice(5, 5) # array([3, 0, 1, 3, 0])
# 从0-4随机抽取5个数,不可重复抽取
np.random.choice(5, 5, False) # rray([3, 0, 1, 4, 2])
np.random.choice(100, (2, 3, 5), False)
"""
array([[[29, 72, 13, 61, 45],
[28, 86, 74, 52, 0],
[76, 77, 60, 48, 12]], [[85, 99, 90, 46, 79],
[64, 3, 57, 83, 87],
[17, 59, 7, 16, 96]]])
"""
bytes
.random.bytes(length):生成随机字节
np.random.bytes(1) # b'\xd4'
np.random.bytes(2) # b'\x9b\x8a'
uniform
.random.uniform(low,high,size):从一个 均匀分布 [low,high) 中随机采样
low:采样下界,float类型,默认值为0;
high:采样上界,float类型,默认值为1;
size:输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出m*n*k个样本,缺省时输出1个值。
返回值:ndarray类型,其形状和参数size中描述一致
import matplotlib.pyplot as plt
import numpy as np s = np.random.uniform(0,1,1200) # 产生1200个[0,1)的数
count, bins, ignored = plt.hist(s, 12, normed=True)
"""
hist原型:
matplotlib.pyplot.hist(x, bins=10, range=None, normed=False, weights=None,
cumulative=False, bottom=None, histtype='bar', align='mid',
orientation='vertical',rwidth=None, log=False, color=None, label=None,
stacked=False, hold=None,data=None,**kwargs)
输入参数很多,具体查看matplotlib.org,本例中用到3个参数,分别表示:s数据源,bins=12表示bin
的个数,即画多少条条状图,normed表示是否归一化,每条条状图y坐标为n/(len(x)`dbin),整个条状图积分值为1
输出:count表示数组,长度为bins,里面保存的是每个条状图的纵坐标值
bins:数组,长度为bins+1,里面保存的是所有条状图的横坐标,即边缘位置
ignored: patches,即附加参数,列表或列表的列表,本例中没有用到。
"""
plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')
plt.show()







