python - 保存矩阵时出错

标签 python numpy import

我有一个包含字符串、 float 和整数的矩阵,我想将其保存为 csv 文件。这是我使用的命令:

numpy.savetxt("X.csv", X, delimiter=",")

其中 X 是矩阵的名称。这是错误:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
 File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 1073, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: float argument required, not str

这是矩阵的第一行:

16  disk    11  10.29   4.63    30.22 11  20.49   60.60   20.22 11  22.17   0.71    10.37

矩阵类型为 numpy.ndarray。

如何保存?谢谢

最佳答案

在您给出的示例中,矩阵由字符串和 float 组成。那么这个问题已经在这里得到了回答:

How to use python numpy.savetxt to write strings and float number to an ASCII file?

将您的 np.savetxt 调用修改为 numpy.savetxt("X.csv", X, delimiter=",", fmt='%s')

关于python - 保存矩阵时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33334284/

相关文章:

python - 如何加速 Python 中的网络爬行?

python - 计算具有 Nan 值的最频繁组

hadoop - 从 SAP Hana 导入到 Hadoop 期间 Sqoop 导入错误

python - 有没有办法在一个命令中获取 python 中的中位数索引?

python - 从 TFRecords 读取时丢失数据

c - 如何导入 C 库?

python - Py.test 掩码缺少导入

python - 有没有办法让 Google App Engine 打印出所有遇到的 Python 错误?我收到服务器错误,但不知道为什么

Python Selenium 异常 AttributeError : "' Service' object has no attribute 'process' "in selenium. webdriver.ie.service.Service

python - 如何比较持有 numpy.ndarray 的数据类的相等性(bool(a==b) 引发 ValueError)?