我通过tcp接收9个双精度数组,拆分它们,使用np.array()方法转换,将它们存储在列表中,最后再次将此列表转换为numpy数组,这样我就可以保存它,并稍后加载以进行训练keras模型。
每次我重新运行代码时,输出的形状都是随机的(500,6)或(500,), 我什么都没做,只是继续运行相同的代码,我得到了不同的结果。
编辑:我的完整代码:
def tcp_server_get_training_data():
host = "localhost"
port = 5367
msg = ""
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mySocket.bind((host, port))
# print(socket.getfqdn())
# print(socket.gethostbyname(socket.getfqdn()))
mySocket.listen(1)
print('waiting for connection...')
conn, addr = mySocket.accept()
print("Connection from: " + str(addr))
x = []
y = []
i = 0
n = 500
while i < n:
data = conn.recv(128)
doubles_sequence = array.array('d', data)
doubles_sequence2 = np.array(doubles_sequence)
x.append(doubles_sequence2[:6])
y.append(doubles_sequence2[-3:])
i += 1
print(str(round((i/n)*100, 2))+"%")
#print(doubles_sequence[:6])
xp = np.array(x)
yp = np.array(y)
print("X shape: "+str(xp.shape))
print("y shape: "+str(yp.shape))
np.save(file='x', arr=xp)
np.save(file='y', arr=yp)
conn.close()
当我打印形状为(500,6)的x时,我得到:
>>> x.shape
(500, 6)
>>> x
array([[ 0. , 0. , 0. , -0.29219246, 0. , 0. ],
[ 0. , 0. , 0. , 0.34277358, 0. , 0. ],
[ 0. , 0. , 0. , 0.34277358, 0. , 0. ],
当我用形状(500,)打印x时,我得到:
>>> x.shape
(500,)
>>> x
array([array([ 0., 0. , 0. , -0.29219246, 0., 0. ]),
array([ 0. , 0. , 0. , 0.34277358, 0., 0.]),
array([ 0. , 0., 0., -0.10241638, , dtype=object)
我真的很感谢您的帮助,我试图自己解决这个问题,但是几个小时后就感到沮丧。 我在编程方面相对较新,并且在C#上花费了更多时间,在python中,我很困惑,没有类型声明。