[Python Cookbook] Numpy: How to Apply a Function to 1D Slices along the Given Axis
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: |
|
---|---|
Returns: |
|
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate
数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.arr ...
- [Python Cookbook] Numpy Array Manipulation
1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...
- 【Python】numpy 数组拼接、分割
摘自https://docs.scipy.org 1.The Basics 1.1 numpy 数组基础 NumPy’s array class is called ndarray. ndarray. ...
- Python使用numpy实现BP神经网络
Python使用numpy实现BP神经网络 本文完全利用numpy实现一个简单的BP神经网络,由于是做regression而不是classification,因此在这里输出层选取的激励函数就是f(x) ...
- 【python cookbook】找出序列中出现次数最多的元素
问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...
- Python中Numpy及Matplotlib使用
Python中Numpy及Matplotlib使用 1. Jupyter Notebooks 作为小白,我现在使用的python编辑器是Jupyter Notebook,非常的好用,推荐!!! 你可以 ...
随机推荐
- 拖拽图片到另一个div里
HTML代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 【 android】When an app is installed on the external storage
When an app is installed on the external storage: The .apk file is saved to the external storage, bu ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- loadView、viewDidLoad及viewDidUnload的关系(转)
本文目录 一.loadView 二.viewDidLoad 三.viewDidUnload 四.三个方法的关系 标题中所说的3个方法,都是UIViewController的方法,跟UIViewCont ...
- 建立,查询二叉树 hdu 5444
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- UIAutomator输入中文
之前一直是英文的测试环境,包括手机也是英文的,app也是英文的,涉及不到中文输入法的东西.但现在在写中文的app,所以需要输入中文.看到网上的解决办法如下: 下载https://github.com/ ...
- Windows和linux(ubuntu)互传文件简便快捷的方法
现在很多开发和测试的工作环境都是Linux,但测试后期报告的处理一般都是在Windows下完成的,所以需要把结果拿到Windows下. 如果是同一台PC还好些(windows下安装linux的虚拟机, ...
- BZOJ 5441: [Ceoi2018]Cloud computing
背包 #include<cstdio> #include<algorithm> using namespace std; int n,m,Len; long long F[2] ...
- 使用 Button 类在 XNA 中创建图形按钮(九)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- Selenium WebDriver-获取与切换浏览器窗口的句柄
通过selenium webdriver去切换浏览器的窗口,需要通过句柄,具体代码如下: #encoding=utf-8 import unittest import time import char ...