将数组,矩阵存入csv文件中
我们在做各种模型训练时,往往会先将数据处理成矩阵,然后交给建模的人去训练。这时通常数据清洗者提交的是保存了矩阵的文件,一般为TXT或csv,接下来主要讲解我在实现这个过程中遇到的一些问题。
import numpy
numpy.savetxt('new.csv', my_matrix, delimiter=',')
看上面这段d代码,通常我们会直接使用上面这两句代码来进行保存。但通常会遇到下面两类错误:
1. 类型匹配错误
如上面截图。原因是:loadtxt()这个函数本身在默认情况下认为数据元素都是数值型的,所以用%.18e的格式去读取。所以当遇到str或其他非数值型时就会出错。这里是的指定数据类型解决的。正确代码如下:
import numpy
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 formatted
-
like ‘ (%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 real
-
and 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].
将数组,矩阵存入csv文件中的更多相关文章
- C++ 把数组数据存入 CSV 文件,以及读取 CSV 文件的数据
1. CSV-百度百科 2. 代码 #pragma once //Microsoft Visual Studio 2015 Enterprise #include<iostream> #i ...
- Jmeter自动化测试 数据驱动测试,将数据存入csv文件中来调用,或将数据存在DB中进行调用
1. 将测试的用例名称,测试请求方式,测试链接,预置数据,断言等都放到excel中,然后转成csv格式,在用Jmeter带的csv数据配置文件导入 运行之前将线程组中配置,线程数设置为1,循环的次数设 ...
- MySQL添加CSV文件中的数据
一.MySQL添加csv数据 此问题是前几天整理数据的时候碰到的,数据存在 CSV文件中(200多万记录),通过python 往数据库中导入太慢了,后来使用MySQL 中自带的命令 LOAD DATA ...
- 直接把数据库中的数据保存在CSV文件中
今天突然去聊就来写一个小小的demo喽,嘿嘿 public partial class Form1 : Form { public Form1() { InitializeComponent(); } ...
- C# 将List中的数据导入csv文件中
//http://www.cnblogs.com/mingmingruyuedlut/archive/2013/01/20/2849906.html C# 将List中的数据导入csv文件中 将数 ...
- 取出csv文件中的中文评论数据
# -*- coding: utf-8 -*- import csv import re csvfile = 'weibo.csv' def columns_data(path, column): c ...
- 将数组打印到txt文件中
用print_r 将数组打印到txt文件中. 1.function save_log($content='', $file='app') { $logDir = './logs'; $now ...
- 将CSV文件中的数据导入到SQL Server 数据库中
导入数据时,需要注意 CSV 文件中的数据是否包含逗号以及双引号,存在时,导入会失败 选择数据库 -> 右键 -> 任务 -> 导入数据 ,然后根据弹出的导入导出向导(如下图)中的提 ...
- Jmeter实现从csv文件中随机读取数据
一.需求 参数放在csv文件中,文件格式如下,需求每次从文件中随机读取一行数据. 二.步骤 1.在csv文件中新增加一列,pl 2.新增一个配置原件-随机数,设置如下: 50是文件数据的行数 3.新增 ...
随机推荐
- 求1000以内的质数c语言
之前在做求1000以内的质数的时候,我们一般能想到的就是从3~(根号)no,逐一和no除,如果存在某个i使得 i|no成立的话,说明no不是质数(“i|no”是i整除除no的意思): 在<明解 ...
- Qt基本布局(QLayout)
概述 Qt提供了QHBoxLayout类(水平排列布局),QVBoxLayout类(垂直排列布局),QGridLayout类(网格排列布局)等基本布局管理.它们之间的继承关系如下图 布局中常用的方法有 ...
- 牛客网NOIP赛前集训营-普及组(第一场)C 括号
括号 思路: dp 状态:dp[i][j]表示到i位置为止未匹配的 '(' 个数为j的方案数 状态转移: 如果s[i] == '(' dp[i][j] = dp[i-1][j] + dp[i-1][j ...
- Notepad++ 的资源管理器 右键菜单
以前装的版本,右键[Edit With Notepad++]都可以出来的. 最近安装的总是不行. 不知道是Windows的原因,还是新版本的原因. 网上也都是用workaround去解决的. 免费的东 ...
- Linux(centos7)上安装最新版R3.4.1
说来惭愧,居然没有在Linux安装R的经验,因为一直很少用R,用也是在win平台. 下载路径:https://cran.rstudio.com/src/base/R-3/ 强烈建议不要安装最新的R,除 ...
- .NET的关于人脸识别引擎分享(C#)
https://www.cnblogs.com/RainbowInTheSky/p/10247921.html
- Weighted Channel Dropout for Regularization of Deep Convolutional Neural Network
这是AAAI2019的一篇论文,主要是为了解决小数据集的过拟合问题,使用了针对于卷积层的Dropout的方法. 论文的要点记录于下: 1.在训练过程中对于卷积层的channels进行droipout, ...
- 性能测试工具 Web Service 性能测试工具比较
[转自]https://testerhome.com/topics/3003 背景 希望选择一款Web Service性能测试工具,能真实模拟大量用户访问网站时的请求,从而获取服务器当前的请求处理能力 ...
- 【IDEA】【6】Maven打包
1,打包成jar包 右侧工具栏Maven Projects->项目名称->Lifecycle->package 2,打包时去掉test 右侧工具栏Maven Projects,打开后 ...
- 关于Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.的解决方案
Warning: setState(...): Can only update a mounted or mounting component. This usually means you call ...