我有大量从 python 编写的 numpy .npz 格式的数据文件。出于几个原因,我想将它们直接读入 C#。
数据文件包含许多不同类型的一维数组——一些是字节数组,还有一些是双精度数组。谁能给我一些关于如何实现这一目标的建议?或者我可能在下面做错了什么?
我曾尝试使用 Accord.NET.NPZFormat 但无法弄清楚如何使其工作。我想可能是因为你必须给它一个返回类型,并且因为数组的类型不同,它会失败。这是一个链接: http ://accord-framework.net/docs/html/M_Accord_IO_NpzFormat_Load__1.htm
我在这里与语法作斗争,不确定使用什么作为“T”。我得到的最接近的是以下内容,但结果中似乎没有任何数据。Accord.IO 没有示例代码。
public static void LoadNPZ(string zip_file, string npz_file)
{
byte[] ret = new byte[0];
using (ZipArchive zip = ZipFile.OpenRead(zip_file))
{
foreach (ZipArchiveEntry entry in zip.Entries)
{
if (entry.Name == npz_file + ".npz")
{
Stream fs = entry.Open();
ret = new byte[fs.Length];
fs.Read(ret, 0, (int)fs.Length);
}
}
}
if (ret.Length==0)
{
return;
}
var ret2 = NpzFormat.Load<object[]>(ret);
};