【课程1.6】 Numpy数据的输入输出
numpy读取/写入数组数据、文本数据
1.存储数组数据 .npy文件
import os
os.chdir('C:/Users/Hjx/Desktop/')
ar = np.random.rand(5,5)
print(ar)
np.save('arraydata.npy', ar)
# 也可以直接 np.save('C:/Users/Hjx/Desktop/arraydata.npy', ar)
-----------------------------------------------------------------------
[[ 0.57358458 0.71126411 0.22317828 0.69640773 0.97406015]
[ 0.83007851 0.63460575 0.37424462 0.49711017 0.42822812]
[ 0.51354459 0.96671598 0.21427951 0.91429226 0.00393325]
[ 0.680534 0.31516091 0.79848663 0.35308657 0.21576843]
[ 0.38634472 0.47153005 0.6457086 0.94983697 0.97670458]]
2.读取数组数据 .npy文件
ar_load =np.load('arraydata.npy')
print(ar_load)
# 也可以直接 np.load('C:/Users/Hjx/Desktop/arraydata.npy')
-----------------------------------------------------------------------
3.存储/读取文本文件
ar = np.random.rand(5,5)
np.savetxt('array.txt',ar, delimiter=',')
# np.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# '):存储为文本txt文件
ar_loadtxt = np.loadtxt('array.txt', delimiter=',')
print(ar_loadtxt)
# 也可以直接 np.loadtxt('C:/Users/Hjx/Desktop/array.txt')
-----------------------------------------------------------------------
[[ 0.28280684 0.66188985 0.00372083 0.54051044 0.68553963]
[ 0.9138449 0.37056825 0.62813711 0.83032184 0.70196173]
[ 0.63438739 0.86552157 0.68294764 0.2959724 0.62337767]
[ 0.67411154 0.87678919 0.53732168 0.90366896 0.70480366]
[ 0.00936579 0.32914898 0.30001813 0.66198967 0.04336824