简体   繁体   中英

numpy matrix of hour (24) and day (365)

I have two vectors; one for hours in the day [1,2,3,...,24] , and the second for days in the year [1,2,3,4,5,6,...,365]

I would like to construct a matrix of 24*365 cells, 24 rows and 365 columns.

Something like:

a = [(1,24),(2,24),(3,24),(4,24),(5,24),...,(365,24),
     (1,23),(2,23),(3,23),(4,23),(5,23),...,(365,23),
     (1,22),(2,22),(3,22),(4,22),(5,22),...,(365,22),
          .,
          .,
          .,
          .,
     (1,1),(2,1),(3,1),(4,1),(5,1),...,(365,1)]

After I would like to apply a function f(x,y) and replace the x,y with z (keeping the same matrix structure). Eventually this will be converted to a color map with a ramp.

It's probably worth noting that while you might be able to store general purpose objects in numpy arrays, it probably isn't a good idea - most of the algorithms are optimised to have a single value in each slot in the matrix.

The consequence of this is that you're probably not going to end up with a 24 x 365 element matrix with two values in each slot, you'll instead end up with a numpy array of 2 x 24 x 365 elements.

One way to do something similar to this is:

hours = numpy.arange(365).reshape(1,-1).repeat(24,axis=0)
days = numpy.arange(24).reshape(-1,1).repeat(365,axis=1)
full = numpy.array([days, hours])
print full
print full.shape

Which gives:

[[[  0   0   0 ...,   0   0   0]
  [  1   1   1 ...,   1   1   1]
  [  2   2   2 ...,   2   2   2]
  ..., 
  [ 21  21  21 ...,  21  21  21]
  [ 22  22  22 ...,  22  22  22]
  [ 23  23  23 ...,  23  23  23]]

 [[  0   1   2 ..., 362 363 364]
  [  0   1   2 ..., 362 363 364]
  [  0   1   2 ..., 362 363 364]
  ..., 
  [  0   1   2 ..., 362 363 364]
  [  0   1   2 ..., 362 363 364]
  [  0   1   2 ..., 362 363 364]]]
(2, 24, 365)

While I completely agree with @Andrew Walker (3-d arrays will be much more efficient), the following code produces the array you asked for:

import numpy as np
a = np.empty((24, 365), dtype=tuple)
for i, h in enumerate(range(24, 0, -1)):
    for k, d in enumerate(range(1, 366)):
        x[i, k] = (d, h)

Although this sort of nested for loop is in-efficient and surely unavoidable, the time required to create this array is very low. If you need a more optimized version of this (one that avoids loops), just say so in a comment.

You can have an array that has the same structure as you wanted with this:

a = np.mgrid[1:366, 24:0:-1].T

The first and last rows:

a[0]
array([[  1,  24],
       [  2,  24],
       [  3,  24],
       ...,
       [363,  24],
       [364,  24],
       [365,  24]])

a[-1]
array([[  1,   1],
       [  2,   1],
       [  3,   1],
       ..., 
       [363,   1],
       [364,   1],
       [365,   1]])

And the first and last columns:

a[:,0]
array([[ 1, 24],
       [ 1, 23],
       [ 1, 22],
       ...,
       [ 1,  3],
       [ 1,  2],
       [ 1,  1]])

a[:,-1]
array([[365,  24],
       [365,  23],
       [365,  22],
       ...,
       [365,   3],
       [365,   2],
       [365,   1]])

All the days, and all the years:

a[...,0]
array([[  1,   2,   3, ..., 363, 364, 365],
       [  1,   2,   3, ..., 363, 364, 365],
       [  1,   2,   3, ..., 363, 364, 365],
       ..., 
       [  1,   2,   3, ..., 363, 364, 365],
       [  1,   2,   3, ..., 363, 364, 365],
       [  1,   2,   3, ..., 363, 364, 365]])

a[...,1]
array([[24, 24, 24, ..., 24, 24, 24],
       [23, 23, 23, ..., 23, 23, 23],
       [22, 22, 22, ..., 22, 22, 22],
       ..., 
       [ 3,  3,  3, ...,  3,  3,  3],
       [ 2,  2,  2, ...,  2,  2,  2],
       [ 1,  1,  1, ...,  1,  1,  1]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM