Saving and sharing your numpy arrays(将你的numpy arrays保存下来并分享)

学习如何保存和分享numpy arrays

学啥?

  • 将numpy arrays保存成压缩文件或者我们能看懂的文件,例如:csv类型文件;
  • 加载文件到自己的Numpy 工作区。

怎么做?

  • 创建两个1D arrays 和一个2D array
  • 将arrays保存为可存储的文件
  • 将变量从工作空间中期初
  • 从文件中加载变量
  • 对比zipped binary files和huma-readabke delimited files
  • 完成保存,加载,分享Numpy arrays

前提

  • Numpy

  • 工作路径的读写权限

  • step 1.创建 arrays

x = np.arange(0,10,1)
y = x ** 2
print(x),print(y)
# 输出[0 1 2 3 4 5 6 7 8 9]  [0 1 4 9 16 25 36 49 64 81]

Note:x,y 都暂时保存在工作空间,接下来先将xy保存到本地文件中再将其删除

  • step 2.使用 numpy’s savaz 将arrays保存下来
np.savaz("x_y-squared.npz",x_axis=x, y_axis=y)
# x_axis=x,y_axis=y 用来标记x轴上对应的是x的值,y轴上对应的是y的值
  • step 3.移除以保存的arrays,将本地npz文件导入到空座空间
    1.从工作空间中移除变量使用del
    2.从给定url中导入变量到工作空间中使用np.load
del x,y #1
# %whos To see what variables are in the workspace
%whos
load_xy = np.load("x_y-squared.npz")
print(load_xy.files) # ['x_axis', 'y_axis']
  • step 4.将NpzFile的arrays 重新分配给x,y
    1.前面已经创建了一个NpzFile类型的字典,其中包含了再savez命令中定义的x_axis 和 y_axis。
x = load_xy["x_axis"]
y = load_xy["y_axis"]
print(x),print(y)

以上,使用np.arange,np.savea,del,np.load完成了创建,保存,删除以及加载变量,nice work.

saving to human-readable csv

让我们考虑另一种情况,你想与其他人或其他程序分享x和y。你可能需要人类可读的文本文件,这样更容易分享。接下来,你使用savetxt将x和y保存在一个逗号分隔值文件中,即x_y-squared.csv。产生的csv是由ASCII字符组成的。你可以将该文件加载回NumPy中,或者用其他程序读取。

  • step 1.将数据重新排列成一个单一的2D矩阵
# 1. 将两个1D的arrays重组为一个2D的matrix csv-文件类型是一个电子表格式的数据集
#np.block:appends arrays together into a 2D array
#np.newaxis:forces the 1D array into a 2D column vector with 10 rows and 1 column.将行向量转换为列向量
ayyay_out = np.block([x[:, np.newaxis], y[:, np.newaxis]])
print("the output array has shape ",array_out.shape,"with values:")
print(array_out)
the output array has shape  (10, 2)  with values:
[[ 0  0]
 [ 1  1]
 [ 2  4]
 [ 3  9]
 [ 4 16]
 [ 5 25]
 [ 6 36]
 [ 7 49]
 [ 8 64]
 [ 9 81]]
  • step 2.使用savetxt 将数据保存为csv格式的文件
np.savetxt("x_y-squared.csv",X=array_out,header="x,y",delimiter=",")
# use savetxt with a three option to make your file easier to read:
# X = array_out:to tells savetxt to save your 2D array,array_out,to file x_y-squared.csv
#header = 'x,y':writes a header before any data that labels the columns of the csv
# delimiter=',' tell savetxt to place a comma between each column in the file 

open the file,you’ll see the following

 x, y
0.000000000000000000e+00,0.000000000000000000e+00
1.000000000000000000e+00,1.000000000000000000e+00
2.000000000000000000e+00,4.000000000000000000e+00
3.000000000000000000e+00,9.000000000000000000e+00
4.000000000000000000e+00,1.600000000000000000e+01
5.000000000000000000e+00,2.500000000000000000e+01
6.000000000000000000e+00,3.600000000000000000e+01
7.000000000000000000e+00,4.900000000000000000e+01
8.000000000000000000e+00,6.400000000000000000e+01
9.000000000000000000e+00,8.100000000000000000e+01
  • step 3.查看csv文件的arrays
    Note
    1.numpy use # to ignore heading when using loadtxt,or use skiprows = <number_of_header_lines>
    2.整数是使用科学计数法写的。The integers were written in scientific notation.可以使用fmt = 来指定format of the text,但数据依然会以ASCII字符的形式来保存。
# delete x and y again and assign them to your columns in x_y-squared.csv
del x, y
load_xy = np.loadtxt("x_y-squared.csv",delimiter=",")
load_xy.shape # (10,2)
x = load_xy[:,0]
y=laod_xy[:,1]

学习网站:https://numpy.org/numpy-tutorials/content/save-load-arrays.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值