Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

แปลงมุมจากองศาเป็นเรเดียนด้วย numpy.deg2rad() ใน Python

เพื่อแปลงอาร์เรย์มุมจากองศาเป็นเรเดียน ให้ใช้เมธอด numpy.deg2rad() ใน NumPy เมธอดนี้คืนค่ามุมที่ตรงกันในหน่วยเรเดียน หาก x เป็นสเกลาร์ ผลลัพธ์ก็จะเป็นสเกลาร์

พารามิเตอร์สำคัญ

  • x (พารามิเตอร์แรก): มุมอินพุตในหน่วยองศา
  • out (พารามิเตอร์ที่สอง - ไม่บังคับ): ตำแหน่งเก็บผลลัพธ์ ต้องมีรูปร่างที่ broadcast ได้ หากไม่ระบุ จะสร้างอาร์เรย์ใหม่
  • where (พารามิเตอร์ที่สาม - ไม่บังคับ): เงื่อนไข broadcast ที่ True จะตั้งค่า ufunc ที่ False จะเก็บค่าเดิม

ขั้นตอนการใช้งาน

import numpy as np

สร้างอาร์เรย์และตรวจสอบคุณสมบัติ

# สร้างอาร์เรย์ 0-11
arr = np.arange(12.)
print("Array...\n", arr)
print("\nArray datatype...\n", arr.dtype)
print("\nArray Dimensions...\n", arr.ndim)
print("\nNumber of elements in the Array...\n", arr.size)

แปลงจากองศาเป็นเรเดียน

# สร้างอาร์เรย์องศา (คูณ 30)
res = arr * 30
# แปลงเป็นเรเดียน
print("\nDegree array to radians...\n", np.deg2rad(res))

โค้ดตัวอย่างสมบูรณ์

import numpy as np

# Create an Array
arr = np.arange(12.)
# Display the array
print("Array...\n", arr)
# Get the type of the array
print("\nOur Array type...\n", arr.dtype)
# Get the dimensions of the Array
print("\nOur Array Dimensions...\n", arr.ndim)
# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)
# Degree array
res = arr * 30
# Convert degree array to radians
print("\nDegree array to radians...\n", np.deg2rad(res))

ผลลัพธ์ที่คาดหวัง

Array...
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11.]
Our Array type...
float64
Our Array Dimensions...
1
Number of elements...
12
Degree array to radians...
[0.         0.52359878 1.04719755 1.57079633 2.0943951  2.61799388
 3.14159265 3.66519143 4.1887902  4.71238898 5.23598776 5.75958653]