如何对numpy数组进行序列化/反序列化?
A = np.random.randint(0, 10, 40).reshape(8, 5)
print(A)
print (A.dtype)
snapshot = A
serialized = snapshot.tobytes()
[[9 5 5 7 4]
[3 8 8 1 0]
[5 7 1 0 2]
[2 2 7 1 2]
[2 6 3 5 4]
[7 5 4 8 3]
[2 4 2 4 7]
[3 4 2 6 2]]
int64
返回
deserialized = np.frombuffer(serialized).astype(np.int64)
print (deserialized)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0]
答案 0 :(得分:1)
用于生成dtype
的默认A
与np.frombuffer
中的默认# Python 3.6 64-bits with numpy 1.12.1 64-bits
A = np.random.randint(0, 10, 40).reshape(8, 5)
print(A)
>>> array([[3, 3, 5, 3, 9],
[1, 4, 7, 1, 8],
[1, 7, 4, 3, 0],
[9, 2, 9, 1, 2],
[2, 8, 9, 1, 1],
[3, 3, 5, 2, 6],
[5, 0, 2, 7, 6],
[2, 8, 8, 0, 7]])
A.dtype
>>> dtype('int32')
deserialized = np.frombuffer(A.tobytes(), dtype=np.int32).reshape(A.shape)
print(deserialized)
>>> array([[3, 3, 5, 3, 9],
[1, 4, 7, 1, 8],
[1, 7, 4, 3, 0],
[9, 2, 9, 1, 2],
[2, 8, 9, 1, 1],
[3, 3, 5, 2, 6],
[5, 0, 2, 7, 6],
[2, 8, 8, 0, 7]])
之间不匹配。使用正确的dtype时可以按预期工作(可能取决于计算机/ Python / numpy版本):
//const fetch = require("node-fetch")
const axios = require("axios")
const DisplayFunctions = require('./app/display');
let i = 0;
var requestLoop = setInterval(() => {
try {
i++;
DisplayFunctions.showError("called " + i + " times")
axios.get("https://jsonplaceholder.typicode.com/todos/1")
//fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(() => {
DisplayFunctions.showError("worked " + i + " times")
})
.catch((err) => {
DisplayFunctions.showError(err.message)
})
} catch (err) {
DisplayFunctions.showError(err.message)
}
},1000);