java csv 数组_将数组,矩阵存入csv文件中

本文主要讨论在使用Python的numpy库处理数据时,将数组和矩阵保存到CSV文件中可能遇到的问题。包括类型匹配错误,由于数据中包含逗号导致的部分数据丢失。解决方案是通过指定数据类型和处理逗号分隔符。详细介绍了numpy.savetxt函数的使用,包括fmt参数的设置以适应不同数据类型的保存需求。
摘要由CSDN通过智能技术生成

我们在做各种模型训练时,往往会先将数据处理成矩阵,然后交给建模的人去训练。这时通常数据清洗者提交的是保存了矩阵的文件,一般为TXT或csv,接下来主要讲解我在实现这个过程中遇到的一些问题。

importnumpy

numpy.savetxt('new.csv', my_matrix, delimiter=',')

看上面这段d代码,通常我们会直接使用上面这两句代码来进行保存。但通常会遇到下面两类错误:

1. 类型匹配错误

f2ff1b79d0531016211cfd346f16d38c.png

如上面截图。原因是:loadtxt()这个函数本身在默认情况下认为数据元素都是数值型的,所以用%.18e的格式去读取。所以当遇到str或其他非数值型时就会出错。这里是的指定数据类型解决的。正确代码如下:

importnumpy

numpy.savetxt('new.csv', my_matrix, fmt='%s', delimiter=',')

2. 存入csv文件时,部分数据丢失

原因,csv默认是用逗号分隔的,所以如果你的数据中含有逗号,就会被它识别,因此在存入之前,应先将逗号进行提换或者加转义字符。

这里补充一下saveTXT的原型:

numpy.savetxt

numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ')[source]Save an array to a text file.

Parameters:

fname : filename or file handle

If the filename ends in .gz, the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.

X : array_like

Data to be saved to a text file.

fmt : str or sequence of strs, optional

A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. ‘Iteration %d – %10.5f’, in which case delimiter is ignored. For complex X, the legal options for fmt are:

a single specifier, fmt=’%.4e’, resulting in numbers formattedlike ‘ (%s+%sj)’ % (fmt, fmt)

a full string specifying every real and imaginary part, e.g.‘ %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej’ for 3 columns

a list of specifiers, one per column - in this case, the realand imaginary part must have separate specifiers, e.g. [‘%.3e + %.3ej’, ‘(%.15e%+.15ej)’] for 2 columns

delimiter : str, optional

String or character separating columns.

newline : str, optional

String or character separating lines.

New in version 1.5.0.

header : str, optional

String that will be written at the beginning of the file.

New in version 1.7.0.

footer : str, optional

String that will be written at the end of the file.

New in version 1.7.0.

comments : str, optional

String that will be prepended to the header and footer strings, to mark them as comments. Default: ‘# ‘, as expected by e.g. numpy.loadtxt.

New in version 1.7.0.

Notes

Further explanation of the fmt parameter (%[flag]width[.precision]specifier):

flags:- : left justify

+ : Forces to precede result with + or -.

0 : Left pad the number with zeros instead of space (see width).width:Minimum number of characters to be printed. The value is not truncated if it has more characters.precision:For integer specifiers (eg. d,i,o,x), the minimum number of digits.

For e, E and f specifiers, the number of digits to print after the decimal point.

For g and G, the maximum number of significant digits.

For s, the maximum number of characters.specifiers:c : character

d or i : signed decimal integer

e or E : scientific notation with e or E.

f : decimal floating point

g,G : use the shorter of e,E or f

o : signed octal

s : string of characters

u : unsigned decimal integer

x,X : unsigned hexadecimal integer

This explanation of fmt is not complete, for an exhaustive specification see [R280].

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值