添加文本列和行标头numpy的数组 [英] Adding textual column and row headers to numpy array

查看:1203
本文介绍了添加文本列和行标头numpy的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code创建从三维阵列中的二维矩阵总结:

I am creating a 2d summary matrix from a 3d array using the following code:

numTests=len(TestIDs)
numColumns=11
numRows=6
SummaryMeansArray =  p.array([])
summary3dArray = ma.zeros((numTests,numColumns,numRows))

j=0
for j in range(0,len(TestIDs)):
    print 'j is:  ',j
    TestID=str(TestIDs[j])
    print 'TestID is:  ',TestID
    reader=csv.reader(inputfile)

    m=1
    for row in reader:
        if row[0]!='TestID':
            summary3dArray[j,1,m] =row[2]
            summary3dArray[j,2,m] =row[3]
            summary3dArray[j,3,m] =row[4]
            summary3dArray[j,4,m] =row[5]
            summary3dArray[j,5,m] =row[6]
            summary3dArray[j,6,m] =row[7]
            summary3dArray[j,7,m] =row[8]
            summary3dArray[j,8,m] =row[9]
            summary3dArray[j,9,m] =row[10]
            summary3dArray[j,10,m] =row[11]
            m+=1
    inputfile.close()
outputfile=open(outputFileName, "wb")
writer = csv.writer(outputfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
outputfile.close()

smith='test'

summary3dArray.mask = (summary3dArray.data == 0) # mask all data equal to zero
summaryMeansArray = mean(summary3dArray, axis=0) # the returned shape is (numColumns,numRows)
print 'SummaryMeansArray is:  ',summaryMeansArray

通过打印二维矩阵返回的数据是:

The data returned by printing the 2d matrix is:

SummaryMeansArray is:   [[-- -- -- -- -- --]  
[-- 0.872486111111 0.665114583333 0.578107142857 0.495854166667 0.531722222222]  
[-- 69.6520408802 91.3136933451 106.82865123 125.834593798 112.847127834]  
[-- 1.26883876577 1.64726525154 1.82965948427 1.93913919335 1.81572414167]  
[-- 0.0707222222222 0.0696458333333 0.0654285714286 0.06196875 0.0669444444444]  
[-- 0.219861111055 0.195958333333 0.179925 0.1641875 0.177]  
[-- 0.290583333278 0.265604166667 0.245353571429 0.22615625 0.243944444444]  
[-- 24.1924238322 23.4668576333 23.2784801383 22.8667912971 21.0416383955]  
[-- 90.7234287345 108.496149905 112.364863351 113.57480005 144.061033524]  
[-- 6.16448575902 9.7494285825 11.6270150699 13.5876342704 16.2569218735]  
[-- 0.052665615304 0.069989497088 0.0783212378582 0.0846757181338 0.0862920065249]]  

我有两个问题:的结果
1)我想文本行标题和列标题添加到summaryMeansArray,但我收到错误消息当我尝试现在做到这一点。 什么是对该code添加行标题和列标题的正确语法?

I have two questions:
1.) I want to add textual row headers and column headers to summaryMeansArray, but I am getting error messages when I try to do this now. What is the proper syntax for adding row headers and column headers in this code?

2) 是summaryMeansArray设置有11列6行?我的理解是正确的语法列,行。然而,似乎要打印出高于11行和6列。这只是因为蟒集团按约定自己的括号内的每一列的数据?还是我搞砸语法?

2.) Is summaryMeansArray set up to have 11 columns and 6 rows? My understanding is that the proper syntax is columns,rows. However, it seems to be printing out 11 rows and 6 columns above. Is this just because python groups each column's data within its own brackets by convention? Or did I mess up the syntax?

推荐答案

1。)我会建议储存列和行头信息在一个单独的数据结构。 numpy的矩阵可以存储混合数据类型(在这种情况下,字符串和浮点数),我会尽量避免它。混合数据类型是混乱和效率低下,似乎对我。如果你愿意,你可以使自己的类与矩阵数据,并在其头信息。这似乎是一个清洁的解决方案给我。

1.) I would recommend storing column and row header information in a separate data structure. Numpy matrices can store mixed data types (in this case strings and floats), I try to avoid it. Mixing data types is messy and seems inefficient to me. If you want to, you can make your own class with your matrix data and header information in it. It seems like a cleaner solution to me.

2)否,summaryMeansArray被设置以具有11行和6列。矩阵的第一维是行数。你可以得到summaryMeansArray的转置与 summaryMeansArray.T 。当你正在服用summary3dArray的平均值在第0轴,下轴成为行和列一前一后。

2.) No, summaryMeansArray is set-up to have 11 rows and 6 columns. The first dimension of a matrix is the number of rows. You can get the transpose of summaryMeansArray with summaryMeansArray.T. When you are taking the mean of summary3dArray on the 0th axis, the next axis becomes the rows and the one after that the columns.

编辑:根据要求,你可以从该方法的numpy的阵列创建一个Python列表了ToList()。例如,

As per request, you can create a python list from a numpy array with the method tolist(). For instance,

newMeansArray = summaryMeansArray.tolist()

然后就可以使用插入列标题

Then you can insert the column headers using

newMeansArray.insert(0,headers)

插入行标题是可以做到的:

Inserting the row headers can be done with:

newMeansArray[i].insert(0,rowheader)

每一行i。当然,如果你已经插入的列标题,然后我计数从1开始,而不是0。

for each row i. Of course, if you've already inserted the column headers, then the counting for i starts with 1 rather than 0.

这篇关于添加文本列和行标头numpy的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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