[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,非常的好用,推荐!!! 你可以 ...
随机推荐
- nginx url rewrite break和last的区别
break 将重写的URI作为一个新的URI,在本块中继续处理,将重写后 的地址在当前location块中处理,不会将新的URI转向到其他location块中 last,终止继续在本location块 ...
- 【netbeans】【ubuntu】ubuntu netbeans 抗锯齿化修复
每一个在ubuntu下用netbeans的,都会对它的字体怎么会显示的那么难看表示很不理解.我就是因此几乎没有用netbeans的. 不过今天终于解决问题了,虽然没有eclipse显示的那么漂亮, ...
- Yii2 基于rbac访问控制
Yii2 是一款非常强大的PHP底层框架, 牛b的人都喜欢用它, 有时候你们可能会发现, Yii2 底层处理不是很好, 比如: 每次分页, yii底层都会多统计一次数据的总条数! 那只能说你对它还不 ...
- ubuntu 16.04下如何打造 sublime python编程环境
一.安装python3 ubuntu自身是安装python2的,例如在ubuntu 16.04中安装的就是python2.7.但我想在python3的环境下进行开发所以就要安装python3. ...
- Python爬虫二
常见的反爬手段和解决思路 1)明确反反爬的主要思路 反反爬的主要思路就是尽可能的去模拟浏览器,浏览器在如何操作,代码中就如何去实现;浏览器先请求了地址url1,保留了cookie在本地,之后请求地址u ...
- CentOS7.2下Hadoop2.7.2的集群搭建
1.基本环境: 操作系统: Centos 7.2.1511 三台虚机: 192.168.163.224 master 192.168.163.225 node1 192.168.163.226 ...
- POJ:3041-Asteroids(匈牙利算法模板)
传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...
- poj 2376 选择工作区间问题 贪心算法
题意:给一些工作区间,如何选取最小的工作数量,覆盖[1,T]的工作时长 一开始的思路,当然也是错误的思路: 我想着,最小工作数量是吧?那肯定就是选择结束时间最晚的,给结束时间来一个排序.哎这个思路错误 ...
- bash函数定义/使用/传参…
函数:function, 功能 过程式编程,代码重用 模块化编程 简洁 语法: function f_name { ...
- MongoDB学习-->设置通用的自增ID替代ObjectId
插入mongodb数据时,会为其分配一个随机id,想要设置通用的自增id,可以进行以下操作 1.创建自增序列 package com.tangzhe.autoid; import lombok.Dat ...