简体   繁体   English

将Numpy矩阵值转换为float32

[英]Convert Numpy matrix values to be float32

I want to enforce the values in the eigenvector matrix below to be of type float32 to save memory space instead of using float64 . 我想将下面的特征向量矩阵中的值强制为float32类型,以节省内存空间,而不是使用float64 However, it seems the way I did below eigenvectors.astype(np.float32) does not work. 但是,似乎我在eigenvectors.astype(np.float32)执行的方法不起作用。 Is there a work around of that? 有没有解决的办法?

import networkx as nx
G = nx.Graph(edges)
L = nx.laplacian_matrix(G)
eigenvalues, eigenvectors.astype(np.float32) = np.linalg.eig(L.todense())

The np.linalg.eig function returns arrays of the same dtype as its parameter. 所述np.linalg.eig函数返回相同的阵列dtype作为其参数。

So you have two choices: 因此,您有两种选择:

  1. Try np.linalg.eig(L.todense().astype(np.float32)) . 尝试np.linalg.eig(L.todense().astype(np.float32)) (This might overflow) (这可能会溢出)
  2. Convert the eigenvectors later. 稍后转换特征向量。

The second option goes like this: 第二个选项是这样的:

eigenvalues, eigenvectors = np.linalg.eig(L.todense())
eigenvectors = eigenvectors.astype(np.float32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM