Python numpy.shapes()
Python numpy.reshape()函数在不改变数组数据的情况下塑造数组。
语法:
numpy.reshape(array, shape, order = 'C')
参数 :
array : [array_like]输入数组
shape : [int or tuples of int] 例如,如果我们要排列一个有10个元素的数组,那么像numpy.reshape(4,8)那样排列是错误的;我们可以用numpy.reshape(2,5)或(5,8)来排列。
我们可以用 numpy.reshape(2, 5) 或 (5, 2) 的方式。
order : [C-contiguous, F-contiguous, A-contiguous; optional] 在内存中的C-contiguous顺序。
C-连续顺序在内存中(最后一个索引变化最快)
C顺序意味着在数组上操作行升将会稍微快一些
FORTRAN-在内存中的连续顺序(第一个索引变化最快)。
F顺序意味着对列的操作会更快。
A’意味着在以下情况下按照类似Fortran的索引顺序读/写元素。
阵列在内存中是FORTRAN连续的,否则就是C-like顺序
返回 类型:
阵列,该阵列在不改变数据的情况下被重塑。
示例
# Python Program illustrating # numpy.reshape() method import numpy as geek # array = geek.arrange(8) # The 'numpy' module has no attribute 'arrange' array1 = geek.arange(8) print("Original array : \n", array1) # shape array with 2 rows and 4 columns array2 = geek.arange(8).reshape(2, 4) print("\narray reshaped with 2 rows and 4 columns : \n", array2) # shape array with 4 rows and 2 columns array3 = geek.arange(8).reshape(4, 2) print("\narray reshaped with 4 rows and 2 columns : \n", array3) # Constructs 3D array array4 = geek.arange(8).reshape(2, 2, 2) print("\nOriginal array reshaped to 3D : \n", array4)
输出 :
Original array : [0 1 2 3 4 5 6 7] array reshaped with 2 rows and 4 columns : [[0 1 2 3] [4 5 6 7]] array reshaped with 4 rows and 2 columns : [[0 1] [2 3] [4 5] [6 7]] Original array reshaped to 3D : [[[0 1] [2 3]] [[4 5] [6 7]]] [[0 1 2 3] [4 5 6 7]]
版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com