在NumPy中返回具有給定形狀和型別,並填充填充值的新陣列
要在Python NumPy中返回具有給定形狀和型別,並填充填充值的新陣列,請使用**numpy.full()**方法。第一個引數是新陣列的形狀。第二個引數設定填充值。
dtype是陣列所需的資料型別。order建議是否以C或Fortran連續(行或列方式)順序在記憶體中儲存多維資料。
NumPy提供全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等等。它支援廣泛的硬體和計算平臺,並且與分散式、GPU和稀疏陣列庫配合良好。
步驟
首先,匯入所需的庫:
import numpy as np
要在Python NumPy中返回具有給定形狀和型別,並填充填充值的新陣列,請使用numpy.full()方法。第二個引數設定填充值:
arr = np.full((4,5), fill_value = 999)
顯示陣列:
print("Array...
",arr)獲取資料型別:
print("
Array datatype...
",arr.dtype)
獲取陣列的維度:
print("
Array Dimensions...
",arr.ndim)獲取陣列的形狀:
print("
Our Array Shape...
",arr.shape)
獲取陣列的元素個數:
print("
Elements in the Array...
",arr.size)示例
import numpy as np
# To return a new array of given shape and type, filled with a fill value, use the numpy.full() method in Python Numpy
# The 1st parameter is the shape of the new array
# The 2nd parameter sets the fill value
arr = np.full((4,5), fill_value = 999)
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)輸出
Array... [[999 999 999 999 999] [999 999 999 999 999] [999 999 999 999 999] [999 999 999 999 999]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (4, 5) Elements in the Array... 20
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP