[英]Error in converting TensorType(float32, matrix) to TensorType(int32, matrix)
[英]Converting Large float64 Matrix into int32 Matrix
我有一個大型數據集,我需要計算歐氏距離矩陣,但是,我受限於RAM並使用np.float64(默認),因為dtype使PC內存不足。 我會使用平方距離,因為它更快,無論如何返回一個整數。
使用.astype(np.int32)無法解決問題,因為它仍然是先創建為float64。
數據集本身是int32但返回的矩陣是float64
matrix = pairwise_distances(dataset, metric='euclidean', squared=True)
print(matrix.dtype)
float64
如何將其直接轉換為int數組?
所以問題是你想讓你的數據RAM高效
你可以將你的返回矩陣轉換為int32(考慮數據中的丟失)或float32,這是更好的我的示例代碼
import numpy as np
arr = np.array([[1.0, 4, 5, 12],
[-5.9, 8, 9.7, 0],
[-6, 7, 11, 19]])
print(arr.dtype)
arr = arr.astype('float32')
print(arr.dtype)
產出如下
float64
float32
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.