简体   繁体   English

小时(24)和天(365)的numpy矩阵

[英]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] 一个用于一天[1,2,3,...,24]中的小时,第二个用于一年中的[1,2,3,4,5,6,...,365]

I would like to construct a matrix of 24*365 cells, 24 rows and 365 columns. 我想构建一个24 * 365单元,24行和365列的矩阵。

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). 之后,我想应用一个函数f(x,y)并用z替换x,y (保持相同的矩阵结构)。 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. 可能值得注意的是,虽然您可以将通用对象存储在numpy数组中,但这可能不是一个好主意-大多数算法已优化为在矩阵的每个插槽中都有一个值。

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. 这样的结果是,您可能最终不会得到每个插槽中都有两个值的24 x 365元素矩阵,而最终会得到2 x 24 x 365元素的numpy数组。

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: 尽管我完全同意@Andrew Walker(3-d数组将效率更高),但以下代码生成了您要求的数组:

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. 尽管这种嵌套的for循环效率低下并且肯定是不可避免的,但是创建此数组所需的时间却很短。 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]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按小时和星期几子集numpy数组 - Subsetting numpy array by hour and day of week 在python中将12小时时间格式转换为24小时时间格式(一天的记录) - Convert a 12 hour time format to 24 hour time format (keeping record of the day) in python 是否可以将24小时制转换为一天中的四个类别或四个象限? - Is there a way to convert 24hour time format into four categories or four quadrants of the day? 如何从 pandas 和 python 中的给定 24 小时时间格式中获取一天中的部分时间 - how to get the part of the day from a given 24 hour time format in pandas and python 通过pd.TimeGrouper('D')按天汇总每小时时间序列; 发行@时间戳00:00:00(24小时) - aggregating hourly time series by Day via pd.TimeGrouper('D'); issue @ timestamp 00:00:00 (hour 24) 当值在每天 24 小时内至少一次超过阈值时,在每小时数据的 pandas 数据框中计算每年的天数 - Counting number of days per year in a pandas dataframe of hourly data when value exceeds a threshold at least once in 24 hour day 24小时制变成12小时制 - 24 hour time into 12 hour time 将12小时格式转换为24小时Python - Convert 12 hour format to 24 hour Python 如何对一列csv中的子集求和并将其写入python(将每小时的数据行汇总为日数据/8000+行到365) - how to sum subsets from one column of csv and write it in python (sum lines of data for each hour into day data / 8000+lines into 365) 将24小时制转换为12小时制 - Converting 24 hour time to 12 hour time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM