【Python基础】Numpy 低纬高纬数据存储

文章介绍了numpy数组的属性,如ndim,shape,size和dtype,并详细讲解了Python中数据的存储方法,包括numpy的savetxt和loadtxt函数,pickle模块以及scipy的Mat文件读写。此外,还提到了使用json进行数据序列化的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


title: numpy&FileSave
date: 2021-5-26 18:45:45
author: liudongdong1
img: https://cdn.pixabay.com/photo/2021/06/12/12/17/bread-6330807__340.jpg
reprintPolicy: cc_by
cover: false
categories: 编程语言
tags:

  • Python
  • numpy

1. attributes

  • ndarray.ndim: the number of axes (dimensions) of the array.
  • ndarray.shape: For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the number of axes, ndim.
  • ndarray.size: the total number of elements of the array.
  • ndarray.dtype: numpy.int32, numpy.int16, and numpy.float64
  • create: np.array(列表数据)

2. 数据存储

.0. 按行存储
with open(filename,coding):
    data=f.read()
    
with open('result.txt',mode='a+') as f:
    f.write("preds:{},charpre:{},charclassify:{}".format(index,charclass,charpre))  # write 写入

#f.readline()#读取第一行
#f.readlines() #把内容按行读取至一个list
.1. 一/二维数据

numpy.savetxt(fname, X, fmt=‘%.18e’, delimiter=’ ‘, newline=’\n’, header=‘’, footer=‘’, comments='# ', encoding=None)

  • fname:表示要保存文件的地址,可以自己建文件名,如‘test.txt’
  • X:表示要保存的文件
  • fmt: 存储格式,如 %d, %.2f
  • delimiter :分隔符,默认空格,也可以用逗号等
  • newline:表示换行的时候用什么,默认\n,表示换一行,也可以用\t,则表示空四
  • header:表示头文件,如“test_data"
  • footer: 文件下的脚注
  • comment:注释,默认是#,因为python的注释是#,也可以用其它符号

numpy.``loadtxt(fname, dtype=<class ‘float’>, comments=‘#’, delimiter=None**,** converters=None**,** skiprows=0**,** usecols=None**,** unpack=False**,** ndmin=0**,** encoding=‘bytes’, max_rows=None**,** *, like=None)

np.savetxt(filename, list1,fmt='%d',delimiter=',')
dets= np.loadtxt(filename,delimiter=',')
.2. 高维数据
.1. pickle
  • write
import pickle
my_data = {'a': [1, 2.0, 3, 4+6j],
           'b': ('string', u'Unicode string'),
           'c': None}
output = open('data.pkl', 'wb')
pickle.dump(my_data, output)
output.close()

###Extract from file
with open("myfile.pkl","rb") as f:
    x_temp = pickle.load(f)
  • read
import pprint, pickle
pkl_file = open('data.pkl', 'rb')
data1 = pickle.load(pkl_file)
pprint.pprint(data1)
pkl_file.close()

###Load into file
with open("myfile.pkl","wb") as f:
    pickle.dump(x_train,f)
.2. scipy Mat
import numpy as np
import scipy.io

# Some test data
x = np.arange(200).reshape((4,5,10))

# Specify the filename of the .mat file
matfile = 'test_mat.mat'

# Write the array to the mat file. For this to work, the array must be the value
# corresponding to a key name of your choice in a dictionary
scipy.io.savemat(matfile, mdict={'out': x}, oned_as='row')

# For the above line, I specified the kwarg oned_as since python (2.7 with 
# numpy 1.6.1) throws a FutureWarning.  Here, this isn't really necessary 
# since oned_as is a kwarg for dealing with 1-D arrays.

# Now load in the data from the .mat that was just saved
matdata = scipy.io.loadmat(matfile)

# And just to check if the data is the same:
assert np.all(x == matdata['out'])
.3. json
import json
with open(filename, 'w') as f:
   json.dump(myndarray.tolist(), f)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路途…

点滴记录

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值