在 Numpy 中返回一個具有給定形狀和型別的新陣列,但不初始化其中的元素
要返回一個具有給定形狀和型別的新陣列,而不初始化其元素,請在 Python Numpy 中使用 **ma.empty()** 方法。第一個引數設定空陣列的形狀。**dtype** 引數設定陣列所需的輸出資料型別。該方法返回一個具有給定形狀、dtype 和順序的未初始化(任意)資料的陣列。物件陣列將初始化為 None。
步驟
首先,匯入所需的庫 -
import numpy as np import numpy.ma as ma
使用 Python Numpy 中的 ma.empty() 方法返回一個具有給定形狀和型別的新陣列,但不初始化其中的元素 -
arr = ma.empty((5, 5),dtype = np.int32)
顯示我們的陣列 -
print("Array...
",arr)獲取陣列的維度 -
print("
Array Dimensions...
",arr.ndim)
獲取陣列的形狀 -
print("
Our Array Shape...
",arr.shape)獲取陣列的元素數量 -
print("
Elements in the Array...
",arr.size)
示例
# Python ma.MaskedArray - Return a new array of given shape and type without initializing entries
import numpy as np
import numpy.ma as ma
# To return a new array of given shape and type, without initializing entries, use the ma.empty() method in Python Numpy
# The 1st parameter sets the shape of the empty array
# The dtype parameter sets the desired output data-type for the array
arr = ma.empty((5, 5),dtype = np.int32)
# Displaying our array
print("Array...
",arr)
# 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... [[ 0 0 0 0 117] [ 99 104 32 102 105] [108 101 32 111 114] [ 32 100 105 114 101] [ 99 116 111 114 121]] Array Dimensions... 2 Our Array Shape... (5, 5) Elements in the Array... 25
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP