numpy diff の挙動メモ

import numpy as np
import matplotlib.pyplot as plt


a = np.array([1, 2, 2, 2, 2, 10, 3, 1, 1, 1])
print(f"a, Expected 10 : {len(a)}")
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
x = np.arange(len(a))
ax.plot(x, a)

b = np.diff(a)
print(f"b, Expected 9 (after diff of a) : {len(b)}")
fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1)
x2 = np.arange(len(b))
ax2.plot(x2, b)

c = np.append(0, a)
print(f"c, Expected 11 (after append of a) : {len(c)}")
fig3 = plt.figure()
ax3 = fig3.add_subplot(1, 1, 1)
x3 = np.arange(len(c))
ax3.plot(x3, c)

d = np.diff(c)
print(f"d, Expected 10 (after append of c) : {len(d)}")
fig4 = plt.figure()
ax4 = fig4.add_subplot(1, 1, 1)
x4 = np.arange(len(d))
ax4.plot(x4, d)

plt.show()

この記事が気に入ったらサポートをしてみませんか?