[英]Convert elements of a matrix into floating numbers
這是我正在處理的 data_1 的輸出:
print(data_1)
[['1.0' '1.585' '1.78']
['1.0' '1.908' '1.816']
['1.0' '1.7' '1.732']
...,
['1.0' '1.046' '4.824']
['1.0' '1.138' '4.874']
['1.0' '1.016' '5']]
我試圖通過簡單地使用以下代碼將元素轉換為浮點數:
data_2 = float(data_1)
但是我收到了以下錯誤
TypeError: only length-1 arrays can be converted to Python scalars
如何將此矩陣的元素轉換為浮點數?
好像你有一個 numpy 數組, float
是一個基本的 python 函數,它只接受標量作為參數,即它只能將標量轉換為浮點數。 對於 numpy 數組,有一個內置的astype
函數可用於類型轉換:
data_1.astype(float)
# array([[ 1. , 1.585, 1.78 ],
# [ 1. , 1.908, 1.816],
# [ 1. , 1.7 , 1.732],
# [ 1. , 1.046, 4.824],
# [ 1. , 1.138, 4.874],
# [ 1. , 1.016, 5. ]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.