温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

np.zeros()函数如何使用

发布时间:2023-02-28 14:48:38 来源:亿速云 阅读:92 作者:iii 栏目:开发技术

这篇文章主要介绍“np.zeros()函数如何使用”,在日常操作中,相信很多人在np.zeros()函数如何使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”np.zeros()函数如何使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

函数调用方法:

numpy.zeros(shape, dtype=float)

各个参数意义:

  • shape:创建的新数组的形状(维度)。

  • dtype:创建新数组的数据类型。

  • 返回值:给定维度的全零数组。

基础用法:

import numpy as np

array = np.zeros([2, 3])
print(array)
print(array.dtype)
"""
result:
[[0. 0. 0.]
 [0. 0. 0.]]
float64
"""

可以看到我们成功创建了一个2行3列的全零二维数组。并且创建的数组中的数据类型是np.float64类型。

进阶用法:

import numpy as np

array = np.zeros([2, 3], dtype=np.int32)
print(array)
print(array.dtype)
"""
result:
[[0 0 0]
 [0 0 0]]
int32
"""

可以看到,这里我们同样成功创建了一个2行3列的全零二维数组。并且我们指定了其数据类型为np.int32。

最高级的用法:

import numpy as np

# Create rain data
n_drops = 10

rain_drops = np.zeros(n_drops, dtype=[('position', float, (2,)),
                                      ('size', float),
                                      ('growth', float),
                                      ('color', float, (4,))])

# Initialize the raindrops in random positions and with
# random growth rates.
rain_drops['position'] = np.random.uniform(0, 1, (n_drops, 2))
rain_drops['growth'] = np.random.uniform(50, 200, n_drops)

print(rain_drops)
"""
result:
[([0.70284885, 0.03590322], 0., 176.4511602 , [0., 0., 0., 0.])
 ([0.60838294, 0.49185854], 0.,  60.51037667, [0., 0., 0., 0.])
 ([0.86525398, 0.65607663], 0., 168.00795695, [0., 0., 0., 0.])
 ([0.25812877, 0.14484747], 0.,  80.17753717, [0., 0., 0., 0.])
 ([0.66021716, 0.90449213], 0., 121.94125106, [0., 0., 0., 0.])
 ([0.88306332, 0.51074725], 0.,  92.4377108 , [0., 0., 0., 0.])
 ([0.68916433, 0.89543162], 0.,  90.77596431, [0., 0., 0., 0.])
 ([0.7105655 , 0.68628326], 0., 144.88783652, [0., 0., 0., 0.])
 ([0.6894679 , 0.90203559], 0., 167.40736266, [0., 0., 0., 0.])
 ([0.92558218, 0.34232054], 0.,  93.48654986, [0., 0., 0., 0.])]
"""

到此,关于“np.zeros()函数如何使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI