返回一個具有給定形狀的新陣列,但不初始化條目,並更改 NumPy 中的預設型別
要返回一個具有給定形狀的新陣列,但不初始化條目,請在 Python NumPy 中使用 **numpy.empty()** 方法。第一個引數是空陣列的形狀。empty() 的預設型別是浮點數。我們使用第二個引數“**dtype**”將其更改為“int”。
dtype 是陣列所需的輸出資料型別,例如 numpy.int8。預設值為 numpy.float64。order 指示是否以行主序(C 樣式)或列主序(Fortran 樣式)方式在記憶體中儲存多維資料。
empty() 函式返回一個具有給定形狀、dtype 和 order 的未初始化(任意)資料的陣列。物件陣列將初始化為 None。
NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等等。它支援廣泛的硬體和計算平臺,並且與分散式、GPU 和稀疏陣列庫配合良好。
步驟
首先,匯入所需的庫:
import numpy as np
要返回一個具有給定形狀的新陣列,但不初始化條目,請在 Python NumPy 中使用 numpy.empty() 方法:
arr = np.empty([3, 3], dtype = int)
顯示陣列:
print("Array...
",arr)獲取陣列的型別:
print("
Array type...
", arr.dtype)獲取陣列的維度:
print("
Our Array Dimensions...
", arr.ndim)獲取陣列中的元素數量:
print("
Number of elements...
", arr.size)示例
import numpy as np
# To return a new array of given shape, without initializing entries, use the numpy.empty() method in Python Numpy
# The 1st parameter is the Shape of the empty array
# The default type for empty() is float. We are changing it to "int" using the 2nd parameter i.e. "dtype"
arr = np.empty([3, 3], dtype = int)
# Display the array
print("Array...
",arr)
# Get the type of the array
print("
Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimensions...
", arr.ndim)
# Get the number of elements in the Array
print("
Number of elements...
", arr.size)輸出
Array... [0 0 7307491100562890867] [8390032522262880356 7232535146496532584 2334111870318900321] [8314046642435483764 7953674041135475819 160]] Array type... int64 Our Array Dimensions... 2 Number of elements... 9
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP