python numpy 矩阵左右翻转/上下翻转

numpy API:

flip:

flip(m, 0) is equivalent to flipud(m).

flip(m, 1) is equivalent to fliplr(m).

flip(m, n) corresponds to m[...,::-1,...] with ::-1 at position n.

flip(m) corresponds to m[::-1,::-1,...,::-1] with ::-1 at all positions.

flip(m, (0, 1)) corresponds to m[::-1,::-1,...] with ::-1 at position 0 and position 1.

>>> A = np.arange(8).reshape((2,2,2))
>>> A
array([[[0, 1],
        [2, 3]],
       [[4, 5],
        [6, 7]]])
>>> flip(A, 0)
array([[[4, 5],
        [6, 7]],
       [[0, 1],
        [2, 3]]])
>>> flip(A, 1)
array([[[2, 3],
        [0, 1]],
       [[6, 7],
        [4, 5]]])
>>> np.flip(A)
array([[[7, 6],
        [5, 4]],
       [[3, 2],
        [1, 0]]])
>>> np.flip(A, (0, 2))
array([[[5, 4],
        [7, 6]],
       [[1, 0],
        [3, 2]]])
>>> A = np.random.randn(3,4,5)
>>> np.all(flip(A,2) == A[:,:,::-1,...])
True

flipud: (==flip(m, 1) )

Flip array in the up/down direction.

Flip the entries in each column in the up/down direction. Rows are preserved, but appear in a different order than before.

Equivalent to m[::-1,...]. Does not require the array to be two-dimensional.

>>> A = np.diag([1.0, 2, 3])
>>> A
array([[ 1.,  0.,  0.],
       [ 0.,  2.,  0.],
       [ 0.,  0.,  3.]])
>>> np.flipud(A)
array([[ 0.,  0.,  3.],
       [ 0.,  2.,  0.],
       [ 1.,  0.,  0.]])
>>>
>>> A = np.random.randn(2,3,5)
>>> np.all(np.flipud(A) == A[::-1,...])
True
>>>
>>> np.flipud([1,2])
array([2, 1])

fliplr: (==flip(m, 0))

  Equivalent to m[:,::-1]. Requires the array to be at least 2-D.

Flip array in the left/right direction.
    rot90
        Rotate array counterclockwise.

>>> A = np.diag([1.,2.,3.])
>>> A
array([[ 1.,  0.,  0.],
       [ 0.,  2.,  0.],
       [ 0.,  0.,  3.]])
>>> np.fliplr(A)
array([[ 0.,  0.,  1.],
       [ 0.,  2.,  0.],
       [ 3.,  0.,  0.]])
>>>
>>> A = np.random.randn(2,3,5)
>>> np.all(np.fliplr(A) == A[:,::-1,...])
True

原文地址:https://www.cnblogs.com/xiaoniu-666/p/11123560.html

时间: 2024-08-08 06:08:14

python numpy 矩阵左右翻转/上下翻转的相关文章

Python numpy数组扩展效率问题

Numpy库的ndarray数组可以方便地进行各种多维数据处理工作 可是它最大的缺点就是不可动态扩展--"NumPy的数组没有这种动态改变大小的功能,numpy.append()函数每次都会重新分配整个数组,并把原来的数组复制到新数组中."(引用自http://blog.chinaunix.net/uid-23100982-id-3164530.html) 场景: 今天用ndarray处理 42000 条数据时,就遇到了数组扩展的效率问题 文件名:train.csv(后附下载) 文件大

Python/Numpy大数据编程经验

Python/Numpy大数据编程经验 1.边处理边保存数据,不要处理完了一次性保存.不然程序跑了几小时甚至几天后挂了,就啥也没有了.即使部分结果不能实用,也可以分析程序流程的问题或者数据的特点. 2. 及时用 del 释放大块内存.Python缺省是在变量范围(variablescope)之外才释放一个变量,哪怕这个变量在后面的代码没有再被用到,所以需要手动释放大的array. 注意所有对数组的引用都del之后,数组才会被del.这些引用包括A[2:]这样的view,即使np.split也只是

python numpy 的运算

一,基本运算 >>> a = array([1,2,3,4])>>> aarray([1, 2, 3, 4])>>> b=arange(4)>>> barray([0, 1, 2, 3])>>> a + barray([1, 3, 5, 7])>>> a - barray([1, 1, 1, 1])>>> a *barray([ 0, 2, 6, 12])>>>

python numpy教程

python numpy教程 2014-08-10 22:21:56 分类: Python/Ruby 先决条件 在阅读这个教程之前,你多少需要知道点python.如果你想重新回忆下,请看看Python Tutorial. 如果你想要运行教程中的示例,你至少需要在你的电脑上安装了以下一些软件: Python NumPy 这些是可能对你有帮助的: ipython是一个净强化的交互Python Shell,对探索NumPy的特性非常方便. matplotlib将允许你绘图 Scipy在NumPy的基础

Python Numpy ValueError: data type must provide an itemsize

天朝网络锁国,百度找了半个小时找不出来原因,只能谷歌 谷歌第一条就是,顿时感觉幸福感来的太突然 原因是输入的矩阵均是字符串(从文件里读的) 那么就需要批量转数组,一行一行的转. 下面是我的代码: rownum = 0 f = open(train_Y_path) for line in f.readlines(): train_Y_matrix[rownum] = map(float,line.strip('\n ').split(' ')) rownum += 1 print train_Y_

Python Numpy shape 基础用法(转自他人的博客,如涉及到侵权,请联系我)

Python Numpy shape 基础用法 shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度,比如shape[0]就是读取矩阵第一维度的长度.它的输入参数可以使一个整数表示维度,也可以是一个矩阵.这么说你可能不太理解,我们还是用各种例子来说明他的用法: 一维矩阵[1]返回值为(1L,) 二维矩阵,返回两个值 一个单独的数字,返回值为空 我们还可以将shape作为矩阵的方法来调用,下面先创建了一个单位矩阵e 我们可以快速读取e的形状 假如我们只想读

python numpy array 与matrix 乘方

python numpy array 与matrix 乘方 编程语言 waitig 1年前 (2017-04-18) 1272℃ 百度已收录 0评论 数组array 的乘方(**为乘方运算符)是每个元素的乘方,而矩阵matrix的乘方遵循矩阵相乘,因此必须是方阵. 2*3的数组与矩阵 >>> from numpy import * >>> import operator >>> a = array([[1,2,3],[4,5,6]]) >>

Recurrent Neural Networks Tutorial, Part 2 – Implementing a RNN with Python, Numpy and Theano

转载 - Recurrent Neural Networks Tutorial, Part 2 – Implementing a RNN with Python, Numpy and Theano This the second part of the Recurrent Neural Network Tutorial. The first part is here. Code to follow along is on Github. In this part we will implemen

最实用windows 下python+numpy安装(转载)

最实用windows 下python+numpy安装 如题,今天兜兜转转找了很多网站帖子,一个个环节击破,最后装好费了不少时间. 希望这个帖子能帮助有需要的人,教你一篇帖子搞定python+numpy,节约科研时间. 水平有限,难免存在不足,敬请指正. *******************python安装**************************************************** step1:官网下载安装包: https://www.python.org/ 我下载的