ในการคำนวณผลต่างที่ไม่ต่อเนื่องลำดับที่ n ให้ใช้วิธีการ numpy.diff() ความแตกต่างแรกถูกกำหนดโดย out[i] =a[i+1] - a[i] ตามแกนที่กำหนด ความแตกต่างที่สูงขึ้นจะคำนวณโดยใช้ diff แบบเรียกซ้ำ พารามิเตอร์ที่ 1 คืออาร์เรย์อินพุต พารามิเตอร์ตัวที่ 2 คือ n นั่นคือจำนวนครั้งที่ค่าต่างกัน หากเป็นศูนย์ อินพุตจะถูกส่งกลับตามที่เป็นอยู่ พารามิเตอร์ตัวที่ 3 คือแกนที่ใช้ค่าความต่าง ค่าดีฟอลต์คือแกนสุดท้าย
พารามิเตอร์ตัวที่ 4 คือค่าที่จะเติมหรือต่อท้ายอาร์เรย์อินพุตตามแนวแกนก่อนที่จะสร้างความแตกต่าง ค่าสเกลาร์จะขยายไปยังอาร์เรย์ที่มีความยาว 1 ในทิศทางของแกนและรูปร่างของอาร์เรย์อินพุตจะขยายไปตามแกนอื่นๆ ทั้งหมด มิฉะนั้น ขนาดและรูปร่างจะต้องตรงกับ a ยกเว้นตามแนวแกน
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
การสร้างอาร์เรย์ numpy โดยใช้เมธอด array() เราได้เพิ่มองค์ประกอบของประเภทที่ไม่ได้ลงนามแล้ว อาร์เรย์จำนวนเต็ม Forunsigned ผลลัพธ์จะไม่ได้รับการลงนาม -
arr = np.array([1,0], dtype=np.uint8)
แสดงอาร์เรย์ -
print("Our Array...\n",arr) ตรวจสอบขนาด -
print("\nDimensions of our Array...\n",arr.ndim)
รับประเภทข้อมูล -
print("\nDatatype of our Array object...\n",arr.dtype) ในการคำนวณผลต่างที่ไม่ต่อเนื่องลำดับที่ n ให้ใช้วิธีการ numpy.diff() ความแตกต่างแรกถูกกำหนดโดย [i] =a[i+1] - a[i] ตามแกนที่กำหนด ความแตกต่างที่สูงขึ้นคำนวณโดยใช้ diff แบบเรียกซ้ำ -
print("\nDiscrete difference..\n",np.diff(arr))
ตัวอย่าง
import numpy as np
# Creating a numpy array using the array() method
# We have added elements of unsigned type
# For unsigned integer arrays, the results will also be unsigned.
arr = np.array([1,0], dtype=np.uint8)
# Display the array
print("Our Array...\n",arr)
# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)
# To calculate the n-th discrete difference, use the numpy.diff() method
# The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively.
print("\nDiscrete difference..\n",np.diff(arr)) ผลลัพธ์
Our Array... [1 0] Dimensions of our Array... 1 Datatype of our Array object... uint8 Discrete difference.. [255]