python怎么初始化数组

1,912次阅读
没有评论

python怎么初始化数组

因为画图中x轴与y轴的数据通常为数组格式的数据,所以先总结一下如何初始化数组:

(1)list得到数组

# 通过array函数传递list对象
L = [1, 2, 3, 4, 5, 6]
a = np.array(L)
# 若传递的是多层嵌套的list,将创建多维数组
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
# 可以通过dtype参数在创建时指定元素类型
d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.float)
# 如果更改元素类型,可以使用astype安全的转换
f = d.astype(np.int)

(2)使用arange

# 和Python的range类似,arange同样不包括终值;但arange可以生成浮点类型,而range只能是整数类型
# 1为开始值,10为终止值(不包括),0.5为步长
a = np.arange(1, 10, 0.5)

(3)使用ones、zeros、empty

# np.ones(shape, dtype),生成元素全为1(默认浮点型)的数组
# shape可以为一个整数得到一个一维数组,也可以为(整数1,整数2)的格式得到二维数组,同理可得多维数组
a = np.ones((3, 3), dtype=np.int32)
print("a: \n", a)
   
# np.zeros(shape, dtype),生成元素全为0(默认浮点型)的数组
# 用法与np.noes()一样
b = np.zeros((3, 3), dtype=np.int32)
print("b: \n", b)
   
# np.empty(shape, dtype),生成元素为随机数(默认浮点型)的数组
# 用法与np.ones()一样
c = np.empty((3, 4), dtype=np.int32)
print("c: \n", c)

# np.ones()、np.zeros()、np.empty()都具有如下形式复制一个结构一样的数组,但数据类型可选择
np.ones_like(array, dtype=)
np.zeros_like(array, dtype=)
np.empty_like(array, dtype=)

(4)等差数列

# linspace函数通过指定起始值、终止值和元素个数来创建等差数组,元素之间是等步长的
# endpoint表示是否包括终止值,默认为True
b = np.linspace(1, 10, 10,endpoint=True)

(5)等比数列

# 指定起始值、终止值、元素个数和基数来创建等比数列
# base表示基数,下式创建了一个1到4之间的有10个数的等比数列
d = np.logspace(1, 2, 10, endpoint=True, base=2)
# 基数为10,下式创建了一个10到100之间的有10个数的等比数列
d = np.logspace(1, 2, 10, endpoint=True, base=10)

(6)随机数

rand()

# 返回一个服从“0~1”均匀分布的随机数,该随机数在[0, 1)内,也可以返回一个由服从“0~1”均匀分布的随机数组成的数组。
# np.random.rand(d0, d1, …, dn)
# 返回一个随机值,随机值在[0, 1)内
In[15]: np.random.rand()
Out[15]: 0.9027797355532956
# 返回一个3x3的数组,数组元素在[0, 1)内
In[16]:np.random.rand(3,3)
Out[16]: 
array([[ 0.47507608,  0.64225621,  0.9926529 ],
    [ 0.95028412,  0.18413813,  0.91879723],
    [ 0.89995217,  0.42356103,  0.81312942]])
In[17]: np.random.rand(3,3,3)
  
# 返回一个3x3x3的数组
Out[17]: 
array([[[ 0.30295904,  0.76346848,  0.33125168],
    [ 0.77845927,  0.75020602,  0.84670385],
    [ 0.2329741 ,  0.65962263,  0.93239286]],
    [[ 0.24575304,  0.9019242 ,  0.62390674],
    [ 0.43663215,  0.93187574,  0.75302239],
    [ 0.62658734,  0.01582182,  0.66478944]],
    [[ 0.22152418,  0.51664503,  0.41196781],
    [ 0.47723318,  0.19248885,  0.29699868],
    [ 0.11664651,  0.66718804,  0.39836448]]])

randn()

# 产生标准正态分布随机数或随机数组,用法与rand(d0, d1, …, dn)方法一样
np.random.randn(d0, d1, …, dn)

randint()

# 可以生成随机数,也可以生成多维随机数组
# np.random.randint(low, high=None, size=None, dtype=)
# [0,4)之间的随机数
In[7]: np.random.randint(4)
Out[7]: 1
# [0,4)之间的一维数组
In[8]: np.random.randint(4,size=4)
Out[8]: array([2, 2, 2, 0])
# [4,10)之间的一维数组
In[9]: np.random.randint(4,10,size=6)
Out[9]: array([7, 9, 7, 8, 6, 9])
# [4,10)之间的2x2数组
np.random.randint(4,10,size=(2,2),dtype='int32')
Out[10]:
array([[7, 4],[6, 9]])

uniform()

# 产生[low, high)之间的均匀分布随机数或随机数组,low默认为0.0,high默认为1.0
np.random.uniform(low=0.0, high=1.0, size=None)

normal()

# 产生均值为loc,方差为scale的服从正太分布的随机数或随机数组,loc默认为0,scale默认为1
np.random.normal(loc=0.0, scale=1.0, size=None)
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-06-22发表,共计2842字。
新手QQ群:570568346,欢迎进群讨论 Python51学习