flatten()函数用法

flatten是numpy.ndarray.flatten的一个函数,即返回一个折叠成一维的数组。但是该函数只能适用于numpy对象,即array或者mat,普通的list列表是不行的。
其官方文档是这样描述的

Parameters:ndarray.flatten(order='C')
Return a copy of the array collapsed into one dimension.
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’.

a是个矩阵或者数组,a.flatten()就是把a降到一维,默认是按横的方向降
那么a.flatten().A又是什么呢? 其实这是因为此时的a是个矩阵,降维后还是个矩阵,矩阵.A(等效于矩阵.getA())变成了数组。具体看下面的例子:

1、用于array对象

>>> from numpy import *
>>> a=array([[1,2],[3,4],[5,6]])
>>> a
array([[1, 2],[3, 4],[5, 6]])
>>> a.flatten()
array([1, 2, 3, 4, 5, 6])
>>> a.flatten('F')
array([1, 3, 5, 2, 4, 6])  # 按列排序
>>> a.flatten('A')
array([1, 2, 3, 4, 5, 6])
>>> 

2、用于mat对象

>>> a=mat([[1,2,3],[4,5,6]])
>>> a
matrix([[1, 2, 3],[4, 5, 6]])
>>> a.flatten()
matrix([[1, 2, 3, 4, 5, 6]])
>>> a=mat([[1,2,3],[4,5,6]])
>>> a
matrix([[1, 2, 3],[4, 5, 6]])
>>> a.flatten()
matrix([[1, 2, 3, 4, 5, 6]])
>>> y=a.flatten().A
>>> shape(y)
(1L, 6L)
>>> shape(y[0])
(6L,)
>>> a.flatten().A[0]
array([1, 2, 3, 4, 5, 6])
>>>  

从中可以看出matrix.A的用法和矩阵发生的变化。

3、但是该方法不能用于list对象,想要list达到同样的效果可以使用列表表达式:

>>> a=array([[1,2],[3,4],[5,6]])
>>> [y for x in a for y in x]
[1, 2, 3, 4, 5, 6]
>>> 

完美实现!!

参考:http://www.cnblogs.com/harvey888/p/5590357.html

Python中flatten( ),matrix.A用法相关推荐

  1. python中flatten_Python中flatten( ),matrix.A用法说明

    flatten()函数用法 flatten是numpy.ndarray.flatten的一个函数,即返回一个折叠成一维的数组.但是该函数只能适用于numpy对象,即array或者mat,普通的list ...

  2. python的继承用法_【后端开发】python中继承有什么用法?python继承的用法详解

    本篇文章给大家带来的内容是关于python中继承有什么用法?python继承的用法详解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 面向对象三大特征 1.封装:根据职责将属性和方法 ...

  3. python中index函数_详解python中的index函数用法

    1.函数的创建 def fun(): #定义 print('hellow') #函数的执行代码 retrun 1 #返回值 fun() #执行函数 2.函数的参数 普通参数 :要按照顺序输入参数 de ...

  4. python中int函数的用法浅析_Python中int()函数的用法浅析

    int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builtin__ ...

  5. python中的lambda函数用法--无需定义函数名的函数或子程序,避免代码充斥着大量单行函数

    匿名函数lambda:是指一类无需定义标识符(函数名)的函数或子程序. lambda 函数可以接收任意多个参数 (包括可选参数) 并且返回单个表达式的值. 要点: lambda 函数不能包含命令 包含 ...

  6. python中int用法,Python中int()函数的用法浅析

    int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builtin__ ...

  7. python中pop函数的用法_python中pop()函数怎么用

    python中pop()函数的用法:pop()函数用于移除列表中的一个元素(默认最后一个元素),并且返回从列表中移除的元素对象.函数语法:[list.pop(ojb=list[-1])]. pop() ...

  8. python中index方法详解_详解python中的index函数用法

    1.函数的创建 def fun(): #定义 print('hellow') #函数的执行代码 retrun 1 #返回值 fun() #执行函数 2.函数的参数 普通参数 :要按照顺序输入参数 de ...

  9. python中config方法作用,python中ConfigParse模块的用法

    本文实例讲述了python中ConfigParse模块的用法,分享给大家供大家参考.具体方法如下: 写配置一般用ConfigParse.RawConfigParse类 读配置用ConfigParse. ...

最新文章

  1. 聊天机器人之文本分词
  2. linux下随机数字的生成
  3. 【PPT 下载】神策 2017 数据驱动大会干货限时分享!
  4. Linux——SSH连接错误【No supported authentication methods available】解决方案
  5. 算法正义_正义联盟的Sprint Boot
  6. 数据库可以存php代码,php把数组保存数据库程序代码
  7. .Net Core WebApi(三)在Linux服务器上部署
  8. ASE光源 ASE噪声
  9. MySQL从零到一解读增量同步数据到elasticsearch canal adapter方式(binlog)实现
  10. dijkstra+priority_queue+vector
  11. [转]bookmark整理之.NET编程相关
  12. 软件测试入门有哪些书籍可供推荐?
  13. 滑动窗口平均值c语言,数据流滑动窗口平均值 · sliding window average from data stream...
  14. IDEA(或Android Studio)切换Git分支时弹出Git Checkout Problem
  15. MATLAB中使用plotyy绘制双纵坐标图及坐标轴设置
  16. R语言入门第一集 R语言、RTools、RStudio的简介、安装与使用
  17. 数据分析报告怎么写(五)
  18. 关于tp-link wr740 v4的刷机救砖的办法(非线刷解决)恢复原版的
  19. 用函数编程计算反馈意见的平均数(Mean)、中位数(Median)和众数(Mode)
  20. 微信搜索上线竞价广告

热门文章

  1. VTK:几何对象之CylinderExample
  2. OpenGL Tessellated Triangle镶嵌三角形的实例
  3. QT的QSharedDataPointer类的使用
  4. QT的QOpenGLDebugLogger类的使用
  5. QT的QGraphicsItem类的使用
  6. 经典C语言程序100例之二一
  7. html fmt转换日期格式,js通用时间格式转换函数
  8. maven web项目导入sts_Spring Boot2 系列教程(二)创建 Spring Boot 项目的三种方式
  9. 一台电脑上同启动两个Tomcat的方式,windows/Linux配置。
  10. 14_Android中Service的使用,关于广播接收者的说明