Reputation: 5512
I have a numpy array of the following shape
(categories, models, types, events, days) -> (2, 3, 4, 100, 14)
Now, I want to calculate the maximum of 14 days of data per event for a particular category, model, and type
I am doing this
np.max(data[0][0][0], axis=1)
I also want to calculate the max per type and per model for example.
I will be doing several of these operations by looping through [0]
as [i]
.
Is this the correct way of accessing the outermost array? Is there another way?
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)
Also,
type(np.array(data)) = numpy.ndarray
type(data) = list
I convert it for these operations.
Upvotes: 0
Views: 123
Reputation: 655
What you have now is a one dimensional array. You can reshape the array to 2D, this way it is easier to access a column. To access all elements of a column use :
. If each column has a specific meaning (events, days, etc.), you could also consider to store the data as a dictionary instead, as {'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 ]
Upvotes: 3