如何结合两个python矩阵numpy

时间:2017-02-22 07:10:36

标签: python numpy matrix concatenation

我有这个CSV“TEMP2”的数据如下所示。

 1376460059,4,33.29,33.23,33.23,33.29,33.23,33.29,33.29,33.29,33.33,33.29,33.33,33.29,33.33,33.33,33.37,33.33,33.33,33.33,33.33,33.37,33.37,33.37,33.37

到目前为止,我的工作是:

 import csv
 import numpy as np
 import datetime

 data = np.genfromtxt('TEMP2.csv', delimiter=',')[1:]

 limite=data[0]
 COLUMN_NUM = int(limite)

 data = np.genfromtxt('TEMP2.csv', delimiter=',')[2:]



 for x in range(0, len(data)):
     tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
     tiempo = np.array(tiempo)  
     print tiempo

 results = []
 for i in range(0, len(data)):
     tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
     results.append(tiempo)
     print np.hstack(results)


 if data.shape[0] % 4 == 0:
     print data.reshape((-1, 4))

 else:
     data = np.pad(data, (0, COLUMN_NUM - len(data) % COLUMN_NUM), 'constant')
     print (data)

     print data.reshape((-1, COLUMN_NUM))

这部分代码将为我提供生成每个数据的时间和毫秒数。

 for x in range(0, len(data)):
      tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
      tiempo = np.array(tiempo)  
      print tiempo

我的问题是,如何将此结果设置为矩阵,如下所示:

 [[1376460059.0 1376460059.25 1376460059.5 1376460059.75]
 [1376460060.0 1376460060.25 1376460060.5 1376460060.75]
 [1376460061.0 1376460061.25 1376460061.5 1376460061.75]
 [. . . . . .  . . . . . . . . . . . . .  and so on . .]
 [1376460063.75 1376460064.0 1376460064.25 1376460064.5]]

另外,一旦我拥有它,是否可以将它与我的其他矩阵结合起来得到这样的结果:

 [[33.29        33.23         33.23        33.29]
  [1376460059.0 1376460059.25 1376460059.5 1376460059.75]
  [33.23        33.29         33.29        33.29]
  [1376460060.0 1376460060.25 1376460060.5 1376460060.75]
  [33.33        33.29         33.33        33.29]
  [1376460061.0 1376460061.25 1376460061.5 1376460061.75]
  [ and so on. . . . .. . . . . .. . . . . . . .]]

我真的这么想,因为我有其他帮助来源。但拼命地知道。我已经搜索过,但我现在没有得到。感谢。

1 个答案:

答案 0 :(得分:0)

对于问题的第一部分,您只需在第一次迭代之前创建一个选项卡。

 res = []
 for x in range(0, len(data)):
     tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
     tiempo = np.array(tiempo)  
     print tiempo
     res.append(tiempo)

对于你的问题的第二部分,要结合两个矩阵,你可以使用numpy.concatenate:

 import numpy as np

 np.concatenate((A, B))
 matrix([[ 1.,  2.],
         [ 3.,  4.],
         [ 5.,  6.]])

我没有测试我的代码,所以我希望它能帮到你

我在代码上做了一些工作,现在我有了:

import csv
import numpy as np
import datetime

data = np.genfromtxt('TEMP2.csv', delimiter=',')[1:]

limite=data[0]
COLUMN_NUM = int(limite)

data = np.genfromtxt('TEMP2.csv', delimiter=',')[2:]


results = np.zeros(shape=(len(data)))

for x in range(0, len(data)):
    tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
    results[x] = tiempo


if data.shape[0] % 4 == 0:
    print(data.reshape((-1, 4)))

else:
    data = np.pad(data, (0, COLUMN_NUM - len(data) % COLUMN_NUM), 'constant')


if results.shape[0] % 4 == 0:
    print(results.reshape((-1, 4)))

else:
    results = np.pad(results, (0, COLUMN_NUM - len(results) % COLUMN_NUM),
                  'constant')

data = data.reshape((-1, COLUMN_NUM))
results = results.reshape((-1, COLUMN_NUM))


final_matrix = np.concatenate((data, results))
final_matrix2 = []
for i in range(len(data)):
    final_matrix2.append(list(data[i]))
    final_matrix2.append(list(results[i]))


print(final_matrix)
print(final_matrix2)

我有这个结果

[  3.32900000e+01   3.32300000e+01   3.32300000e+01   3.32900000e+01]
 [  3.32300000e+01   3.32900000e+01   3.32900000e+01   3.32900000e+01]
 [  3.33300000e+01   3.32900000e+01   3.33300000e+01   3.32900000e+01]
 [  3.33300000e+01   3.33300000e+01   3.33700000e+01   3.33300000e+01]
 [  3.33300000e+01   3.33300000e+01   3.33300000e+01   3.33700000e+01]
 [  3.33700000e+01   3.33700000e+01   3.33700000e+01   0.00000000e+00]
 [  1.37646006e+09   1.37646006e+09   1.37646006e+09   1.37646006e+09]
 [  1.37646006e+09   1.37646006e+09   1.37646006e+09   1.37646006e+09]
 [  1.37646006e+09   1.37646006e+09   1.37646006e+09   1.37646006e+09]
 [  1.37646006e+09   1.37646006e+09   1.37646006e+09   1.37646006e+09]
 [  1.37646006e+09   1.37646006e+09   1.37646006e+09   1.37646006e+09]
 [  1.37646006e+09   1.37646006e+09   1.37646006e+09   0.00000000e+00]]
[[33.289999999999999, 33.229999999999997, 33.229999999999997, 33.289999999999999], [1376460059.0, 1376460059.25, 1376460059.5, 1376460059.75], [33.229999999999997, 33.289999999999999, 33.289999999999999, 33.289999999999999], [1376460060.0, 1376460060.25, 1376460060.5, 1376460060.75], [33.329999999999998, 33.289999999999999, 33.329999999999998, 33.289999999999999], [1376460061.0, 1376460061.25, 1376460061.5, 1376460061.75], [33.329999999999998, 33.329999999999998, 33.369999999999997, 33.329999999999998], [1376460062.0, 1376460062.25, 1376460062.5, 1376460062.75], [33.329999999999998, 33.329999999999998, 33.329999999999998, 33.369999999999997], [1376460063.0, 1376460063.25, 1376460063.5, 1376460063.75], [33.369999999999997, 33.369999999999997, 33.369999999999997, 0.0], [1376460064.0, 1376460064.25, 1376460064.5, 0.0]]