在 Numpy 中将 1-D 数组作为列堆叠成 2-D 数组
要将 1-D 数组作为列堆叠成 2-D 数组,请在 Python Numpy 中使用 **ma.column_stack()** 方法。获取一系列 1-D 数组并将它们作为列堆叠以创建一个 2-D 数组。2-D 数组按原样堆叠,就像使用 hstack 一样。1-D 数组首先转换为 2-D 列。参数是要堆叠的数组。所有数组都必须具有相同的第一个维度。
返回由堆叠给定数组形成的数组。它应用于 _data 和 _mask(如果有)。
步骤
首先,导入所需的库 -
import numpy as np import numpy.ma as ma
使用 array() 方法创建一个新数组 -
arr = np.array([[200], [300], [400], [500]])
print("Array...
", arr)数组类型 -
print("
Array type...
", arr.dtype)
获取数组的维度 -
print("
Array Dimensions...
",arr.ndim)要将 1-D 数组作为列堆叠成 2-D 数组,请在 Python Numpy 中使用 ma.column_stack() 方法
resArr = np.ma.column_stack (arr)
结果数组 -
print("
Result...
", resArr)示例
# Python ma.MaskedArray - Stack 1-D arrays as columns into a 2-D array
import numpy as np
import numpy.ma as ma
# Create a new array using the array() method
arr = np.array([[200], [300], [400], [500]])
print("Array...
", arr)
# Type of array
print("
Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# To stack 1-D arrays as columns into a 2-D array, use the ma.column_stack() method in Python Numpy
resArr = np.ma.column_stack (arr)
# Resultant Array
print("
Result...
", resArr)输出
Array... [[200] [300] [400] [500]] Array type... int64 Array Dimensions... 2 Result... [[200 300 400 500]]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP