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.

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.

 import numpy as np
from scipy.ndimage.interpolation import shift
def shift_one_pixel(image, dx,dy):
image=image.reshape(28,28)
image_shifted=shift(image,[dy,dx],cval=0,mode='constant')
return image_shifted.reshape(28*28) 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. 如何用纯 CSS 创作一个小球反弹的动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/OwWROO 可交互视频 ...

  2. jquery图片切换插件jquery.cycle.js参数详解

    转自:国人的力量 blog.163.com/xz551@126/blog/static/821257972012101541835491/ 自从使用了jquery.cycle.js,我觉得再也不用自己 ...

  3. 20.Yii2.0框架多表关联一对多查询之hasMany

    目录 新手模式 hasMany关联模式查询 新建mode层Article.php 新建mode层Category.php 新建控制器HomeController.php 新手模式 用上次的查询结果,作 ...

  4. win7下设置git客户端

    msysgit官网: http://msysgit.github.io/ 下载msysgit http://msysgit.googlecode.com/files/Git-1.8.5.2-previ ...

  5. 自己做一款简易的chrome扩展--清除页面广告

    大家肯定有这样的经历,浏览网页的时候,左右两端广告,诸如“屠龙宝刀,点击就送”,以及最近火的不行的林子聪37传奇霸业什么“霸业面具,霸业吊坠”的魔性广告总是充斥我们的眼球. 当然有现成的扩展程序或者插 ...

  6. 一个JS判断客户端是否已安装某个字体(Only IE)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 算法理论——Linear SVM

    问题引入 下面的三个超平面都起到分类的效果,哪个最好? 答案显然是第三个.为什么? 直觉上,如果现在我们有个测试点,非常靠近右下角的那个红叉叉,也就是说这个点的特征与那个红叉叉非常接近,这时候,我们希 ...

  8. [python学习篇][廖雪峰][1]高级特性--创建生成器 方法1 a = (x for x in range(1,3))

    创建一个生成器的方法: for x in range(1,10000000) ,先生成一个列表[1........9999999] 如果我们只想要后面的几个元素,会发现浪费很多空间.所以,如果列表元素 ...

  9. Set容器——TreeSet及常用API

    TreeSet及常用Api ①   TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储,访问和检索很快; ②   在存储了大量的需要进行快速检索的排序信息的情况下,TreeSe ...

  10. learn资料

    老陈的CSDN博客: http://blog.csdn.net/qq_35587839 1.memcache 和 memcached的区别:http://www.phpweblog.net/fuyon ...