PyPackage02---Numpy01

tech2024-01-20  79

Intro

  对于numpy中的多维数组,需要将其转换成1维。此时可以用flatten方法。 相关环境和package信息:

import sys import pandas as pd import numpy as np print("Python版本:",sys.version) print("numpy版本:",np.__version__) Python版本: 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] numpy版本: 1.17.4

参数说明

Docstring: a.flatten(order='C') Return a copy of the array collapsed into one dimension. Parameters ---------- order : {'C', 'F', 'A', 'K'}, optional 'C' means to flatten in row-major (C-style) order. 'F' means to flatten in column-major (Fortran- style) order. 'A' means to flatten in column-major order if `a` is Fortran *contiguous* in memory, row-major order otherwise. 'K' means to flatten `a` in the order the elements occur in memory. The default is 'C'.

C和F比较好理解,C即按照行展开,拼接。F按照列展开拼接,AK不太理解啥逻辑。 default是"C"

Case

arr1 = np.array([[1,2,3],[4,5,6]]) arr1.flatten() array([1, 2, 3, 4, 5, 6]) arr1.flatten("C") array([1, 2, 3, 4, 5, 6])

如上,是按照行展开拼接的

arr1.flatten("F") array([1, 4, 2, 5, 3, 6])

如上,是按照列展开拼接的

                                2020-09-03 于南京市江宁区九龙湖

最新回复(0)