我有一个以下形状的numpy数组
(categories, models, types, events, days) -> (2, 3, 4, 100, 14)
现在,我要计算maximum of 14 days of data per event for a particular category, model, and type
我正在这样做
np.max(data[0][0][0], axis=1)
例如,我还想计算每种类型和每种型号的最大值。
我将以[0]
的形式遍历[i]
来完成其中的一些操作。
这是访问最外面的数组的正确方法吗?还有另一种方法吗?
np.max(data[0][0][0], axis=1)
array([ 3.9264417 , 3.3029506 , 3.0707457 , 3.6646023 , 1.7508441 ,
3.1634364 , 6.195052 , 1.5353022 , 1.8033538 , 1.4508389 ,
1.3882699 , 2.0849068 , 3.654939 , 6.6364765 , 3.92829 ,
6.6467876 , 1.5442419 , 4.639682 , 9.361191 , 5.261462 ,
1.7438816 , 5.6970205 , 2.4356377 , 1.6073244 , 2.6177561 ,
6.886767 , 3.890399 , 2.8880894 , 1.9826577 , 1.0888597 ,
4.3763924 , 3.8597727 , 1.790302 , 1.0277777 , 6.270729 ,
9.311213 , 2.318774 , 2.9298437 , 1.139397 , 0.9598383 ,
3.0489902 , 1.6736581 , 1.3983868 , 2.0979824 , 4.169757 ,
1.0739225 , 1.5311266 , 1.4676268 , 1.726325 , 1.8057758 ,
2.226462 , 2.6197987 , 4.49518 , 2.3042605 , 5.7164993 ,
1.182242 , 1.5107205 , 2.2920077 , 2.205539 , 1.4702082 ,
2.154468 , 2.0641963 , 4.9628353 , 1.9987459 , 2.1360166 ,
1.7073958 , 1.943267 , 7.5767093 , 1.3124634 , 2.2648168 ,
1.1504744 , 3.210688 , 2.6720855 , 2.998225 , 4.365262 ,
3.5410352 , 10.765423 , 4.6292825 , 3.1789696 , 0.92157686,
1.663245 , 1.5835482 , 3.1070056 , 1.6918416 , 8.086268 ,
3.7994847 , 2.4314868 , 1.6471033 , 1.1688241 , 1.7820593 ,
3.3509188 , 1.3092748 , 3.7915008 , 1.018912 , 3.2404447 ,
1.596657 , 2.0869658 , 2.6753283 , 2.1096318 , 8.786542 ],
dtype=float32)
还
type(np.array(data)) = numpy.ndarray
type(data) = list
我将其转换为这些操作。
答案 0 :(得分:3)
您现在拥有的是一维数组。您可以将数组重塑为2D,这样可以更轻松地访问列。要访问列的所有元素,请使用:
。如果每一列都有特定的含义(事件,天等),您还可以考虑将数据存储为字典,而不是{{days':array([...]),'events':array( [])}
from numpy import array, float32
import numpy as np
x = array([ 3.9264417 , 3.3029506 , 3.0707457 , 3.6646023 , 1.7508441 ,
3.1634364 , 6.195052 , 1.5353022 , 1.8033538 , 1.4508389 ,
1.3882699 , 2.0849068 , 3.654939 , 6.6364765 , 3.92829 ,
6.6467876 , 1.5442419 , 4.639682 , 9.361191 , 5.261462 ,
1.7438816 , 5.6970205 , 2.4356377 , 1.6073244 , 2.6177561 ,
6.886767 , 3.890399 , 2.8880894 , 1.9826577 , 1.0888597 ,
4.3763924 , 3.8597727 , 1.790302 , 1.0277777 , 6.270729 ,
9.311213 , 2.318774 , 2.9298437 , 1.139397 , 0.9598383 ,
3.0489902 , 1.6736581 , 1.3983868 , 2.0979824 , 4.169757 ,
1.0739225 , 1.5311266 , 1.4676268 , 1.726325 , 1.8057758 ,
2.226462 , 2.6197987 , 4.49518 , 2.3042605 , 5.7164993 ,
1.182242 , 1.5107205 , 2.2920077 , 2.205539 , 1.4702082 ,
2.154468 , 2.0641963 , 4.9628353 , 1.9987459 , 2.1360166 ,
1.7073958 , 1.943267 , 7.5767093 , 1.3124634 , 2.2648168 ,
1.1504744 , 3.210688 , 2.6720855 , 2.998225 , 4.365262 ,
3.5410352 , 10.765423 , 4.6292825 , 3.1789696 , 0.92157686,
1.663245 , 1.5835482 , 3.1070056 , 1.6918416 , 8.086268 ,
3.7994847 , 2.4314868 , 1.6471033 , 1.1688241 , 1.7820593 ,
3.3509188 , 1.3092748 , 3.7915008 , 1.018912 , 3.2404447 ,
1.596657 , 2.0869658 , 2.6753283 , 2.1096318 , 8.786542 ],
dtype=float32)
x = np.reshape(x, (20, 5))
print x[:, -1]
>> [1.7508441 1.4508389 3.92829 5.261462 2.6177561 1.0888597
6.270729 0.9598383 4.169757 1.8057758 5.7164993 1.4702082
2.1360166 2.2648168 4.365262 0.92157686 8.086268 1.7820593
3.2404447 8.786542 ]