numpy中结构化数组的形状 [英] Shape of a structured array in numpy

查看:65
本文介绍了numpy中结构化数组的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试预分配一个空数组,同时使用以下代码定义大小为19x5的数据类型:

I am trying to preallocate an empty array and at the same time defining the data type with a size of 19x5 using the following code:

import numpy as np
arr=np.empty((19,5),dtype=[('a','|S1'),('b', 'f4'),('c', 'i'),('d', 'f4'),('e', 'f4')])

结果有些出乎意料,产生了19 * 5 * 5的数组. 但是,尝试:

The result is somewhat unexpected, yielding a 19*5*5 array. However, trying:

arr=np.empty((19,1),dtype=[('a','|S1'),('b', 'f4'),('c', 'i'),('d', 'f4'),('e', 'f4')])

赋予每行适当的长度(5个字段),这看起来像是一维数组.

gives the proper length per row (5 fields), which apparently looks like a 1D array.

当我尝试编写此代码时,仅允许使用以下格式:

When I am trying to write this, only this formatting is allowed:

np.savetxt(file, arr, delimiter=',', fmt='%s')

这告诉我我正在处理单个字符串. 有没有办法获得未展平的19x5形状的结构化数组?

This tells me I am dealing with a single string. Is there no way to get a 19x5 shaped structured array that is not flattened?

使用savetxt编写此文件时会出现主要问题.我想要一个具有所有5列值的csv文件.由于将其作为字符串处理,因此输出错误.

The main problem arises when writing this with savetxt. I want to have a csv file that has all the 5 column values. As this is handled as a string it gives the wrong output.

推荐答案

通常,结构化数组的字段替换2d数组的列.人们经常用genfromtxt加载一个csv,并且想知道为什么结果是1d.如您所见,您可以使用化合物dtype制作二维数组,但是每个元素将具有多个值-由dtype指定.

Typically the fields of a structured array replace the columns of a 2d array. Often people load a csv with genfromtxt and wonder why the result is 1d. As you found you can make a 2d array with a compound dtype, but each element will have multiple values - as specified by the dtype.

通常,您会使用一维形状初始化该数组,例如(19,).

Normally you'd initialize that array with a 1d shape, e.g. (19,).

请注意,您必须按字段或元组列表填充值.

Note that you have to fill values by field or with a list of tuples.

我没有将savetxt与结构化数组一起使用的经验,并且无法在此平板电脑上运行测试.但是可能有一些问题会有所帮助.

I don't have experience using savetxt with a structured array, and can't run tests on this tablet. But there probably are SO questions that help.

savetxt在数组上进行迭代,并写入fmt%tuple(row),其中fmt是根据您的输入构建的.

savetxt iterates on an array, and writes fmt%tuple(row), where fmt is built from your input.

我建议为dtype中的每个字段尝试fmt='%s %s. %s. %s %s'-一种%格式.查看其文档.我也不知道(19,)数组是否会比(19,1)更好.

I'd suggest trying fmt='%s %s. %s. %s %s' - a % format for each field in the dtype. See its docs. Also I don't know if a (19,) array will behave better than a (19,1).

使用数组的格式化元素进行实验.对于格式化程序,它们应该看起来像元组.如果没有,请尝试tolist()tuple(A[0]).

Experiment with formatting elements of your array. They should look like tuples to the formatter. If not try tolist() or tuple(A[0]).

这里的答案几乎可以重复了

Here's answer that is almost good enough to be a duplicate

https://stackoverflow.com/a/35209070/901925

 ab = np.zeros(names.size, dtype=[('var1', 'S6'), ('var2', float)])
 np.savetxt('test.txt', ab, fmt="%10s %10.3f")

==================

===================

savetxt只能处理一维结构化数组.

savetxt can only handle a 1d structured array, because of the tuple formatting.

这篇关于numpy中结构化数组的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆