Numpy Memmap无法从.npy恢复numpy数组

时间:2019-10-30 21:23:14

标签: numpy

问题

请帮助找到一种使用Numpy memmap从.npy文件恢复numpy数组的方法。

MNIST数据

{
  xtype: 'widgetcolumn',
  text: 'Applicant Name',
  dataIndex: 'applicantName',
  width: '17%',
  widget: {
    completeOnEnter: false,
    xtype: 'textfield',
    margin: '0 0',
    allowBlank: false,
    maxLength: 100,
    enforceMaxLength: true,
    validator: function (val) {
      return Ext.isEmpty(val) ? 'Applicant name is required' : true;
    }
  }
}

保存到.npy

from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)

# Split data into training and test
X, y = mnist["data"], mnist["target"]
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
print(X_train[9179][599])
-----> 200.0   

从npy恢复到memmap

import numpy as np
np.save("X_train.npy", X_train)

numpy加载

如果使用numpy加载,则可以恢复数据。因此,它与内存映射有关。

X_mm = np.memmap('X_train.npy', dtype=type(X_train[0][0]), mode="r", shape=(X_train.shape))
print(X_mm[9179][599])
-----> 0.0  (data is not restored)

from numpy.lib.format import open_memmap
X_mm = open_memmap('X_train.npy', mode='r', dtype=type(X_train[0][0]), shape=(X_train.shape))
X_mm[9179][599]
-----> 0.0  (data is not restored)

0 个答案:

没有答案