【问题标题】:Numpy has no attribute loadtxt?Numpy没有属性loadtxt?
【发布时间】:2018-07-29 10:41:44
【问题描述】:

下面的代码:

import numpy as np
f = np.loadtxt('ex21_votes.csv', delimiter=",")

我收到一个错误:module 'numpy' has no attribute 'loadtxt'.

今天安装了numpy,输出如下:

git clone https://github.com/numpy/numpy.git numpy
Cloning into 'numpy'...
remote: Counting objects: 137888, done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 137888 (delta 13), reused 19 (delta 10), pack-reused 137854
Receiving objects: 100% (137888/137888), 57.00 MiB | 1.18 MiB/s, done.
Resolving deltas: 100% (109223/109223), done.
Checking connectivity... done.

【问题讨论】:

  • 请向我们展示np.__file__ 的输出。看起来您正在加载的 numpy 模块太旧,或者不是真正的 numpy,而是另一个名为 numpy.py 的文件,如 loadtxt has been in numpy for a long time
  • 你可能需要更新 numpy.在 Ubuntu 上:sudo apt-get install python-numpy
  • 编译 Numpy 的任何具体原因?否则我建议通过 pip 或包管理器安装。
  • 您是克隆了 numpy 存储库还是关注了all the instructions
  • 你有自己的脚本numpy.py吗?

标签: python numpy python-import


【解决方案1】:

就像this answer 显示您可以使用 genfromtxt 代替。是比loadtxt更通用的方法:

import numpy as np print np.genfromtxt('col.txt',dtype='str')

使用文件 col.txt:

foo bar cat dog man wine

这给出了:

[['foo' 'bar'] ['cat' 'dog'] ['man' 'wine']]

如果您希望每行具有相同的列数,请读取第一行并设置属性filling_values 以修复任何缺失的行。

【讨论】:

  • 这确实不能解决OP的numpy模块缺少should be there的方法的问题。
  • @IonicSolutions 我知道,但我建议将此作为替代方法,因为其他答案说这是一种更通用的方法。
  • 没错,但这不是对 OP 问题的回答。他们的numpy 显然有问题/不一致。
  • 如果 OP 导入的模块没有loadtxt,它可能不会有genfromtxt
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2021-12-01
  • 2019-08-02
  • 1970-01-01
  • 2017-09-20
  • 2018-10-22
  • 2020-08-11
相关资源
最近更新 更多