为什么numpy.concatenate对于小的一维数组沿轴= 1工作,而不是更大的?

时间:2015-07-08 23:32:11

标签: python arrays numpy concatenation

我无法获得我的4年,年,小时和分钟的数组来连接我想要的方式,所以我决定在比我的数据更短的数组上测试几种变体。

我发现它使用我的测试代码中的方法“t”工作:

import numpy as np

a=np.array([[1, 2, 3, 4, 5, 6]])
b=np.array([[11, 12, 13, 14, 15, 16]])
c=np.array([[21, 22, 23, 24, 25, 26]])
d=np.array([[31, 32, 33, 34, 35, 36]])

print a
print b
print c
print d

q=np.concatenate((a, b, c, d), axis=0)
#concatenation along 1st axis
print q

t=np.concatenate((a.T, b.T, c.T, d.T), axis=1)
#transpose each array before concatenation along 2nd axis
print t

x=np.concatenate((a, b, c, d), axis=1)
#concatenation along 2nd axis
print x

但是当我尝试使用较大的数组时,它的行为与方法“q”相同。

我找到了一种使用vstack over here的替代方法,它做了我想要的,但我想弄清楚为什么连接有时适用于此,但并非总是如此。

感谢您的任何见解。

此外,这里是代码的输出:

问:

[[ 1  2  3  4  5  6]    
 [11 12 13 14 15 16]    
 [21 22 23 24 25 26]
 [31 32 33 34 35 36]]

T:

[[ 1 11 21 31]
 [ 2 12 22 32]
 [ 3 13 23 33]
 [ 4 14 24 34]
 [ 5 15 25 35]
 [ 6 16 26 36]]

X

[[ 1  2  3  4  5  6 11 12 13 14 15 16 21 22 23 24 25 26 31 32 33 34 35 36]]

编辑:我将方法t添加到已经使用vstack修复的代码部分的末尾,因此您可以比较vstack如何使用此数据但不能连接。再说一遍,我已经找到了一个解决方法,但我不知道为什么连接方法似乎不一致。

以下是代码:

import numpy as np


BAO10m=np.genfromtxt('BAO_010_2015176.dat', delimiter=",", usecols=range(0-6), dtype=[('h', int), ('year', int), ('day', int), ('time', int), ('temp', float)])
#10 meter weather readings at BAO tower site for June 25, 2015

hourBAO=BAO10m['time']/100
minuteBAO=BAO10m['time']%100
#print hourBAO
#print minuteBAO
#time arrays

dayBAO=BAO10m['day']
yearBAO=BAO10m['year']
#date arrays

datetimeBAO=np.vstack((yearBAO, dayBAO, hourBAO, minuteBAO))
#t=np.concatenate((a.T, b.T, c.T, d.T), axis=1)  <this gave desired results in simple tests
#not working for this data, use vstack instead, with transposition after stack
print datetimeBAO
test=np.transpose(datetimeBAO)
#rotate array
print test
#this prints something that can be used for datetime

t=np.concatenate((yearBAO.T, dayBAO.T, hourBAO.T, minuteBAO.T), axis=1)
print t
#this prints a 1D array of all the year values, then all the day values, etc...
#but this method worked for shorter 1D arrays

我使用的文件可以在this site.

找到

0 个答案:

没有答案