Here is a function in Numpy module which could apply a function to 1D slices along the Given Axis. It works like apply funciton in Pandas.

  1. numpy.apply_along_axis(func1d, axis, arr, *args, **kwargs)
Parameters:
func1d : function (M,) -> (Nj…)

This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.

axis : integer

Axis along which arr is sliced. (axis = 1: along the row; axis = 0: along the column)

arr : ndarray (Ni…, M, Nk…)

Input array.

args : any

Additional arguments to func1d.

kwargs : any

Additional named arguments to func1d.

New in version 1.9.0.

Returns:
out : ndarray (Ni…, Nj…, Nk…)

The output array. The shape of out is identical to the shape of arr, except along the axisdimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value of func1d. So if func1d returns a scalar out will have one fewer dimensions than arr.

Here is an example to shift the image one pixel down through numpy.apply_along_axis method.

  1. import numpy as np
  2. from scipy.ndimage.interpolation import shift
  3. def shift_one_pixel(image, dx,dy):
  4. image=image.reshape(28,28)
  5. image_shifted=shift(image,[dy,dx],cval=0,mode='constant')
  6. return image_shifted.reshape(28*28)
  7.  
  8. X_train_expanded = np.apply_along_axis(shift_one_pixel,1,X_train,1,0)

[Python Cookbook] Numpy: How to Apply a Function to 1D Slices along the Given Axis的更多相关文章

  1. [Python Cookbook] Numpy: Multiple Ways to Create an Array

    Convert from list Apply np.array() method to convert a list to a numpy array: import numpy as np myl ...

  2. [Python Cookbook] Numpy Array Slicing and Indexing

    1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...

  3. [Python Cookbook] Numpy: Iterating Over Arrays

    1. Using for-loop Iterate along row axis: import numpy as np x=np.array([[1,2,3],[4,5,6]]) for i in ...

  4. [Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate

    数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.arr ...

  5. [Python Cookbook] Numpy Array Manipulation

    1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...

  6. 【Python】numpy 数组拼接、分割

    摘自https://docs.scipy.org 1.The Basics 1.1 numpy 数组基础 NumPy’s array class is called ndarray. ndarray. ...

  7. Python使用numpy实现BP神经网络

    Python使用numpy实现BP神经网络 本文完全利用numpy实现一个简单的BP神经网络,由于是做regression而不是classification,因此在这里输出层选取的激励函数就是f(x) ...

  8. 【python cookbook】找出序列中出现次数最多的元素

    问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...

  9. Python中Numpy及Matplotlib使用

    Python中Numpy及Matplotlib使用 1. Jupyter Notebooks 作为小白,我现在使用的python编辑器是Jupyter Notebook,非常的好用,推荐!!! 你可以 ...

随机推荐

  1. nginx url rewrite break和last的区别

    break 将重写的URI作为一个新的URI,在本块中继续处理,将重写后 的地址在当前location块中处理,不会将新的URI转向到其他location块中 last,终止继续在本location块 ...

  2. 【netbeans】【ubuntu】ubuntu netbeans 抗锯齿化修复

    每一个在ubuntu下用netbeans的,都会对它的字体怎么会显示的那么难看表示很不理解.我就是因此几乎没有用netbeans的.   不过今天终于解决问题了,虽然没有eclipse显示的那么漂亮, ...

  3. Yii2 基于rbac访问控制

    Yii2 是一款非常强大的PHP底层框架, 牛b的人都喜欢用它, 有时候你们可能会发现, Yii2 底层处理不是很好, 比如: 每次分页, yii底层都会多统计一次数据的总条数!  那只能说你对它还不 ...

  4. ubuntu 16.04下如何打造 sublime python编程环境

    一.安装python3     ubuntu自身是安装python2的,例如在ubuntu 16.04中安装的就是python2.7.但我想在python3的环境下进行开发所以就要安装python3. ...

  5. Python爬虫二

    常见的反爬手段和解决思路 1)明确反反爬的主要思路 反反爬的主要思路就是尽可能的去模拟浏览器,浏览器在如何操作,代码中就如何去实现;浏览器先请求了地址url1,保留了cookie在本地,之后请求地址u ...

  6. CentOS7.2下Hadoop2.7.2的集群搭建

    1.基本环境: 操作系统: Centos 7.2.1511 三台虚机: 192.168.163.224  master 192.168.163.225  node1 192.168.163.226   ...

  7. POJ:3041-Asteroids(匈牙利算法模板)

    传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...

  8. poj 2376 选择工作区间问题 贪心算法

    题意:给一些工作区间,如何选取最小的工作数量,覆盖[1,T]的工作时长 一开始的思路,当然也是错误的思路: 我想着,最小工作数量是吧?那肯定就是选择结束时间最晚的,给结束时间来一个排序.哎这个思路错误 ...

  9. bash函数定义/使用/传参…

    函数:function, 功能     过程式编程,代码重用         模块化编程         简洁             语法:         function f_name {    ...

  10. MongoDB学习-->设置通用的自增ID替代ObjectId

    插入mongodb数据时,会为其分配一个随机id,想要设置通用的自增id,可以进行以下操作 1.创建自增序列 package com.tangzhe.autoid; import lombok.Dat ...