numpy的阵列的访问列?错误试图通过移调或列访问攻略 [英] Access Columns of Numpy Array? Errors Trying to Do by Transpose or by Column Access

查看:135
本文介绍了numpy的阵列的访问列?错误试图通过移调或列访问攻略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy.ndarray其中我想访问的列。我将在8个后采取的所有列,并测试他们的差异,消除列,如果方差/平均值为低。为了做到这一点,我需要访问的列,$ P $与numpy的pferably。根据我目前的方法,我遇到错误或不转。

I've a numpy.ndarray the columns of which I'd like to access. I will be taking all columns after 8 and testing them for variance, removing the column if the variance/average is low. In order to do this, I need access to the columns, preferably with Numpy. By my current methods, I encounter errors or failure to transpose.

要开采这些数组,我现在用的是IOPro适配器,这让正规numpy.ndarray。

To mine these arrays, I am using the IOPro adapter, which gives a regular numpy.ndarray.

import iopro
import sys

adapter = iopro.text_adapter(sys.argv[1], parser='csv')
all_data = adapter[:]
z_matrix = adapter[range(8,len(all_data[0]))][1:3]

print type(z_matrix) #check type
print z_matrix # print array
print z_matrix.transpose() # attempt transpose (fails)
print z_matrix[:,0] # attempt access by column (fails)

有人能解释发生了什么事?

Can someone explain what is happening?

的输出是这样的:

<type 'numpy.ndarray'>
[ (18.712, 64.903, -10.205, -1.346, 0.319, -0.654, 1.52398, 114.495, -75.2488, 1.52184, 111.31, 175.
408, 1.52256, 111.699, -128.141, 1.49227, 111.985, -138.173)
 (17.679, 48.015, -3.152, 0.848, 1.239, -0.3, 1.52975, 113.963, -50.0622, 1.52708, 112.335, -57.4621
, 1.52603, 111.685, -161.098, 1.49204, 113.406, -66.5854)]
[ (18.712, 64.903, -10.205, -1.346, 0.319, -0.654, 1.52398, 114.495, -75.2488, 1.52184, 111.31, 175.
408, 1.52256, 111.699, -128.141, 1.49227, 111.985, -138.173)
 (17.679, 48.015, -3.152, 0.848, 1.239, -0.3, 1.52975, 113.963, -50.0622, 1.52708, 112.335, -57.4621
, 1.52603, 111.685, -161.098, 1.49204, 113.406, -66.5854)]
Traceback (most recent call last):
  File "z-matrix-filtering.py", line 11, in <module>
    print z_matrix[:,0]
IndexError: too many indices

这是怎么回事了?有没有更好的方式来访问列?我会读文件的所有行,从8对显著方差检验所有列,删除不显著更改任何列,然后重新打印结果作为新的CSV。

What is going wrong? Is there a better way to access the columns? I will be reading all lines of a file, testing all columns from the 8th for significant variance, removing any columns that don't vary significantly, and then reprinting the result as a new CSV.

编辑:
基于反应,我已经创建了下面的非常难看,我觉得空洞无物的方法。

Based on responses, I have created the following very ugly and I think inane approach.

all_data = adapter[:]
z_matrix = []

for line in all_data:
    to_append = []
    for column in range(8,len(all_data.dtype)):
        to_append.append(line[column].astype(np.float16))
    z_matrix.append(to_append)

z_matrix = np.array(z_matrix)

这列必须直接访问的理由是,有在数据中的字符串。如果这个字符串不是以某种方式规避,错误将被抛出约空隙阵列使用缓冲区错误对象成员。
有没有更好的解决方案吗?这似乎是可怕的,而现在看来,这将是低效的数据的数千兆字节。

The reason that the columns must be directly accessed is that there is a String inside the data. If this string is not circumvented in some way, an error will be thrown about a void-array with object members using buffer error. Is there a better solution? This seems terrible, and it seems it will be inefficient for several gigabytes of data.

推荐答案

注意打印z_matrix 输出的形式

[ (18.712, 64.903, ..., -138.173)
  (17.679, 48.015, ..., -66.5854)]

也就是说,它打印为元组的列表。这就是你当阵列是一种结构化阵列的输出。它是结构的一维阵列。阵列中的每个元素有18个领域。错误发生,因为你正试图指数一维数组,就好像它是2-D; z_matrix [:0] 将无法正常工作

打印数组的数据类型以查看详细信息。例如。

Print the data type of the array to see the details. E.g.

print z_matrix.dtype

这应该显示的字段的名字和他们的个人数据类型

That should show the names of the fields and their individual data types.

您可以得到元素为一体,例如, z_matrix [K] (其中 K 是一个整数),或者您也可以访问列(其实是一个结构化数组的字段)为 z_matrix ['名'] (改变的名字在DTYPE领域)中的一个。

You can get one of the elements as, for example, z_matrix[k] (where k is an integer), or you can access a "column" (really a field of the structured array) as z_matrix['name'] (change 'name' to one of the fields in the dtype).

如果该领域均具有相同的数据类型(它看起来像这里的情况 - 每个字段的类型为 np.float64 ),您可以创建一个2-D视图中的数据通过重塑视图方法的结果。例如:

If the fields all have the same data type (which looks like the case here--each field has type np.float64), you can create a 2-D view of the data by reshaping the result of the view method. For example:

z_2d = z_matrix.view(np.float64).reshape(-1, len(z_matrix.dtype.names))

度日数列中的数据的另一种方式,而不是名称是:

Another way to get the data by column number rather than name is:

col = 8  # The column number (zero-based).
col_data = z_matrix[z_matrix.dtype.names[col]]

有关更多信息结构化阵列,请参见 HTTP://docs.scipy .ORG / DOC / numpy的/用户/ basics.rec.html

For more about structured arrays, see http://docs.scipy.org/doc/numpy/user/basics.rec.html.

这篇关于numpy的阵列的访问列?错误试图通过移调或列访问攻略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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