【numpy學習筆記】陣列的儲存和下載

Datawhale發表於2018-07-11

1. Saving array in binary format (.npy)

>>>a = np.array([1,2,3,4,5])
>>>np.save('array_a',a)
>>>np.load('array_a.npy')
array([1, 2, 3, 4, 5])

2. Saving multiple arrays into a zip file

>>>b = np.array([[1,2,3],[4,5,6]])
>>>np.savez('two_arrays.npz',x=b,y=b.T)
>>>np.load('two_arrays.npz')['x']
array([[1, 2, 3],
       [4, 5, 6]])
>>>np.load('two_arrays.npz')['y']
array([[1, 4],
       [2, 5],
       [3, 6]])

3. Saving and loading into text files

>>>b = np.array([[1,2,3],[4,5,6]])
>>>np.savetxt('array_text.txt',b,delimiter=',')
>>>np.loadtxt('array_text.txt',delimiter=',')
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

相關文章