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. destoon 信息发布表单提交验证

    sell 模块的form表单如下: <form method="post" id="dform" action="?" target= ...

  2. python的标准模块

    本文用于记录python中的标准模块,随时更新. decimal模块(解决小数循环问题): >>> import decimal >>> a = decimal.D ...

  3. AND和OR

    AND和OR用于组合多个选择条件,即用于组合where之中的多个条件

  4. 关于host,nslookup,dig 的安装

    host,nslookup,dig依赖bind包,所以先看一下系统有没有bind包 命令如下:rpm -qa |grep bind 如果没有或者版本太低请升级安装 命令是:yum install bi ...

  5. Linux任务计划、周期性任务执行

    Linux任务计划.周期性任务执行 周期性任务执行: cron 守护进程(crond):服务,不间断地运行于后台 # service crond {start|stop|status|restart} ...

  6. socketcluster 客户端请求

    <html> <head> <title>test</title> <script src="https://cdn.bootcss.c ...

  7. 九度oj 题目1358:陈博的平均主义

    题目描述: 在JOBDU团队里,陈博是最讲平均主义的人了,但并不是像梁山好汉那样能够做到有钱同花,有肉同吃,毕竟,他还是被家里的领导管着的……陈博的平均主义,就只能体现在他对数字的喜好了.陈博特别喜欢 ...

  8. 设计模式(二 & 三)工厂模式:概述

    工厂 从 coding 的角度来说,在需要创建对象的时候,直接在方法内部使用 new 关键字来创建,是非常方便的. 然而从全局的角度考虑,这样会使对象变得难以管理和控制,代码会变得非常脆弱,缺乏弹性. ...

  9. Kafka单机配置部署

    摘要:上节 学习了Kafka的理论知识,这里安装单机版以便后续的测试. 首先安装jdk 一.单机部署zk 1.1安装: tar -zxf zookeeper-3.4.10.tar.gz -C /opt ...

  10. ubuntu服务器与本地文件传输

    ubuntu SSH 连接.远程上传下载文件 博客分类: Ubuntu   安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH ...