日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

np.zeros()函數(shù)的使用方法_python

作者:勤奮的大熊貓 ? 更新時間: 2023-10-27 編程語言

函數(shù)調(diào)用方法:

numpy.zeros(shape, dtype=float)

各個參數(shù)意義:

  • shape:創(chuàng)建的新數(shù)組的形狀(維度)。
  • dtype:創(chuàng)建新數(shù)組的數(shù)據(jù)類型。
  • 返回值:給定維度的全零數(shù)組。

基礎用法:

import numpy as np

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

可以看到我們成功創(chuàng)建了一個2行3列的全零二維數(shù)組。并且創(chuàng)建的數(shù)組中的數(shù)據(jù)類型是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
"""

可以看到,這里我們同樣成功創(chuàng)建了一個2行3列的全零二維數(shù)組。并且我們指定了其數(shù)據(jù)類型為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.])]
"""

原文鏈接:https://blog.csdn.net/u011699626/article/details/122193581

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新