[Tips] First-order difference calculation and inverse conversion [python / numpy]

After investigating, I found that it can be easily realized with the numpy function, so that memo.

 import numpy as np

"""
1st floor difference
"""
x = np.array(range(10, 100)) #Suitable vector

x0 = x[0] #Save initial value
x_diff1 = np.diff(x)

"""
Inverse conversion
"""
tmp = np.concatenate(([x0], x_diff1))
#Or below
# tmp = np.r_[x[0],x_diff1]
x_ = np.cumsum(tmp)

"""
check
"""
np.all(x == x_)

For the most part, numpy's functions do the trick. I'm still not familiar with it, but I want to gradually remember it.

Miscellaneous notes

I'm planning to run stats models in the sckit-learn pipeline, including this pre-processing, and implement it neatly, but it's awkward that the interface does not fit subtly. I'm hoping that by putting it on the pipeline, the logic changes (parameter changes, execution order changes, method changes, etc.) will be more flexible, but isn't it going smoothly?

I want to know if there is an easy way. ..

reference

http://qiita.com/sotetsuk/items/d0e73afdcffdc8ac3e6b

Recommended Posts

[Tips] First-order difference calculation and inverse conversion [python / numpy]
Python and numpy tips
python numpy array calculation
[python] Calculation of months and years of difference in datetime
Python / Numpy np.newaxis thinking tips
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between == and is in python
Difference between Numpy randint and Random randint
Difference between python2 series and python3 series dict.keys ()
Python / Numpy> Link> Difference between numpy.random and random.random> thread-safe or not
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Bilinear interpolation function when performing nonlinear coordinate conversion with Python and Numpy
difference between statements (statements) and expressions (expressions) in Python
Difference between PHP and Python finally and exit
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Python] Difference between class method and static method
Find and check inverse matrix in Python
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
Python and NumPy numeric type inheritance relationship
python tips
numpy tips
Python Tips
Python tips
Install and run Python3.5 + NumPy + SciPy on Windows 10
Python numpy ignores very small values ​​and displays
[Python] Swapping rows and columns in Numpy data
[python] Difference between variables and self. Variables in class
[Python] Conversion memo between time data and numerical data
About the difference between "==" and "is" in python
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Identity matrix and inverse matrix: Linear algebra in Python <4>