【问题标题】:Read ints from .txt file into numpy array将 .txt 文件中的整数读入 numpy 数组
【发布时间】:2015-11-23 09:32:41
【问题描述】:

我想从这个问题中描述的简单 .txt 数组中读取 4 个整数 genfromtxt : read ints from space separated .txt file

但我希望它是 2D numpy 数组。

def read_data():
    data = np.genfromtxt('Skin_NonSkin.txt', dtype=(int, int, int, int))

    print type(data)
    print data.shape
    print data[0]
    print type(data[0])
    print data[0].shape
    print data[0][1]

    return data

它给了我

<type 'numpy.ndarray'>
(245057L,)
(74, 85, 123, 1)
<type 'numpy.void'>
()
85

那么如何正确读取数据或将其转换为形状为 (245057,4) 的 2D nampy 数组?

【问题讨论】:

    标签: python numpy text genfromtxt


    【解决方案1】:

    只需使用:

    data = np.genfromtxt('Skin_NonSkin.txt', dtype=np.int32)
    

    您正在创建(int,int,int,int) 的一维数组,但您真正想要的是np.int32 的二维数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 2018-03-25
      • 1970-01-01
      • 2019-02-19
      相关资源
      最近更新 更多