Numpy之文件存取
来源:互联网 发布:国学软件哪个好啊 编辑:程序博客网 时间:2024/05/21 03:55
Numpy提供的文件存取功能。Numpy可以将数组保存至二进制文件、文本文件,同时支持将多个数组保存至一个文件中。
1. np.tofile() & np.fromfile()
import numpy as npimport osos.chdir("d:\\")a = np.arange(0,12)a.reshape(3,4)array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])a.tofile("a.bin") #保存至a.binb = np.fromfile("a.bin", dtype=np.int32) #从文件中加载数组,错误的dtype会导致错误的结果array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])b.reshape(3,4)array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])#读取的数据将为一维数组,需要使用reshape改变其数组结构
2. np.save() & np.load() & np.savez()
load()和save()用Numpy专用的二进制格式保存数据,它们会自动处理元素类型和形状等信息。savez()提供了将多个数组存储至一个文件的能力,调用load()方法返回的对象,可以使用数组名对各个数组进行读取。默认数组名arr_0,arr_1,arr_2......
np.save("a.npy", a.reshape(3,4))c = np.load("a.npy")carray([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])多个数组存储至一个文件:
a = np.array([[1,2,3],[4,5,6]])b = np.arange(0,1.0,0.1)c = np.sin(b)np.savez("result.npz", a, b, sin_arr=c) #使用sin_arr命名数组cr = np.load("result.npz") #加载一次即可r["arr_0"]array([[1, 2, 3], [4, 5, 6]])r["arr_1"]array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])r["sin_arr"]array([ 0. , 0.09983342, 0.19866933, 0.29552021, 0.38941834, 0.47942554, 0.56464247, 0.64421769, 0.71735609, 0.78332691])3. savetxt() & loadtxt()
a = np.arange(0,12,0.5).reshape(4,-1)aarray([[ 0. , 0.5, 1. , 1.5, 2. , 2.5], [ 3. , 3.5, 4. , 4.5, 5. , 5.5], [ 6. , 6.5, 7. , 7.5, 8. , 8.5], [ 9. , 9.5, 10. , 10.5, 11. , 11.5]])np.savetxt("a.txt", a)np.loadtxt("a.txt")array([[ 0. , 0.5, 1. , 1.5, 2. , 2.5], [ 3. , 3.5, 4. , 4.5, 5. , 5.5], [ 6. , 6.5, 7. , 7.5, 8. , 8.5], [ 9. , 9.5, 10. , 10.5, 11. , 11.5]])np.savetxt("a.txt", a, fmt="%d", delimiter=",") #指定存储数据类型为整型,分隔符为,np.loadtxt("a.txt", delimiter=',') #以,分隔符读取array([[ 0., 0., 1., 1., 2., 2.], [ 3., 3., 4., 4., 5., 5.], [ 6., 6., 7., 7., 8., 8.], [ 9., 9., 10., 10., 11., 11.]])
转载自 :http://www.it165.net/pro/html/201406/15606.html
0 0
- Numpy之文件存取
- numpy之文件存取
- Numpy之文件存取
- numpy文件存取
- numpy 文件存取
- Numpy文件存取
- NumPy文件存取
- numpy文件存取
- Numpy文件存取
- numpy 文件存取
- Numpy文件存取
- python:numpy 文件存取
- Numpy学习(1)numpy文件存取
- 【Python学习】NumPy文件存取
- numpy文件存取-npz,npy
- NumPy 文件存取 tofile,fromfile, load,save
- python 科学计算学习一:numpy文件存取,内存映射
- python 科学计算学习一:numpy文件存取,内存映射
- matlab图像处理基础知识2(matlab位运算操作)
- Android判断GPS是否开启和强制帮用户打开GPS
- Activity的启动模式
- Ti:事务的四大特性
- 理解PHP中的stdClass类
- Numpy之文件存取
- [EverString收录]综述 - 【Dr.Elephant源码分析系列文章-1】
- Elasticsearch ——ELK安装
- 用友通10.2标准版_100站点免狗补丁
- Java编程思想(第四版)笔记(二)
- java was started but returned exit code=13
- poj2502 subway dijkstra
- IOS开发之----常用函数和常数
- socket关闭: close()和shutdown()的差异