如何使用 NumPy 將陣列元素舍入到給定的小數位數?
NumPy 庫的 round() 函式用於將陣列元素舍入到給定的小數位數。NumPy 是一個 Python 庫,用於執行數學運算、陣列操作和建立等。在本文中,我們將使用 round() 函式將陣列元素舍入到給定的小數位數。
方法 1:使用 round() 函式
round() 函式的語法
numpy.round(arr,decimals=0, out=None)
這裡,arr 是要舍入的輸入陣列,decimal 是陣列元素要舍入到的小數位數。預設情況下,decimal 值為 0。out 是舍入原始陣列元素後建立的輸出陣列。
示例 1:舍入單個數組元素
在下面的示例中,我們建立了一個包含單個元素的陣列 arr,然後使用 round() 函式將陣列元素舍入到小數點後 2 位。然後打印出舍入後的陣列元素。
import numpy as np # Create an array arr = np.array([3.14159]) # Round the array element to 2 decimal places rounded_arr = np.round(arr, decimals=2) # Print the rounded array element print(rounded_arr)
輸出
[3.14]
示例:舍入多個數組元素
在下面的示例中,我們建立了一個包含多個元素的陣列 arr,然後使用 round() 函式將陣列元素舍入到小數點後 1 位。然後打印出舍入後的陣列元素。
import numpy as np # Create an array arr = np.array([1.234, 2.345, 3.456, 4.567]) # Round the array elements to 1 decimal place rounded_arr = np.round(arr, decimals=1) # Print the rounded array elements print(rounded_arr)
輸出
[1.2 2.3 3.5 4.6]
示例 3:舍入到最接近的整數
在下面的示例中,我們建立了一個包含多個元素的陣列 arr,然後使用 round() 函式將陣列元素舍入到最接近的整數。然後打印出舍入後的陣列元素。
import numpy as np # Create an array arr = np.array([1.234, 2.345, 3.456, 4.567]) # Define the precision precision = 0.001 # Calculate the multiplier needed to achieve the desired precision multiplier = 1 / precision # Round the array elements to the nearest integer rounded_arr = np.round(arr * multiplier) / multiplier # Print the rounded array elements print(rounded_arr)
輸出
1.234 2.345 3.456 4.567]
示例 4:舍入到指定的精度
在下面的示例中,我們首先將所需的精度定義為變數 precision。然後,我們透過將 1 除以精度來計算實現所需精度的乘數。在舍入之前,我們將陣列元素乘以該乘數,然後將其除以乘數,以獲得所需精度下的舍入值。
import numpy as np # Create an array arr = np.array([1.234, 2.345, 3.456, 4.567]) # Define the precision precision = 0.001 # Calculate the multiplier needed to achieve the desired precision multiplier = 1 / precision # Round the array elements to the nearest integer rounded_arr = np.round(arr * multiplier) / multiplier # Print the rounded array elements print(rounded_arr)
輸出
[1.234 2.345 3.456 4.567]
方法 2:使用 numpy.floor() 方法
numpy.floor 方法返回不大於輸入元素的最大整數。當應用於陣列時,它會將每個元素向下舍入到最接近的整數或小數位,朝負無窮大方向。
語法
numpy.floor(arr, decimals)
這裡,arr 是輸入陣列,decimals(可選)是要舍入到的小數位數。預設為 0。
示例
在下面的示例中,程式碼建立了一個包含浮點值的 NumPy 陣列 arr。然後,它使用 numpy.floor 方法將陣列中每個元素向下舍入到最接近的 0.1(即小數點後 1 位),朝負無窮大方向。舍入後的陣列儲存在名為 rounded_arr 的變數中。最後,使用 print 函式將舍入後的陣列列印到控制檯。
import numpy as np # Create an array arr = np.array([1.234, 2.345, 3.456, 4.567]) # Round down the array elements to 1 decimal place rounded_arr = np.floor(arr * 10) / 10 # Print the rounded array elements print(rounded_arr)
輸出
[1.2 2.3 3.4 4.5]
方法 3:使用 numpy.ceil()
numpy.ceil 方法返回不小於輸入元素的最小整數。當應用於陣列時,它會將每個元素向上舍入到最接近的整數或小數位,朝正無窮大方向。
語法
numpy.ceil(arr, decimals)
這裡,arr 是輸入陣列,decimals(可選)是要舍入到的小數位數。預設為 0。
示例
在下面的示例中,程式碼建立了一個包含浮點值的 NumPy 陣列 arr。然後,它使用 numpy.ceil 方法將陣列中每個元素向上舍入到最接近的 0.1(即小數點後 1 位),朝正無窮大方向。舍入後的陣列儲存在名為 rounded_arr 的變數中。最後,使用 print 函式將舍入後的陣列列印到控制檯。
import numpy as np # Create an array arr = np.array([1.234, 2.345, 3.456, 4.567]) # Round up the array elements to 1 decimal place rounded_arr = np.ceil(arr * 10) / 10 # Print the rounded array elements print(rounded_arr)
輸出
[1.3 2.4 3.5 4.6]
方法 4:使用 numpy.trunc() 方法
numpy.trunc 方法返回輸入元素的整數部分。當應用於陣列時,它透過刪除每個元素的小數部分來舍入到零。
語法
numpy.trunc(arr)
這裡,arr 是包含需要舍入的小數元素的輸入陣列。
示例
在下面的示例中,我們建立了一個包含浮點值的 NumPy 陣列 arr。然後,它使用 numpy.trunc 方法透過刪除值的十進位制部分將陣列中每個元素向下舍入到最接近的整數。截斷後的陣列儲存在名為 truncated_arr 的變數中。最後,使用 print 函式將截斷後的陣列列印到控制檯。
import numpy as np # Create an array arr = np.array([1.234, 2.345, 3.456, 4.567]) # Truncate the array elements to integers truncated_arr = np.trunc(arr) # Print the truncated array elements print(truncated_arr)
輸出
[1. 2. 3. 4.]
結論
在本文中,我們討論瞭如何使用 numpy 將陣列元素舍入到給定的小數位數。NumPy 庫提供了一個 round() 函式來對陣列元素執行舍入操作。我們可以使用 round() 方法將陣列元素舍入到任何所需的小數位數。我們還可以使用 numpy.floor()、numpy.ceil() 和 numpy.trunc() 來舍入 numpy 陣列元素。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP